1. ARBA MINCH UNIVERSITY
INSTITUTE OF
TECHNOLOGY(AMIT)
FACULTY OF COMPUTER
&
SOFTWARE ENGINEERING
Object Oriented
Programming -OOP (Java)
SWE – Week 7 - 2021
METHOD OVERRIDING IN JAVA
What is method overriding?
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that
is already provided by one of its super-classes or parent classes. When a method in a subclass has the same
name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the
method in the subclass is said to override the method in the super-class.
What is polymorphism?
Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when
A parent class reference is used to refer to a child class object.
Types of Polymorphism
Why overriding isrun time
polymorphism?
Because the methods get resolved at run time.
In simple words, when you execute a program,
the method of which class out of many will be
called, if they have overridden the method.
Using @Override
@Override annotation is used when we override a method in sub class.
Generally novice developers overlook this feature as it is not
mandatory to use this annotation while overriding the method.
2. Why we override a method in oop?
1) Large projects heavily depend on inheritance
2) Possible to change the method calling code at all occurrences of the project. it may break the whole project.
3) The code base refers to the same method with the same methods signature in different classes of the project.
class parent {
public void show() {
System.out.println ("parent");
}}
class child extends parent {
public void show() {
System.out.println("child");
}}
Public Static void main(String []args) {
parent p=new parent();
p.show();
child c=new child();
c.show();
parent p1=new child();
p1.show();
}} Output:
parent
child
child
class Father {
public void home() {
System.out.println("Father’s home");} public
void car() {
System.out.println("Father's Car");
} }
class Son extends Father{
@Override
public void car() {
System.out.println("Son's Car");
} }
public class TestOverriding {
public static void main(String[] args) {
Son s = new Son();
s.home(); s.car(); Out PUT:
Father’s home
Son’s Car
Group members
1) Eyob Mesfin NSR/957/12
2) Biniyam Belihu NSR/568/12
3) Ananiya Legese NSR/ 298/12