Method Overriding With Example
Proclaiming a technique in sub class which is as of now present in parent class is known as strategy superseding. Abrogating is done so a youngster class can give its own execution to a strategy which is now given by the parent class. For this situation the strategy in parent class is called abrogated technique and the strategy in youngster class is called superseding strategy. In this guide, we will perceive what is technique abrogating in Java and why we use it.
Strategy Overriding Example
Lets take a basic guide to get this. We have two classes: A youngster class Boy and a parent class Human. The Boy class expands Human class. Both the classes have a typical technique void eat(). Kid class is giving its own execution to the eat() strategy or as such it is superseding the eat() technique.
The motivation behind Method Overriding is clear here. Kid class needs to give its own execution so when it calls this strategy, it prints Boy is eating rather than Human is eating.
class Human{
/Overridden technique
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy broadens Human{
/Overriding technique
public void eat(){
System.out.println("Boy is eating");
}
public static void principle( String args[]) {
Kid obj = new Boy();
/This will call the kid class variant of eat()
obj.eat();
}
}
Yield:
Kid is eating
Benefit of technique superseding
The fundamental benefit of technique abrogating is that the class can give its own particular execution to an acquired strategy without altering the parent class code.
This is useful when a class has a few kid classes, so if a kid class needs to utilize the parent class technique, it can utilize it and different classes that need to have diverse execution can utilize superseding highlight to make changes without contacting the parent class code.
Strategy Overriding and Dynamic Method Dispatch
Strategy Overriding is an illustration of runtime polymorphism. At the point when a parent class reference focuses to the kid class object then the call to the abrogated technique is resolved at runtime, on the grounds that during strategy call which method(parent class or youngster class) is to be executed is controlled by the kind of item. This interaction wherein call to the superseded technique is settled at runtime is known as unique strategy dispatch. Lets see a guide to get this:
class ABC{
/Overridden technique
public void disp()
{
System.out.println("disp() strategy for parent class");
}
}
class Demo expands ABC{
/Overriding strategy
public void disp(){
System.out.println("disp() strategy for Child class");
}
public void newMethod(){
System.out.println("new strategy for kid class");
}
public static void primary( String args[]) {
/* When Parent class reference alludes to the parent class object
* at that point for this situation superseded strategy (the technique for parent class)
* is called.
*/
ABC obj = new ABC();
obj.disp();
/* When parent class reference alludes to the kid class object
* at that point the superseding strategy (technique for youngster class) is called.
* This is called dynamic technique dispatch and runtime polymorphism
*/
ABC obj2 = new Demo();
obj2.disp();
}
}
Yield:
disp() strategy for parent class
disp() strategy for Child class
In the above model the call to the disp() technique utilizing second article (obj2) is runtime polymorphism (or dynamic strategy dispatch).
Note: In powerful strategy dispatch the item can call the abrogating strategies for kid class and all the non-superseded techniques for base class however it can't call the strategies which are recently proclaimed in the kid class. In the above model the item obj2 is calling the disp(). Be that as it may in the event that you attempt to call the newMethod() technique (which has been recently proclaimed in Demo class) utilizing obj2 then you would give assemblage mistake with the accompanying message:
Exemption in string "principle" java.lang.Error: Unresolved assemblage
issue: The technique xyz() is unclear for the kind ABC
Rules of strategy superseding in Java
Contention list: The contention rundown of superseding technique (strategy for kid class) should coordinate with the Overridden method(the strategy for parent class). The information sorts of the contentions and their succession ought to precisely coordinate.
Access Modifier of the abrogating technique (strategy for subclass) can't be more prohibitive than the superseded technique for parent class. For example on the off chance that the Access Modifier of parent class strategy is public, the superseding technique (kid class technique ) can't have private, ensured and default Access modifier,because these three access modifiers are more prohibitive than public.
For example This isn't permitted as kid class disp strategy is more restrictive(protected) than base class(public)
class MyBaseClass{
public void disp()
{
System.out.println("Parent class strategy");
}
}
class MyChildClass expands MyBaseClass{
secured void disp(){
System.out.println("Child class strategy");
}
public static void primary( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
Yield:
Special case in string "primary" java.lang.Error: Unresolved aggregation
issue: Cannot lessen the perceivability of the acquired strategy from MyBaseClass
Anyway this is totally legitimate situation as open is less prohibitive than secured. Same access modifier is additionally a legitimate one.
class MyBaseClass{
ensured void disp()
{
System.out.println("Parent class technique");
}
}
class MyChildClass broadens MyBaseClass{
public void disp(){
System.out.println("Child class technique");
}
public static void primary( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
Output:
Kid class strategy
private, static and last strategies can't be superseded as they are nearby to the class. Anyway static strategies can be re-pronounced in the sub class, for this situation the sub-class strategy would act contrastingly and will have nothing to do with a similar static technique for parent class.
Superseding strategy (technique for youngster class) can toss unchecked special cases, whether or not the abrogated method(method of parent class) tosses any exemption or not. Anyway the superseding technique ought not toss checked special cases that are new or more extensive than the ones announced by the abrogated strategy. We will talk about this in detail with model in the impending instructional exercise.
Restricting of abrogated strategies occur at runtime which is known as unique restricting.
Assuming a class is broadening a theoretical class or executing an interface, it needs to abrogate every one of the theoretical strategies except if the actual class is a theoretical class.
Super catchphrase in Method Overriding
The super catchphrase is utilized for calling the parent class strategy/constructor. super.myMethod() calls the myMethod() technique for base class while super() calls the constructor of base class. How about we see the utilization of very in technique Overriding.
As we realize that we abrogate a technique in youngster class, at that point call to the strategy utilizing kid class object calls the superseded technique. By utilizng super we can call the superseded technique as demonstrated in the model underneath:
class ABC{
public void myMethod()
{
System.out.println("Overridden technique");
}
}
class Demo broadens ABC{
public void myMethod(){
/This will call the myMethod() of parent class
super.myMethod();
System.out.println("Overriding technique");
}
public static void fundamental( String args[]) {
Demo obj = new Demo();
obj.myMethod();
}
}
Yield:
Class ABC: mymethod()
Class Test: mymethod()
As you see utilizing super catchphrase, we can get to the overriden technique.
0 Comments:
Post a Comment