Java Study Notes 13 (Object-Oriented 6: super), learning notes super

Source: Internet
Author: User

Java Study Notes 13 (Object-Oriented 6: super), learning notes super

When creating a subclass object, the constructor of the parent class is executed first, becauseThe first line of all constructor methods has the default implicit super ();Statement

Note: The first line of the parent class constructor also has an implicit super ();

All classes have an "ancestor class": Object. The parent class inherits it.

 

Super (); has similarities with this (); in the previous article:

This (); is to call its own null parameter Constructor

Super (); is to call the null parameter constructor of the parent class

Example:

Public class Person {public Person () {System. out. println ("parent class constructor ");}}
public class Student extends Person {}
Public class Test {public static void main (String [] args) {new Student () ;}// output: parent class Constructor
// This shows the implicit super ();

 

Principle of sub-class and parent class memory:

In the object space of a subclass object, the object is divided into two areas. One part is left to the parent class, the super mark is used, the other part is left to the user, and the this mark is used.

The member variables of the parent class follow the Child class into the area divided into the parent class.

 

However, if the constructor of the parent class has parameters, the sub-classes must be fully written.

The subclass can overload the constructor, but the first line must be super ()

If the parent class has multiple constructor methods, the subclass can call any one of them.

Example:

public class Person {    public Person(int a){        System.out.println(a);    }    public Person(double d){        System.out.println(d);    }}
public class Student extends Person {    public Student() {        super(0);    }    public Student(String s) {        super(1.5);        System.out.println(s);    }}
Public class Test {public static void main (String [] args) {new Student (); new Student ("... ") ;}}/* output: 01. 5... */

 

 

Here we find a conflict: this () and super () must both be written in the first line of the constructor. ThereforeThe two keywords cannot coexist.

But it must be guaranteedAll constructor methods of the subclass can directly or indirectly call the constructor of the parent class.

Example:

public class Person {    public Person(int a){    }}
Public class Student extends Person {public Student () {// call your own constructor // indirectly calls the constructor of the parent class this ("... ") ;}public Student (String s) {super (2 );}}

 

To better understand super, write a super application here:

public class Person {    private String name;    private int age;        public Person(String name, int age){        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }
public class Student extends Person {    public Student(String name,int age){        super(name, age);    }}
public class Worker extends Person {    public Worker(String name,int age){        super(name, age);    }}
Public class Test {public static void main (String [] args) {Worker worker = new Worker ("Zhang San", 18); Student student = new Student ("Li Si ", 19); System. out. println (worker. getName (); System. out. println (student. getAge ());}}

 

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.