Java Interfaces and inheritance

Source: Internet
Author: User

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

{

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.