Class Pclass
{
void Drew ()
{
System.out.println ("Pclass class: Drew Method! ");
}
}
Class Sclass extends Pclass
{
void Drew ()
{
System.out.println ("Sclass class: Drew Method! ");
}
void Newdrew ()
{
System.out.println ("Sclass class: Newdrew Method! ");
}
public class Classinherit {
public static void Main (String[]args)
{
Pclass obj1=new Pclass (); Create an object based on a parent class
Sclass obj2=new Sclass (); Creating objects based on subclasses
Obj1. Drew ();
Obj2. Drew ();
Obj2.newdrew ();
Obj1=obj2; The object of the subclass is assigned to the parent class
Obj1. Drew (); Method that is already a subclass of Drew
}
}
Characteristics:
1. One is to assign to the parent class only the object of the class, and not to assign the object of the parent class to the child class.
2. Even if an object of a subclass is assigned to a parent class, the parent class object can only invoke methods and member variables in the parent class.
Characteristics of objects that use inheritance when the parent and child classes