0% found this document useful (0 votes)
37 views7 pages

Java 2

The document discusses method overriding in object-oriented programming. It defines method overriding as a subclass providing a specific implementation of a method that is already defined in its parent superclass. Method overriding allows runtime polymorphism. The document outlines the rules for method overriding and provides an example comparing overriding in the Bike class which extends the Vehicle class. It concludes that method overriding is integral to presenting Java's object-oriented capabilities and improves readability and reuse through polymorphism.

Uploaded by

Kartik Tanwar
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)
37 views7 pages

Java 2

The document discusses method overriding in object-oriented programming. It defines method overriding as a subclass providing a specific implementation of a method that is already defined in its parent superclass. Method overriding allows runtime polymorphism. The document outlines the rules for method overriding and provides an example comparing overriding in the Bike class which extends the Vehicle class. It concludes that method overriding is integral to presenting Java's object-oriented capabilities and improves readability and reuse through polymorphism.

Uploaded by

Kartik Tanwar
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/ 7

INDEX

CONTENT PAGE NO.

Objectives 3

Method Overriding 4

Rules 5

Example 7

Conclusion 8

Bibliography 8

pg. 2
TITLE

Method Overriding

OBJECTIVES

To develop further understanding of the theories and concepts covered in the


course.

To develop a practice of learning new aspects of the subject & develop habit
of research to the subject.

pg. 3
METHOD OVERRIDING

In any object-oriented programming language, 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
superclass parent classes. When a method in a subclass has the same name, same parameters or
signature and same return as a method in its super-class, then the method in the subclass is said to
override the method in the super-class.

Method Overriding is one of the way by which java achieve Run Time Polymorphism

How and When to Override

In some cases, method overriding is mandatory - if you implement an interface, for example, you
must override its methods. Yet, in others, it is usually up to the programmer to decide whether
they will override some given methods or not.

Take a scenario where one extends a non-abstract class, for instance. The programmer is free (to
some extent) to choose methods to override from the superclass.

pg. 4
RULES FOR METHOD OVERRIDING

a) Overriding and Access – Modifiers.


b) Final methods cannot be overridden.
c) Static methods cannot be overridden.
d) Private methods cannot be overridden.
e) The overriding method must have same return type.
f) Invoking overridden method must have same return type.
g) Constructor & Overriding
h) Overriding & Exception Handling
i) Abstract Method & Overriding
j) Synchronized Method & Overriding

METHOD OVERLOADING & METHOD OVERRIDING

Another distinguishing difference between the two is how compilers treat them. Compilers choose
between overloaded methods when compiling and resolve overridden methods at runtime. That is
why overloading is also known as compile-time polymorphism. And we may also refer to
overriding as runtime polymorphism.

Still, overriding is a better than overloading when it comes to realizing polymorphism. With
overloading, you risk creating hard-to-read APIs. In contrast, overriding forces one to adopt class
hierarchies. These are especially useful because they force programmers to design for OOP.

pg. 5
In summary, overloading and overriding differ in these ways:

Method Overloading Method Overriding

Writing two or more methods with the Writing two or more methods with
same name but different signatures is same name but different signatures is
overloading. Overriding.

It is done in the same class. It is done in super & subclasses.

Method return types can be same or Method return types should always be
different. same.

It is done when the programmer wants It is done when the programmer wants
to extend the available features. to provide different implementation for
the same feature.

Method overloading is code refinement. Method overriding is code replacement.

pg. 6
METHOD OVERRIDING EXAMPLE

Class Vehicle {
Void run(){System.out.println(“Vehicle is Running”);}
}

Class Bike extends Vehicle {


Void run(){System.out.println(“Bike is Running”):}
Public static void main (String args[]){
Bike obj = new Bike();
Obj.run();
}
}

OUTPUT :-

Bike is Running

pg. 8
CONCLUSION

Method overriding is integral to the presentation of Java's OOP muscle. It cements class hierarchies
by allowing subclasses to possess and even extend the capabilities of their super classes.

Still, most programmers encounter the feature only when implementing interfaces or extending
abstract classes. Non-mandatory overriding can improve a class' readability and consequent
usability.

Finally, because method overriding combines inheritance and polymorphism, it makes an excellent
tool for removing common code smells. Issues, such as, excessive conditionals and utility classes
could become less prevalent through wise use of overriding.

BIBLIOGRAPHY

Books : Core Java : An Integrated Approach


Website : www.GeeksforGeeks.org
www.Javatpoint.com

pg. 9

You might also like