Together to learn Java (eighteen) ----- super keyword

Short step, a thousand miles; not small streams, into jianghai.

 

Java language foundation

 

Java's super keyword

super is a direct reference to the parent object. The method may be accessed or properties of the parent class covered by subclass super.

The first line of all construction methods not explicitly called Super (...) or the this (...); then calls the Java default super (), meaning no-argument constructor call the parent class. Here super () can be omitted.

 

  • Call the parent class method or property in the subclass

Call parent class property or method in the subclass. In particular subclasses have the same attributes as the parent class or method, so then if you want to call the property or method of the parent class, then you must use the super keyword. Because the child class and the parent class property or method of the same name, then the parent class method or property to be covered or hidden. super may refer to the parent class of class attributes and class methods.

class Father {
	
	public String name = "tutu";
	public int age = 24;
	
	public void sleep() {
		System.out.println("Father is sleeping!");
	}
	public void work() {
		System.out.println("Father is working!");
	}
}

public class Child extends Father {
	
	public String name = "tututu";
	
	private void test() {
		System.out.println("Father的名字是:" + super.name);
		System.out.println("Child的名字是:" + name);
		System.out.println("Father的年龄是:" + age);
	}
	
	public void sleep() {
		System.out.println("Child is sleeping!");
	}
	
	private void testmethod() {
		sleep();
		super.sleep();
		super.work();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Child child = new Child();
		child.test();
		child.testmethod();

	}
}

Program output:

Father's name: Tutu 
Child's name: tututu 
Father's age: 24- 
! Sleeping Child IS 
! Father IS Sleeping 
Father IS Working!

  

  • In the constructor for the subclass is the parent class to refer

In Java, a subclass is the parent class of the derived class, it instantiates dependent on the parent class is instantiated. So any one of its constructors must initialize the parent class, Java is to call the super keyword parent class constructor, to complete this operation.

class Father {
	
	public String name = "tutu";
	public int age = 24;
	
	public Father(int age){
		
	}	
	public void sleep() {
		System.out.println("Father is sleeping!");
	}
}

public class Child extends Father {
	
	public Child(int age) {
		super(age);
	}
	public String name = "tututu";
		
	public void printname() {
		System.out.println("Father's name is:" + super.name + "!");
	}
	
	private void testmethod() {
		sleep();
		super.sleep();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Child child = new Child(15);
		child.printname();
		child.testmethod();

	}
}

Program output:

Father's name is:tutu!
Father is sleeping!
Father is sleeping!

  

note:

In the constructor, if we do not explicitly call super to initialize the parent class, then Java will implicitly call super (); to call the parent class and put it first on the no-argument constructor constructor, Java only implicit call no-argument constructor, if the parent is not no-argument constructor, then the subclass must show the super keyword to call the call has been argument constructor to initialize the parent class.

 

Blog reference: https://blog.csdn.net/u012518665/article/details/80353225

Guess you like

Origin www.cnblogs.com/smilexuezi/p/11936678.html