Java Learning (12), object-oriented programming (4) Inheritance, concepts and super keywords, object-oriented programming super

Source: Internet
Author: User

Java Learning (12), object-oriented programming (4) Inheritance, concepts and super keywords, object-oriented programming super

Inheritance

Concept:

① The idea behind inheritance is to build new classes based on existing classes;

② When an existing class is inherited, its methods and attributes are reused. You can also add new methods and attributes to customize the new class to meet the requirements;

③ When the class exported from other classes is called a subclass, the exported class is called a parent class;

④ In Java, except for the Object class, all classes are subclasses and each class has a unique parent class;

⑤ Inheritance is indispensable in OO;

6. A class is always inherited when it is created;

Relationships between classes: Is-a inheritance, Has-a combination, and Like-a implementation interface;

The significance of inheritance: code reuse, reflecting different abstract levels;

Parent-child relationship: the parent class is more abstract and general; the Child class is more specific and special;

Inheritance features: 1. Subclass inherits members of the parent class; 2. It has a hierarchical structure;

Advantages of inheritance: 1. code reuse; 2. Parent fields and methods can be used for subclasses; 3. Structure System from abstraction to concrete class formation; 4. Subclass can be easily defined;

 

Implementation:

In Java, the extends keyword is used to indicate that a class inherits from another class;

Public class Teacher extends Person {

// Code

}

 

Super keyword

Features:

① Super represents the reference of the parent class object, and this represents the reference of the current object;

② When the sub-parent class members have duplicate names, they can be distinguished by super;

③ In the constructor of the subclass, the constructor of the parent class is called using the super keyword;

Public class JavaTeacher extends Teacher (){

Public JavaTeacher (String name, String school ){

// Call the constructor of the parent class to initialize the related field values.

Super (name, school );

}

}

 

☆When constructing a class object, you must first call the constructor of the parent class to construct the parent class object, the statement that calls the constructor of the parent class must be the first instruction in the subclass constructor;

Of

1 public class TeacherDemo {2 public static void main (String [] args) {3 B B = new B (); 4 5 // after adding the showB () method to the parent class, it calls showB () of the subclass; 6 7 // to call showB () of the parent class; add super in the subclass. showB (); 8 B. showB (); 9} 10} 11 12 class A {13 public A () {14 System. out. println ("constructor of A"); 15} 16 public void showA () {17 System. out. println ("A"); 18} 19 20 public void showB () {21 System. out. println ("showB method in parent class"); 22} 23} 24 25 class B extends A {26 public B () {27 // super (); // call the constructor without parameters of the parent class, which can omit 28 System. out. println ("B constructor"); 29} 30 31 public void showB () {32 System. out. println ("B"); 33 // showA (); // subclass inherits the parent class and can be called. showA (); because the name does not conflict 34 super. showB (); 35 36} 37}View Code

The following code clearly expresses the inheritance

1 public class TeacherDemo {2 public static void main (String [] args) {3 JavaTeacher jTeacher = new JavaTeacher ("James", "Tsinghua"); 4 jTeacher. teaching (); 5 6 DBTeacher dTeacher = new DBTeacher ("", ""); 7 dTeacher. teaching (); 8} 9} 10 11 class Teacher {12 private String name; 13 private String school; 14 15 public Teacher (String name, String school) {16 this. name = name; 17 this. school = school; 18} 19 20 public void teaching () {21 System. out. println ("preparations before teaching"); 22} 23} 24 25 class JavaTeacher extends Teacher {26 public JavaTeacher (String name, String school) {27 super (name, school ); 28} 29 30 public void teaching () {31 super. teaching (); // call the parent class teaching32 System. out. println ("Open eclipse"); 33 System. out. println ("Writing java code"); 34} 35} 36 37 class DBTeacher extends Teacher {38 public DBTeacher (String name, String school) {39 super (name, school ); 40} 41 42 public void teaching () {43 super. teaching (); // call the parent class teaching44 System. out. println ("Open Oracle"); 45 System. out. println ("Writing pl-SQL commands"); 46} 47}View Code

 

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.