There is no more inheritance in the
1.JAVA, a class can have only one parent class. And the expression of inheritance is polymorphism. A parent class can have more than one subclass, and in a subclass you can override the parent class's method (for example, method print ()) so that the code that is rewritten in each subclass is different, and the natural representation is different. This uses the variables of the parent class to refer to the different subclasses, and when the same method print () is invoked, the result is different from the representation, which is polymorphic, and the same message (that is, calling the same method) will have various results. Illustration:
Parent class public class
Father
{
//parent class has a child-beating method public
void Hitchild ()
{
System.out.println ("Bastard, You stand, don't hit you I'm not your father! ");
}} Subclass 1 Public
class Son1 extends Father
{
//overriding parent to beat child method public
void Hitchild ()
{
System.out.println (Why did you hit me?) What have I done wrong! ");
}
}
Subclass 2 Public
class Son2 extends Father
{
//overriding parent to beat child method public
void Hitchild ()
{
System.out.println ("I know it's wrong, don't fight!") ");
}
}
Subclass 3 Public
class Son3 extends Father
{
//overriding parent to beat child method public
void Hitchild ()
{
System.out.println ("I run, you can't hit!") ");
}
}
Test class public
class test
{public
static void Main (String args[])
{
Father Father;
Father=new father (); Father.hitchild ();
Father = new Son1 ();
Father.hitchild ();
Father = new Son2 ();
Father.hitchild ();
Father = new Son3 ();
Father.hitchild ();
}
Run results
C:\java>java Test
You stand, don't beat you, I'm not your father!
Why did you hit me? What have I done wrong!
I know it's wrong, stop fighting!
I run, you can't hit!
Have called the same method, and there have been different results! This is the manifestation of polymorphism!
There is not much inheritance in 2.JAVA, and multiple inheritance is implemented with interfaces! A class or can implement multiple interfaces at the same time! (It's equivalent to a class in C + + that inherits multiple classes at the same time!) e.g.
public class Son implements Father1,father2,father3
{
}