0% found this document useful (0 votes)
81 views9 pages

Super Keyword in Java

The document discusses method overriding in Java. Method overriding occurs when a child class defines a method with the same name and signature as a method in the parent class. The child class's version of the method overrides the parent's version, allowing the child class to provide its own implementation. This is an example of runtime polymorphism, as which version of the method gets called depends on the object's type and is determined at runtime through dynamic method dispatch. The document provides rules and examples of method overriding in Java.

Uploaded by

Chandia Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views9 pages

Super Keyword in Java

The document discusses method overriding in Java. Method overriding occurs when a child class defines a method with the same name and signature as a method in the parent class. The child class's version of the method overrides the parent's version, allowing the child class to provide its own implementation. This is an example of runtime polymorphism, as which version of the method gets called depends on the object's type and is determined at runtime through dynamic method dispatch. The document provides rules and examples of method overriding in Java.

Uploaded by

Chandia Panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Method overriding

Method having the same name and same signature in parent class and in child class is called
method overriding.

Return type may or may not same

Declaring a method in sub class which is already present in parent class is known as method
overriding.

Overriding is done so that a child class can give its own implementation to a method which is
already provided by the parent class. In this case the method in parent class is called
overridden method and the method in child class is called overriding method.

class Human
{
//Overridden method
void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human
{
//Overriding method
void eat()
{
System.out.println(“hello”)
System.out.println("Boy is eating");
}
public static void main( String args[])
{
Boy obj = new Boy();
//This will call the child class version method void eat()
obj.eat();
}
}

We have two classes: A child class Boy and a parent class Human. The Boy class
extends Human class. Both the classes have a common method void eat(). Boy class is giving
its own implementation to the eat() method or in other words it is overriding the eat() method.
The purpose of Method Overriding is clear here. Child class wants to give its own
implementation so that when it calls this method, it prints “hello”, “Boy is eating” instead of
Human is eating.

Advantage of method overriding

The main advantage of method overriding is that the class can give its own specific
implementation to a inherited method without even modifying the parent class code.
This is helpful when a class has several child classes, so if a child class needs to use the
parent class method, it can use it and the other classes that want to have different
implementation can use overriding feature to make changes without touching the parent class
code.

Method Overriding and Dynamic Method Dispatch

Method Overriding is an example of runtime polymorphism. When a parent class reference


points to the child class object then the call to the overridden method is determined at
runtime, because during method call which method(parent class or child class) is to be
executed is determined by the type of object. This process in which call to the overridden
method is resolved at runtime is known as dynamic method dispatch. Lets see an example to
understand this:

class ABC{
//Overridden method
void disp()
{
System.out.println("disp() method of parent class");
}
}
class Demo extends ABC
{
//Overriding method
void disp()
{
System.out.println("disp() method of Child class");
}
void newMethod()
{
System.out.println("new method of child class");
}
public static void main( String args[])
{
/* When Parent class reference refers to the parent class object
* then in this case overridden method (the method of parent class)
* is called.
*/
ABC obj = new ABC();
obj.disp(); //disp() method of child class
/* When parent class reference refers to the child class object
* then the overriding method (method of child class) is called.
* This is called dynamic method dispatch and runtime polymorphism
*/
ABC obj2 = new Demo();
obj2.disp(); //disp() method of parent class
//obj2.newMethod() //gives compile time error
}
}
In the above example the call to the disp() method using second object (obj2) is runtime
polymorphism (or dynamic method dispatch).
Note: In dynamic method dispatch the object can call the overriding methods of child class
and all the non-overridden methods of base class but it cannot call the methods which are
newly declared in the child class. In the above example the object obj2 is calling the disp().
However if you try to call the newMethod() method (which has been newly declared in Demo
class) using obj2 then you would get compilation error with the following message:

Exception in thread "main" java.lang.Error: Unresolved compilation


problem: The method newMethod() is undefined for the type ABC

Rules of method overriding in Java

Argument list: The argument list of overriding method (method of child class) must
match the Overridden method(the method of parent class). The data types of the
arguments and their sequence should exactly match.

Access Modifier of the overriding method (method of subclass) cannot be more


restrictive than the overridden method of parent class. For e.g. if the Access Modifier of
parent class method is public then the overriding method (child class method ) cannot
have private, protected and default Access modifier, because all of these three access
modifiers are more restrictive than public.
For e.g. it would not allowed if child class disp() method is more restrictive(protected)
than base class(public)

class Base
{
public void disp()
{
System.out.println("Parent class method");
}
}
class Child extends Base
{
public void disp() // if protected void disp() then give C.T error as in output
{
System.out.println("Child class method");
}
public static void main( String args[])
{
Child obj = new Child ();
obj.disp();
}
}
Note: if disp() method is declared as protected void disp() then we get error as:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: Cannot reduce the visibility of the inherited method from Base class
However this is perfectly valid scenario as public is less restrictive than protected. Same
access modifier is also a valid one.

1. private, static and final methods cannot be overridden as they are local to the class.
However static methods can be re-declared in the sub class, in this case the sub-class
method would act differently and will have nothing to do with the same static method of
parent class.
2. Overriding method (method of child class) can throw unchecked exceptions,
regardless of whether the overridden method (method of parent class) throws any
exception or not. However the overriding method should not throw checked
exceptions that are new or broader than the ones declared by the overridden method. We
will discuss this in detail with example in the upcoming tutorial.
3. Binding of overridden methods happen at runtime which is known as dynamic
binding.
4. If a class is extending an abstract class or implementing an interface then it has to
override all the abstract methods unless the class itself is an abstract class.

Super keyword
The super keyword refers to the objects of immediate parent class or refers to super class
(parent) objects.

It is used to call super class methods, and to access the super class constructor.

The most common use of the super keyword is to eliminate the confusion between super
classes and subclasses that have methods with the same name.

Use of super keyword

1) To access the data members of parent class when both parent and child class
have member with same name

