Method Overriding in Java With Example PDF
Method Overriding in Java With Example PDF
Declaring a method in sub class which is already present in parent class is known as method
overriding. Overriding is done so that a child class can give its own implementation to a method
which is already provided by the parent class. In this case the method in parent class is called
overridden method and the method in child class is called overriding method. In this guide, we will
see what is method overriding in Java and why we use it.
The purpose of Method Overriding is clear here. Child class wants to give its own implementation so
that when it calls this method, it prints Boy is eating instead of Human is eating.
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
Output:
Boy is eating
This is helpful when a class has several child classes, so if a child class needs to use the parent class
method, it can use it and the other classes that want to have different implementation can use
overriding feature to make changes without touching the parent class code.
class ABC{
//Overridden method
public void disp()
{
System.out.println("disp() method of parent class");
}
}
class Demo extends ABC{
//Overriding method
public void disp(){
System.out.println("disp() method of Child class");
}
public void newMethod(){
System.out.println("new method of child class");
}
public static void main( String args[]) {
/* When Parent class reference refers to the parent class object
* then in this case overridden method (the method of parent class)
* is called.
*/
ABC obj = new ABC();
obj.disp();
Output:
disp() method of parent class
disp() method of Child class
In the above example the call to the disp() method using second object (obj2) is runtime
polymorphism (or dynamic method dispatch).
Note: In dynamic method dispatch the object can call the overriding methods of child class and all
the non-overridden methods of base class but it cannot call the methods which are newly declared
in the child class. In the above example the object obj2 is calling the disp(). However if you try to
call the newMethod() method (which has been newly declared in Demo class) using obj2 then you
would give compilation error with the following message:
class MyBaseClass{
public void disp()
{
System.out.println("Parent class method");
}
}
class MyChildClass extends MyBaseClass{
protected void disp(){
System.out.println("Child class method");
}
public static void main( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
Output:
However this is perfectly valid scenario as public is less restrictive than protected. Same access
modi er is also a valid one.
class MyBaseClass{
protected void disp()
{
System.out.println("Parent class method");
}
}
class MyChildClass extends MyBaseClass{
public void disp(){
System.out.println("Child class method");
}
public static void main( String args[]) {
MyChildClass obj = new MyChildClass();
obj.disp();
}
}
Output:
class ABC{
public void myMethod()
{
System.out.println("Overridden method");
}
}
class Demo extends ABC{
public void myMethod(){
//This will call the myMethod() of parent class
super.myMethod();
System.out.println("Overriding method");
}
public static void main( String args[]) {
Demo obj = new Demo();
obj.myMethod();
}
}
Output:
As you see using super keyword, we can access the overriden method.
❮ Previous Next ❯
Comments
Hi Dear,
Although i have visited may sites to learn java programming but the concept and
explanation giving by example on your side never seen anywhere else.
I need you cooperation to learn java.
Pls Help.
Thanks.
Reply
tahir says
SEPTEMBER 16, 2014 AT 10:50 AM
can we call the non overridden methods of base class in dynamic method dispatch with
the base class reference to which the child class object is assign?
Reply
Yes we can.
Reply
Owais says
DECEMBER 12, 2014 AT 3:57 PM
https://beginnersbook.com/2014/01/method-overriding-in-java-with-example/
Reply
Sachindra says
NOVEMBER 28, 2014 AT 9:46 AM
I called Newly created Method xyz() of child class,but its running perfectly..i does not
give any error as you said it will throw
Exception in thread “main” java.lang.Error: Unresolved compilation
problem: The method xyz() is unde ned for the type ABC
Reply
Dave says
JULY 31, 2015 AT 12:33 PM
Then you did something wrong, because it shouldn’t work. At compile time the
object is static bound to the base class and it will not nd a method xyz() in the
base class.
Reply
I’ve visited so many sites but this site for learning java is exceptionally well
i hope everybody can understand and learn java easily.surly i need a help from your side
is in depth about static keyword and object .how object stores memory and how method
behaves on object
Reply
Reply
Prakash says
DECEMBER 8, 2015 AT 6:27 PM
Reply
laxman says
MARCH 17, 2016 AT 11:20 AM
Reply
Roman says
NOVEMBER 29, 2015 AT 11:28 AM
Reply
naveen says
JUNE 12, 2016 AT 3:59 AM
this is an upcasting
Reply
Gayathri says
JANUARY 5, 2016 AT 3:16 PM
Reply
Learner says
APRIL 20, 2016 AT 5:37 AM
Ketan says
AUGUST 6, 2016 AT 4:52 AM
Yes we can change but, return type can be either same or sub type of the super
class method return type that is called as a covariance (introduced from java 1.5)
Reply
maryam says
MAY 10, 2016 AT 6:05 AM
Reply
natwar says
NOVEMBER 25, 2016 AT 5:57 AM
Examples illustrated are very simple and easy to understand and covers all the basic
requirements.Please keep updating your posts.
Reply
Raju says
JANUARY 13, 2017 AT 4:45 AM
Hey, lovee your work, but I would like to make a suggestion, please add a ‘next chapter’
or next botton at the end so we can continue to the next article of this post or any post,
it would be helpful
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
POST COMMENT
Java Tutorial
Java Index
Java Introduction
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
Continue statement
break statement
OOPs Concepts
OOPs Concepts
Constructor
Static keyword
Inheritance
Types of inheritance
Aggregation
Association
Super Keyword
Method overloading
Method overriding
Overloading vs
Overriding
Polymorphism
Types of
polymorphism
Interface
Abstract class vs
interface
Encapsulation
Packages
Garbage Collection
Inner classes
Static import
Static constructor
Java Interview Q
MORE ...
Java 8 Features
Java 9 Features
Java Conversion
Java String
Exception handling
Java Multithreading
Java I/O
Java Serialization
Java Regex
Java AWT
Java Swing
Java Enum
Java Annotations
Copyright © 2012 – 2020 BeginnersBook . Privacy Policy . Sitemap