Java Inheritance - How To Extend A Class
We are going to cover Java inheritance. This will effectively allow you to extend a class.
Quick Answer: This is how you extend a class in Java using inheritance:
class MySuperClass {
.....
.....
}
class MySubClass extends MySuperClass {
.....
.....
}
Keep reading for more complete examples.
Overriding - If you define a method in a subclass with the same signature ( name and params ) as a method in the superclass it will replace the original method. You can also call the original method in the superclass from the new, overridden method in the subclass.
When you override a method it can be useful to call the original method in the superclass from the new method. Call the version of a method in the super class like this:
super.showData2();
Using super refers to the super class instance and needs to be called from within the subclass. It also can’t be used in a static context because it refers to the current instance which won’t exist in a static context.
If you have not overridden a method then you can call that method in the superclass the same as though it were defined in the subclass.
Here is a more complete example:
class MySuperClass {
public void showData1() {
System.out.println("From showData1() in superclass");
}
public void showData2() {
System.out.println("From showData2() in superclass");
}
public void showInfo() {
System.out.println("From showInfo() in superclass");
}
}
class MySubClass extends MySuperClass {
public void showData1() {
System.out.println("From showData1() in subclass");
}
public void showData2() {
super.showData2();
System.out.println("From showData2() in subclass");
}
public void extraInfo() {
System.out.println("From extraInfo() in subclass");
}
public static void main( String[] args ) {
MySubClass x = new MySubClass();
x.showData1(); // defined in both, overridden
x.showData2(); // same but sub calls super
x.showInfo(); // only defined in super
x.extraInfo(); // only defined in sub
}
}
The output will look like this:
From showData1() in subclass
From showData2() in superclass
From showData2() in subclass
From showInfo() in superclass
From extraInfo() in subclass
Here is an example showing how you can call the superclass constructor from the subclass constructor. In this example the subclass does some extra stuff (adding 5 ) and then calls the superclass constructor which actually assigns the value as an attribute of the class. Note that the call to super needs to be the first statement in the constructor.
class MySuperClass {
public int z;
public MySuperClass( int z ) {
this.z = z;
}
}
class MySubClass extends MySuperClass {
public int x;
public MySubClass( int z, int x ) {
super(z);
this.x = x;
}
public static void main( String[] args ) {
MySubClass y = new MySubClass( 3, 4 );
System.out.println( y.z );
System.out.println( y.x );
}
}
Inheritance works with static methods too but you can’t use super to call a superclass method.
class MySuperClass {
public static void showData1() {
System.out.println("From showData1() in superclass");
}
}
class MySubClass extends MySuperClass {
public static void showData1() {
System.out.println("From showData1() in subclass");
}
public static void main( String[] args ) {
MySubClass.showData1(); // defined in both, overridden
}
}