The concept and implementation of 2.Java inheritance

Source: Internet
Author: User

Inheritance is the relationship between classes and classes, and is a very simple and intuitive concept, similar to inheritance in the real world (such as a son inheriting a father's property).

Inheritance can be understood as a procedure for a class to get methods and properties from another class. If Class B inherits from Class A, then B has the method and properties of a.

Inheritance uses the extends keyword.

For example, we have defined a class people:

  1. Class people{
  2. String name;
  3. int age;
  4. int height;
  5. void say(){
  6. System. Out. println("My name is" + name + ", age is" + ages + ", height is" + height ");
  7. }
  8. }

If you need to define a class Teacher now, it also has the name, age, Height property, and say () method, and you need to add school, seniority, subject properties, and lecturing () methods. Do we need to redefine a class?

Completely unnecessary, you can first inherit the members of the People class, and then add your own members, for example:

  1. Class Teacher extends people{
  2. String School; //Your school
  3. String subject; //Disciplines
  4. int seniority; //seniority
  5. //overwrite the Say () method in the People class
  6. void say(){
  7. System. Out. println("I call" + name + "," + school + "teach" + subject + ", have" + Seniorit Y + "Years of seniority");
  8. }
  9. void lecturing(){
  10. System. Out. println("I have" + Age + "old, still stand on the podium lectures");
  11. }
  12. }

Description of the program

    • The name and age variables, although not defined in Teacher, are defined in people and can be used directly.
    • Teacher is a subclass of people, people is the parent class of the Teacher class.
    • Subclasses can override methods of the parent class.
    • Subclasses can inherit all members of the parent class except private.
    • The construction method cannot be inherited.


Inheritance is a great step forward in terms of maintenance and reliability. If you make modifications in the People class, the Teacher class is automatically modified without requiring the programmer to do any work, except to compile it.

Single inheritance: Java allows a class to inherit only one other class, that is, a class can have only one parent class, and this restriction is called single inheritance. The concept of Interface (interface) is learned later, and interfaces allow multiple inheritance.

Finally, the above code is organized:

  1. Public class Demo {
  2. public static void main(String[] args) {
  3. Teacher t = new Teacher();
  4. T. Name = "cloth";
  5. T. Age = ;
  6. T. School = "Tsinghua University";
  7. T. Subject = "Java";
  8. T. Seniority = n;
  9. T. Say();
  10. T. Lecturing();
  11. }
  12. }
  13. Class people{
  14. String name;
  15. int age;
  16. int height;
  17. void say(){
  18. System. Out. println("My name is" + name + ", age is" + ages + ", height is" + height ");
  19. }
  20. }
  21. Class Teacher extends people{
  22. String School; //Your school
  23. String subject; //Disciplines
  24. int seniority; //seniority
  25. //overwrite the Say () method in the People class
  26. void say(){
  27. System. Out. println("I call" + name + "," + school + "teach" + subject + ", have" + Seniorit Y + "Years of seniority");
  28. }
  29. void lecturing(){
  30. System. Out. println("I have" + Age + "old, still stand on the podium lectures");
  31. }
  32. }

Operation Result:
My name is Xiao, teaching Java at Tsinghua University, with 12 years of seniority
I'm 70 years old, still standing on the podium and lecturing.

Note: The construction method cannot be inherited, it is important to master this. A class can be constructed with only two methods: writing a constructor method, or not having a constructor at all, and the class has a default constructor.

The concept and implementation of 2.Java inheritance

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.