JAVA Super Keyword
JAVA Super Keyword
The super keyword in Java is a reference variable which is used to refer immediate
parent class object.
EXAMPLE 1
1. class Animal{
2. String color="white";
3. }
4. class Dog extends Animal{
5. String color="black";
6. void printColor(){
7. System.out.println(color);//prints color of Dog class
8. System.out.println(super.color);//prints color of Animal class
9. }
10. }
11. class TestSuper1{
12. public static void main(String args[]){
13. Dog d=new Dog();
14. d.printColor();
15. }}
Output
black
white
In the above example, Animal and Dog both classes have a common property color. If
we print color property, it will print the color of current class by default. To access the
parent property, we need to use super keyword.
EXAMPLE 2
class Vehicle {
}
// sub class Car extending vehicle
void display()
+ super.maxSpeed);
// Driver Program
class Test {
small.display();
Output
Maximum Speed: 120
In the above example, both the base class and subclass have a member
maxSpeed. We could access the maxSpeed of the base class in subclass
using super keyword.
2) super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method. It should be used if
subclass contains the same method as parent class. In other words, it is used if method
is overridden.
EXAMPLE 1
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){System.out.println("eating bread...");}
6. void bark(){System.out.println("barking...");}
7. void work(){
8. super.eat();
9. bark();
10. }
11. }
12. class TestSuper2{
13. public static void main(String args[]){
14. Dog d=new Dog();
15. d.work();
16. }}
Output
eating...
barking...
In the above example Animal and Dog both classes have eat() method if we call eat()
method from Dog class, it will call the eat() method of Dog class by default because
priority is given to local.
To call the parent class method, we need to use super keyword.
EXAMPLE 2
// superclass Person
class Person {
void message()
// Subclass Student
void message()
void display()
message();
super.message();
}
}
// Driver Program
class Test {
s.display();
Output
This is student class
This is person class
In the above example, we have seen that if we only call
method message() then, the current class message() is invoked but with
the use of the super keyword, message() of the superclass could also be
invoked.
EXAMPLE 1
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
5. Dog(){
6. super();
7. System.out.println("dog is created");
8. }
9. }
10. class TestSuper3{
11. public static void main(String args[]){
12. Dog d=new Dog();
13. }}
Output:
animal is created
dog is created
// superclass Person
class Person {
Person()
{
System.out.println("Person class Constructor");
}
}
// Driver Program
class Test {
public static void main(String[] args)
{
Student s = new Student();
}
}
Output
Person class Constructor
Student class Constructor
In the above example, we have called the superclass constructor using
the keyword ‘super’ via subclass constructor.
EXAMPLE 3
class ParentClass {
public boolean isTrue() { return true; }
}
// prints "false"
System.out.println(result);
}
}
Output
false
1. class Animal{
2. Animal(){System.out.println("animal is created");}
3. }
4. class Dog extends Animal{
5. Dog(){
6. System.out.println("dog is created");
7. }
8. }
9. class TestSuper4{
10. public static void main(String args[]){
11. Dog d=new Dog();
12. }}
Output:
animal is created
dog is created
1. class Person{
2. int id;
3. String name;
4. Person(int id,String name){
5. this.id=id;
6. this.name=name;
7. }
8. }
9. class Emp extends Person{
10. float salary;
11. Emp(int id,String name,float salary){
12. super(id,name);//reusing parent constructor
13. this.salary=salary;
14. }
15. void display(){System.out.println(id+" "+name+" "+salary);}
16. }
17. class TestSuper5{
18. public static void main(String[] args){
19. Emp e1=new Emp(1,"ankit",45000f);
20. e1.display();
21. }}
Output:
Example
Using super to call the superclass of Dog (subclass):
class Animal { // Superclass (parent)
OUTPUT
class Temp
// default constructor 1
Temp()
// calls constructor 2
this(5);
// parameterized constructor 2
Temp(int x)
{
// calls constructor 3
this(5, 15);
System.out.println(x);
// parameterized constructor 3
Temp(int x, int y)
System.out.println(x * y);
new Temp();
Output:
75
5
The Default constructor
Rules of constructor chaining :
1. The this() expression should always be the first line of the constructor.
2. There should be at-least be one constructor without the this() keyword
(constructor 3 in above example).
3. Constructor chaining can be achieved in any order.
class Base
String name;
// constructor 1
Base()
this("");
// constructor 2
Base(String name)
this.name = name;
System.out.println("Calling parameterized constructor"
+ " of base");
// constructor 3
Derived()
"of derived");
// parameterized constructor 4
Derived(String name)
super(name);
"constructor of derived");
}
public static void main(String args[])
Output:
Calling parameterized constructor of base
Calling parameterized constructor of derived
Note : Similar to constructor chaining in same class, super() should be
the first line of the constructor as super class’s constructor are invoked
before the sub class’s constructor.
The Object class aka super class is at the top of the class hierarchy in
Java’s java.lang package. Every class, whether predefined or user-
defined, is a subclass of the Object class.