Overriding in Java
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
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.
overraidding in java programming |
Method overriding is one of the way by which java
achieve Run Time Polymorphism.The version of a method that is
executed will be determined by the object that is used to invoke it. If an
object of a parent class is used to invoke the method, then the version in the
parent class will be executed, but if an object of the subclass is used to
invoke the method, then the version in the child class will be executed.
Example:
// A Simple Java program to demonstrate
// method overriding in java
// Base Class
class Parent {
void
show()
{
System.out.println("Parent's
show()");
}
}
// Inherited class
class Child extends Parent {
//
This method overrides show() of Parent
@Override
void
show()
{
System.out.println("Child's
show()");
}
}
// Driver class
class Main {
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();
}
}
Output:
Parent ‘S show()
Child ‘S show()
Rules
for method overriding:
1. Overriding and
Access-Modifiers : The access modifier for an overriding method can allow
more, but not less, access than the overridden method. For example, a protected
instance method in the super-class can be made public, but not private, in the
subclass. Doing so, will generate compile-time error.
2. Final methods
can not be overridden : If we don’t want a method to be overridden, we declare it
as final. Please see Using final with Inheritance
//
A Java program to demonstrate that
//
final methods cannot be overridden
class
Parent {
// Can't be overridden
final void show() {}
}
class
Child extends Parent {
// This would produce error
void show() {}
}
Output:
13:error:show()
in child cannot override show() in parent
Void
show() { }
Overridden
method is final
3. Static methods can not be overridden(Method Overriding vs Method Hiding) : When you defines a static method with same signature as a static method in base class, it is known as method hiding.
The
following table summarizes what happens when you define a method with the same
signature as a method in a super-class.
Example:
//
Java program to show that
//
if the static method is redefined by
//
a derived class, then it is not
//
overriding, it is hiding
class
Parent {
// Static method in base class
// which will be hidden in subclass
static void m1()
{
System.out.println("From
parent "
+
"static m1()");
}
// Non-static method which will
// be overridden in derived class
void m2()
{
System.out.println("From
parent "
+
"non-static(instance) m2()");
}
}
class
Child extends Parent {
// This method hides m1() in Parent
static void m1()
{
System.out.println("From
child static m1()");
}
// This method overrides m2() in
Parent
@Override
public void m2()
{
System.out.println("From
child "
+
"non-static(instance) m2()");
}
}
//
Driver class
class
Main {
public static void main(String[] args)
{
Parent obj1 = new Child();
// As per overriding rules
this
// should call to class Child
static
// overridden method. Since
static
// method can not be
overridden, it
// calls Parent's m1()
obj1.m1();
// Here overriding works
// and Child's m2() is called
obj1.m2();
}
}
Output:
From
parent static m1()
From
child non-static(instance) m2()
No comments:
Post a Comment