Understanding Java Inheritance, super, this, protected, and Method Overriding
This article explains Java inheritance, the use of extends, super and this keywords, protected members, method overriding, and constructor chaining, illustrating concepts with clear code examples and diagrams to show base and derived class relationships.
Inheritance is a core concept of object‑oriented programming. It allows a class to reuse the definition of another class, reducing code duplication compared with composition.
Class Inheritance
Initially we define a Human class with accessor, mutator, and a breath() method:
class Human {
/** accessor */
public int getHeight() {
return this.height;
}
/** mutator */
public void growHeight(int h) {
this.height = this.height + h;
}
/** breath */
public void breath() {
System.out.println("hu...hu...");
}
private int height;
}If we create a Woman class from scratch, we would repeat all of the above code and only add a new method giveBirth() :
class Woman {
public int getHeight() { return this.height; }
public void growHeight(int h) { this.height = this.height + h; }
public void breath() { System.out.println("hu...hu..."); }
/** new method */
public Human giveBirth() {
System.out.println("Give birth");
return new Human(20);
}
private int height;
}Writing the same members again is tedious. By letting Woman extend Human , the derived class automatically inherits all public members of the base class.
class Woman extends Human {
/** new method */
public Human giveBirth() {
System.out.println("Give birth");
return new Human(20);
}
}Now Woman has getHeight() , growHeight() , and breath() without redefining them.
Derived Layer
The creation of a derived object first constructs a base‑class subobject, then adds the derived‑class members. The public interface seen by external code is the union of base and derived public members.
protected
Members marked protected are visible in the class itself and in all its subclasses, but not to external code.
Method Overriding
If a derived class defines a method with the same signature as a public method in the base class, the derived version overrides the base version. The overridden method can still be invoked from the derived class using super .
class Woman extends Human {
@Override
public void breath() {
super.breath();
System.out.println("su...");
}
}Constructors
When a derived object is created, the base‑class constructor runs first. The derived constructor can explicitly invoke it with super(arguments) .
class Human {
public Human(int h) { this.height = h; }
// other members as before
}
class Woman extends Human {
public Woman(int h) {
super(h); // call base constructor
System.out.println("Hello, Pandora!");
}
// other members and overridden breath as above
}A test program demonstrates usage:
public class Test {
public static void main(String[] args) {
Woman aWoman = new Woman(100);
aWoman.growHeight(120);
System.out.println(aWoman.getHeight());
}
}Summary
The key points are the extends keyword for inheritance, method overriding to customize behavior, the protected access level for subclass visibility, and the use of super (both as a method call and as a member reference) together with constructor chaining.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.