Java Inheritance Base Edition

Source: Internet
Author: User

Inheritance is an effective way to implement code reuse in software development, if a class A inherits from Class B, then the public, protected, and default modifiers of the class B are decorated with instance members or static members that are inherited by Class A. It can also be said that the members of Class B are members of Class A, and Class A can also extend new members on this basis.

If two classes (Class A and Class B) have the same members, they can be abstracted out of their parent class (placing the same members of two classes in the parent class) Class C, which enables code reuse.

Grammar:

1//Class C
2 public abstract Animal{3 public static string color;//static member 4 public String name;//Instance member 5 public Void Eat (String fool)//Implement eating Method 6 {7 System.Out.Println ("I'm Eating" +fool); 8 }9}
10//Class A11 public Cat extends animal{12 //Class A inherits Class C13}14/class B15 public Dog extends animal{16 //Class B inherits class C17}

From the example above, Class A and Class B inherit Class C, so there are two classes that have color and name and how to eat them. But the dog bites, so we can define a method in Class B.

1 public Dog extends animal{  2      //Class A Inherits Class C  3 public      void Eatperson () {  4                    System.Out.Println (" I'll bite. ")  5            }  6}

If there's an animal, it doesn't eat anything else. It only eats fish. Eat () method that we can overwrite the parent class

1 public Cate extends animal{  2 public  void Eat (String fool)  3 {  4      if (fool. Equals ("fish")) {  5    System.Out.Println ("I'm eating fish");  6    }else{  7    System.Out.Println ("I only eat fish!") Take away ");  8   9    } 10} 11}

  

Note points for using inheritance:

Inheritance at the level of inheritance is best not more than three layers because the hierarchy of inheritance if more than three layers will make the program complex, not easy to maintain

The use of inheritance is not to be abused. You cannot extend a subclass by property values.

The disadvantage of inheritance: breaking encapsulation, is the internal implementation of the parent class is completely exposed to subclasses.

Java Inheritance Base Edition

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.