Java Concepts Explained Part 7
Java Concepts Explained Part 7
The super keyword refers to the objects of immediate parent class. Before learning super keyword
you must have the knowledge of inheritance in Java so that you can understand the examples given
in this guide.
Lets take an example to understand this: In the following program, we have a data member num
declared in the child class, the member with the same name is already present in the parent class.
There is no way you can access the num variable of parent class without using super keyword. .
Output:
110
super.variable_name
Let’s take the same example that we have seen above, this time in print statement we are passing
super.num instead of num.
class Superclass
{
int num = 100;
}
class Subclass extends Superclass
{
int num = 110;
void printNumber(){
/* Note that instead of writing num we are
* writing super.num in the print statement
* this refers to the num variable of Superclass
*/
System.out.println(super.num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber();
}
}
Output:
100
As you can see by using super.num we accessed the num variable of parent class.
2) Use of super keyword to invoke constructor of parent class
When we create the object of sub class, the new keyword invokes the constructor of child class,
which implicitly invokes the constructor of parent class. So the order to execution when we create
the object of child class is: parent class constructor is executed first and then the child class
constructor is executed. It happens because compiler itself adds super()(this invokes the no-arg
constructor of parent class) as the first statement in the constructor of child class.
class Parentclass
{
Parentclass(){
System.out.println("Constructor of parent class");
}
}
class Subclass extends Parentclass
{
Subclass(){
/* Compile implicitly adds super() here as the
* first statement of this constructor.
*/
System.out.println("Constructor of child class");
}
Subclass(int num){
/* Even though it is a parameterized constructor.
* The compiler still adds the no-arg super() here
*/
System.out.println("arg constructor of child class");
}
void display(){
System.out.println("Hello!");
}
public static void main(String args[]){
/* Creating object using default constructor. This
* will invoke child class constructor, which will
* invoke parent class constructor
*/
Subclass obj= new Subclass();
//Calling sub class method
obj.display();
/* Creating second object using arg constructor
* it will invoke arg constructor of child class which will
* invoke no-arg constructor of parent class automatically
*/
Subclass obj2= new Subclass(10);
obj2.display();
}
}
Output:
We can call super() explicitly in the constructor of child class, but it would not make any sense
because it would be redundant. It’s like explicitly doing something which would be implicitly done
otherwise.
However when we have a constructor in parent class that takes arguments then we can use
parameterized super, like super(100); to invoke parameterized constructor of parent class from the
constructor of child class.
Let’s see an example to understand this:
class Parentclass
{
//no-arg constructor
Parentclass(){
System.out.println("no-arg constructor of parent class");
}
//arg or parameterized constructor
Parentclass(String str){
System.out.println("parameterized constructor of parent class");
}
}
class Subclass extends Parentclass
{
Subclass(){
/* super() must be added to the first statement of constructor
* otherwise you will get a compilation error. Another important
* point to note is that when we explicitly use super in constructor
* the compiler doesn't invoke the parent constructor automatically.
*/
super("Hahaha");
System.out.println("Constructor of child class");
}
void display(){
System.out.println("Hello");
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.display();
}
}
Output:
class Parentclass
{
//Overridden method
void display(){
System.out.println("Parent class method");
}
}
class Subclass extends Parentclass
{
//Overriding method
void display(){
System.out.println("Child class method");
}
void printMsg(){
//This would call Overriding method
display();
//This would call Overridden method
super.display();
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printMsg();
}
}
Output:
What if the child class is not overriding any method: No need of super
When child class doesn’t override the parent class method then we don’t need to use the super
keyword to call the parent class method. This is because in this case we have only one version of
each method and child class has access to the parent class methods so we can directly call the
methods of parent class without using super.
class Parentclass
{
void display(){
System.out.println("Parent class method");
}
}
class Subclass extends Parentclass
{
void printMsg(){
/* This would call method of parent class,
* no need to use super keyword because no other
* method with the same name is present in this class
*/
display();
}
public static void main(String args[]){
Output:
❮ Previous Next ❯
Comments
This is excellent website for java programmers and i have learned a lot from it thanks
Reply
Hi,
I am new to the Java world and will truly appreciate if someone could help and guide
me with the error and getting in the program below –
public class A {
public void f1() {
System.out.println(“A—>f1”);}}
public class B extends A{
public void f1(){
System.out.println(“B—->f1”);}}
In the above program, the first object ‘b’ of class type ‘A’ is working fine. But the object
‘a1’ of class type ‘B’ is giving an error which says – ‘incompatible types: test.A cannot be
converted to test.B’
Is there a way to make the object ‘a1’ work?
Many Thanks
Satya
Reply
Reply
Bharath says
FEBRUARY 29, 2016 AT 5:07 PM
Reply
Reply
Reply
qwerty says
MARCH 31, 2017 AT 5:42 AM
Reply
Leave a Reply
Your email address will not be published. Required fields are marked *
Comment
Name *
Email *
POST COMMENT
Java Tutorial
Java Index
Java Introduction
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
Continue statement
break statement
OOPs Concepts
OOPs Concepts
Constructor
Static keyword
Inheritance
Types of inheritance
Aggregation
Association
Super Keyword
Method overloading
Method overriding
Overloading vs
Overriding
Polymorphism
Types of
polymorphism
Interface
Abstract class vs
interface
Encapsulation
Packages
Access modifiers
Garbage Collection
Inner classes
Static import
Static constructor
Java Interview Q
MORE ...
Java 8 Features
Java 9 Features
Java Conversion
Java String
Exception handling
Java Multithreading
Java I/O
Java Serialization
Java Regex
Java AWT
Java Swing
Java Enum
Java Annotations