0% found this document useful (0 votes)
6 views3 pages

J Method Overriding 07 - 03 - 23

Method overriding in Java occurs when a subclass has a method with the same name and parameters as a method in its parent class, enabling runtime polymorphism. The document provides examples demonstrating method overriding with classes like Parent and Child, as well as a Bank class with different interest rates for different banks. It also explains runtime polymorphism through a Shape class and its subclasses, illustrating how method calls are resolved at runtime based on the object type.
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)
6 views3 pages

J Method Overriding 07 - 03 - 23

Method overriding in Java occurs when a subclass has a method with the same name and parameters as a method in its parent class, enabling runtime polymorphism. The document provides examples demonstrating method overriding with classes like Parent and Child, as well as a Bank class with different interest rates for different banks. It also explains runtime polymorphism through a Shape class and its subclasses, illustrating how method calls are resolved at runtime based on the object type.
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/ 3

Method Overriding

 If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
 Method overriding is one of the way by which java achieve Run Time Polymorphism.

Rules for Java Method Overriding


1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).

class Parent {
void show()
{
System.out.println("Parent's show()");
}
}

// Inherited class
class Child extends Parent {

void show() // This method overrides show() of Parent


{
System.out.println("Child's show()");
}
}

class TestOverride{
public static void main(String[] args)
{
// If a Parent type reference refers to a Parent object, then Parent's show is called
Parent obj1 = new Parent();
obj1.show();

// If a Parent type reference refers to a Child object Child's show() is called. This is called
//RUN TIME POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}
A real example of Java Method Overriding
Consider a scenario where Bank is a class that provides functionality to get the rate of interest.
However, the rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks
could provide 8%, 7%, and 9% rate of interest.

class Bank
{
float GetROI()
{
return 4.5f;
}
}
class SBI extends Bank
{
float GetROI()
{
return 7.5f;
}
}
class ICICI extends Bank
{
float GetROI()
{
return 8.5f;
}
}
class TestBank
{
public static void main(String a[])
{
Bank bob=new Bank();
Bank bob1= new SBI();
Bank bob2=new ICICI();
float i1=bob.GetROI();
System.out.println("Bank Object "+i1);
float i2=bob1.GetROI();
System.out.println("SBI Object "+i2);
float i3=bob2.GetROI();
System.out.println("ICICI Object "+i3);
}}
Runtime Polymorphism in Java
 Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an
overridden method is resolved at runtime rather than compile-time.
 In this process, an overridden method is called through the reference variable of a superclass. The
determination of the method to be called is based on the object being referred to by the reference
variable.
class Shape{
void draw(){System.out.println("drawing...");}
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle...");}
}
class Circle extends Shape{
void draw(){System.out.println("drawing circle...");}
}
class Triangle extends Shape{
void draw(){System.out.println("drawing triangle...");}
}
class TestPolymorphism{
public static void main(String args[]){
Shape s;
s=new Rectangle();
s.draw();
s=new Circle();
s.draw();
s=new Triangle();
s.draw();
}
}

You might also like