Java super keyword
In this chapter you will learn:
- What is super keyword
- How to use super to call superclass constructors
- How to use super to reference members from parent class
Description
You can use super
in a subclass to refer to its immediate superclass.
super
has two general forms.
- The first calls the superclass' constructor.
- The second is used to access a member of the superclass.
Call superclass constructors
To call a constructor from its superclass:
super(parameter-list);
parameter-list
is defined by the constructor in the superclass.super(parameter-list)
must be the first statement executed inside a subclass' constructor.
Here is a demo for how to use super to call constructor from parent class.
class Box {//w w w . ja v a2 s . c o m
private double width;
private double height;
private double depth;
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
class BoxWeight extends Box {
double weight; // weight of box
BoxWeight(Box ob) { // pass object to constructor
super(ob);
}
}
public class Main {
public static void main(String args[]) {
Box mybox1 = new Box(10, 20, 15);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
}
}
This program generates the following output:
Volume of mybox1 is 3000.0
Reference members from parent class
We can reference member variable from parent class with super keyword. Its general form is:
super.member
member
can be either a method or an instance variable.
Let's look at the following code.
class Base {// w w w . j a v a 2s. c o m
int i;
}
class SubClass extends Base {
int i; // this i hides the i in A
SubClass(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}
public class Main {
public static void main(String args[]) {
SubClass subOb = new SubClass(1, 2);
subOb.show();
}
}
This program displays the following:
Next chapter...
What you will learn in the next chapter:
- How to override a method from parent class
- Example - Java Method Overriding
- How to use super to access the overridden method
- When to overload method and when to override method
Java Class
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files
Java Object
Java Object Reference Variable
Java Methods
Java Method Return
Java Method Parameters
Java Class Constructors
Java Default Constructor
Java Constructor Parameters
Java this Keyword
Java static keyword
Java Method Overload
Java Constructors Overload
Java Method Argument Passing
Java Method Recursion
Java Nested Class
Java Anonymous Classes
Java Local Classes
Java Member Classes
Java Static Member Classes
Java Class Variables
Java main() Method
Java Class Inheritance
Java super keyword
Java Method OverridingJava Constructor in hierarchy
Polymorphism
Java final keyword
Java Abstract class
Java Class Access Control
Java Package
Java Packages Import
Java Interface
Java Interface as data type
Java interface as build block
Java instanceof operator
Java Source Files