java-inherited Super's use

Source: Internet
Author: User


public class Father {
protected String name;
int age;

Father () {
System.out.println ("Father non-parametric constructor");
}
Father (String name) {
SYSTEM.OUT.PRINTLN ("parameter constructor for person");
}

Public, private, protected, subclass can inherit the public, protected
The default is to be private, not inherited,
Java does not support multiple inheritance, C + + support,
Extends: extension, not the meaning of inheritance

void jump (int lenth) {
System.out.println ("lenth =" +lenth);
}
protected void Jump (int num,int a) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("num =" +num+ "a =" +a);
}
static void Fly () {
System.out.println ("I can fly!");
}
}






public class daughter extends Father {

Daughter () {
Super ();
In the constructor of a subclass, you must call the constructor of the parent class,
Super (); The default call to the parent class's parameterless constructor
System.out.println ("daughter parameterless constructor");
}
Daughter (String name) {
Super ("");
System.out.println ("Daughter with parametric constructor: name =" +name);
}
public static void Main (string[] a) {
/* Regardless of which constructor you use, as long as you do not indicate which constructor of the parent class is used (if the parent class is composed of more than one constructor)
The parent class will call your own default constructor when you call your own constructor, and if the parent class is cut with only one parameter constructor, then be careful.
Because when you call your own constructor and the parent class imposes your own constructor, and the constructor that the parent imposes on you does have arguments, it does not add a parameter to it itself.
So it's still in the form of super (), so this is the equivalent of adding "super ()" to your own constructor, which is certainly wrong, because the parent class doesn't have this constructor
So you need to write the right form "super (" ")", so this is a bit strong to buy strong selling meaning. If the parent class consists of a parameterless and an argument constructor, then you do not need to specify, because the constructor that the parent class imposes on you is
*/
New daughter ();
New daughter ("daughter");

No super in Static methods
Super.jump (100);
Super ();
}
}
/*
* Why the parent class imposes its own constructors on subclasses, because subclasses cannot inherit the parent class's
* Constructor function,
* Moreover, constructors generally carry the role of initializing member variables, while subclasses cannot use the constructor of the parent class
* Then you need to write the method again when initializing the member variable.
* */

java-inherited Super's use

Related Article

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.