2) To explicitly call the default and parameterized constructor of immediate parent


class

3) Implicitly call default constructor of parent class from default constructor of


child class

4) To access the method of parent class when child class has overridden that
method.

When you have a variable in child class which is already present in the parent class then in
order to access the variable of parent class, we need to use the super keyword.

In the following program, we have a data member n declared in the child class; the member
with the same name is already present in the parent class. There is no way we can access
the n variable of parent class without using super keyword.

//Parent class or Superclass or base class


class Super
{
int n = 1000;
String s;
double d;
}
//Child class or subclass or derived class
class child extends Super
{
/* The same variable num is declared in the Subclass
* which is already present in the Super class Super
*/
int n = 1110;

void printNumber(String s,double d)


{
super.s=s;
super.d=d;
System.out.println(n); //super.num
System.out.println(super.n);
System.out.println(d);
System.out.println(super.s);
}
}
class subclass
{
public static void main(String args[])
{
child obj= new child();
obj.printNumber("sonam",5.0);
}
}
Accessing the n variable of parent class:
By calling a variable like this, we can access the variable of parent class if both the classes
(parent and child) have same variable name.
super.variable_name

Let’s take the same example that we have seen above, this time in print statement we are
passing super.n instead of num.

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 or default constructor of parent class) as the first statement in the
constructor of child class.

class Parent
{
Parent()
{
System.out.println("Constructor of parent class");
}
}
class Subclass extends Parent
{
Subclass()
{
/* Compiler 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("parameterised constructor of child class");
System.out.println(num);
}
void display()
{
System.out.println("Hello!");
}
}
class corona
{
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(100);
obj2.display();
}
}
Parameterized super() call to invoke parameterized constructor of parent class

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.

class Parentcorona
{
//no-arg constructor
Parentcorona()
{
System.out.println("default constructor of parent class");
}
//arg or parameterized constructor
Parentcorona(int n)
{
System.out.println("parameterized constructor of parent class");
}
}
class Subclass extends Parentcorona
{
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(7); //only call parameterized constructor of parent class but does not pass any value
System.out.println("Constructor of child class");
}
void display()
{
System.out.println("Hello");
}
}
class corona1
{
public static void main(String args[])
{
Subclass obj= new Subclass();
obj.display();
}
}Output:
parameterized constructor of parent class
Constructor of child class
Hello
There are few important points to note in this example:
1) super()(or parameterized super must be the first statement in constructor otherwise you
will get the compilation error: “Constructor call must be the first statement in a constructor”
2) When we explicitly placed super in the constructor, the java compiler didn’t call the
default no-arg or default constructor of parent class.

3) super keyword in case of method overriding or super can be used to invoke parent class
method

When a child class declares a same method which is already present in the parent class then
this is called method overriding. By using super keyword like this: super.method_name we
can call the method of parent class (the method which is overridden). In case of method
overriding, these terminologies are used:
Overridden method: The method of parent class
Overriding method: The method of child class
Example:

class Base
{
//Overridden method
void display()
{
System.out.println("Parent class method");
}
}
class derived extends Base
{
//Overriding method
void display()
{
System.out.println("Child class method");
}
void show()
{
display(); //This would call Overriding method
super.display(); //This would call Overridden method
}
class over
{
public static void main(String args[])
{
derived obj= new derived();
obj.show();
}
}
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[])
{
Subclass obj= new Subclass();
obj.printMsg();
}
}

You might also like