Thursday 13 October 2016

method overriding in java ?


What do you mean by method  overriding? Why it is needed? Explain the uses of this and super keyword with example.




The benefit of overriding is: ability to define a behavior that's specific to the subclass type, which means a sub class can implement a parent class method based on its requirement.

In object-oriented terms, overriding means to override the functionality of an existing method

Method Overriding:

Method overriding means have same signature but with different implementation.

If subclass (child class) has same method as declared in the parent class, it is known as method overriding in java.

In other words, if sub class provides the specific implementation of a method that is already provided by its super class. it is known as

method overriding.

Super class reference can be used to refer to a subclass object. The dynamic method dispatch mechanism in java selects the appropriate version of an overridden method to execute based on the class of the executing object, not the type of a variable that references that object. 

Usage of java Method Overriding:

Method overriding is used to provide specific implementation of the method that is already provided by its super class. 

Method overriding is used for runtime polymorphism.

Why it is Needed?

Method overriding in object oriented programming is a language feature that all allows a subclass or child class to specific implementation of a method that is already provided by one of its super classes or parent classes.

The implementation in the subclass override(replaces)the implementation in the super class by providing a method that has same name, same parameters or signature and same return type as the method in the parent class.

Super keyword:

super keyword in java is a reference variable that is used to refer parent class object super class an implicit keyword create by JVM and supply each and every java program for performing impotent in three places:

---> At variable level
---> At method level
---> At constructor level

Need of super keyword:

Whenever the derived class is inherits the base class features there is a possibility that base class features are similar to derived class features and JVM gets an ambiguity.

In order to different between base class features and derived class feature must be preceded by super keyword.

Usage of java super Keyword:

1. super is used to refer immediate parent class instance variable.
2. super() is used to invoke immediate parent class constructor.
3. super is used to invoke immediate parent class method.


Real example of Java Method Overriding:

Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7% and 9% rate of interest.




Example:

class A
{
int i;
A()
{
}
A(int i)
{
this.i=i;
}
void print()
{
System.out.println(this.i);
}
}
class B extends A
{
int k;
B(int k)
{
super(k);
this.k=k;
}
B()
{
super.i=5;
this.k=5;
}
void print()
{
super.print();
System.out.println(this.k);
}
public static void main(String s[])
{
B b1=new B(50);
b1.print();
}
}


OutPut:
50
50

No comments:
Write comments

Labels

ADVERTISEMENT