In this method, use override, the display( ) function will be only call the new one, not first one.
x
class University {
public void display() {
System.out.println("My name is David")
}
}
class Student extends University {
public void display() {
System.out.println("Name: " + name);
System.out.println();
}
}
class Main {
public static void main(String[] args) {
Student david = new Student();
david.name = "David Jiang";
david.display();
}
}
Use super keyword can call the function in first class.
xxxxxxxxxx
class University {
public void display() {
System.out.println("My name is David")
}
}
class Student extends University {
public void display() {
super.display();
System.out.println("Name: " + name);
System.out.println();
}
}
class Main {
public static void main(String[] args) {
Student david = new Student();
david.name = "David Jiang";
david.display();
}
}
The same method declared in the superclass and its subclasses can have different access specifiers.
--> P.s. Only use those access specifiers in subclasses that provide larger access than the access specifier of the superclass.
x
class University {
protected void display() {
System.out.println("My name is David")
}
}
class Student extends University {
public void display() {
System.out.println("Name: " + name);
System.out.println();
}
}
class Main {
public static void main(String[] args) {
Student david = new Student();
david.name = "David Jiang";
david.display();
}
}