Overriding in Java in context of design.
While you are going for an Interview for a Lead or Developer role. The interviewer will definitely ask you some questions on Overriding and try to puzzle you.
In this tutorial, I try to cover all topics of Overriding so you feel comfortable and easily answer whatever questions they ask
Definition of Overriding:
In Java, Overriding is a technique by which parent class delegates responsibility to Child. A child can define child specific behavior which will be executed at run time.
From the above definition, it is clear that to do Overriding Parent class and child class is must need, without Child class, we can't achieve overriding, unlike overloading.
Please remember , The interviewer may ask you-- can it be possible to do overriding without child class?
As I said child specific behavior executed at run-time what I mean by this is the JVM will decide which Subclass/Object's method it has to call, so we call it late binding or Dynamic method dispatch.Because runtime JVM decides what to do not at compile time.
Question: What is late binding/ Dynamic method dispatch in Java
If you don't get my point don't worry follow example will clear the concept.
Before dig down to Overriding details let clear one small thing.
The big power of OOP programming is it supports Polymorphic assignment where a parent class reference can point a Subclass object. It will be heavily used in Design pattern or I can say the Whole Design pattern is based on this small concept.
Q: What do you mean by polymorphic assignment and Why it is important?
Let's understand why this is important.
say Foo is Parent class and Bar is child class so we can do that,
Foo foo = new Bar();//Polymorphic assignment.
By parent reference, we can point child Object. It is logical as child inherited all properties of the parent so whatever in parent must have in the child unless properties are private in a parent.
What is the benefits of polymorphic assignment?
let see the small code
Class Power
{
private Foo f;
public setFoo(Foo f)
{
this.f = f;
}
public void show()
{
f.print();
}
}
Class Foo has a print() method which is overridden in Bar and Zoo two subclasses of Foo
overridden in Bar
public void print()
{
Sysout(“Bar”);
}
overridden in Zoo
public void print()
{
sysout(“Zoo”);
}
so by the setFoo method in Power class, I can easily set Bar or Zoo instance at run time based on some business logic .so when it calls show method based on the subclass it prints Bar or Z
So I can easily change the behavior of the Power class on the fly based on strategy. It is the crux of Strategy design pattern.
But remember, As SetFoo method takes parent reference what ever method are in parent class you can call those, all Association condition applied here. So if any new method added in subclass that is not visible by parent reference.
Qustion : expect a coding snippet here where interviewr add a method in child and try to call it by polymorphic refrence.
To make it visible you need to do down-casting.
Tip: Always use @Override annotation if you are using Java 5 or more to make sure if maintains all rules of overriding.
To do a perfect overriding we have to follow some rules.
The method which will be overridden must have same data signature also return type will be same
but onwards java 1.5 return type can be subclasses of parent type we say it Co-variant return.
Class Power
{
private Foo f;
public setFoo(Foo f)
{
this.f = f;
}
public void show()
{
f.print();
}
}
Class Foo has a print() method which is overridden in Bar and Zoo two subclasses of Foo
overridden in Bar
public void print()
{
Sysout(“Bar”);
}
overridden in Zoo
public void print()
{
sysout(“Zoo”);
}
so by the setFoo method in Power class, I can easily set Bar or Zoo instance at run time based on some business logic .so when it calls show method based on the subclass it prints Bar or Z
So I can easily change the behavior of the Power class on the fly based on strategy. It is the crux of Strategy design pattern.
But remember, As SetFoo method takes parent reference what ever method are in parent class you can call those, all Association condition applied here. So if any new method added in subclass that is not visible by parent reference.
Qustion : expect a coding snippet here where interviewr add a method in child and try to call it by polymorphic refrence.
To make it visible you need to do down-casting.
Tip: Always use @Override annotation if you are using Java 5 or more to make sure if maintains all rules of overriding.
To do a perfect overriding we have to follow some rules.
The method which will be overridden must have same data signature also return type will be same
but onwards java 1.5 return type can be subclasses of parent type we say it Co-variant return.
Q: What is covarient return?
Please follow the following contracts and burn it into the head as in Interview or in Exam you can found various types of combination and you have to find the perfect overridden method.
Please follow the following contracts and burn it into the head as in Interview or in Exam you can found various types of combination and you have to find the perfect overridden method.
1. The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method .
2. The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass. we say it covariant return
3. The access level can't be more restrictive than the overridden methods.
The access level CAN be less restrictive than that of the overridden method.
Instance methods can be overridden only if they are inherited by the subclass.
A subclass within the same package as the instance's superclass can override
any superclass method that is not marked private or final. A subclass in a
different package can override only those non-final methods marked public
or protected (since protected methods are inherited by the subclass).
4. The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.
5. The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
the method that declares a FileNotFoundException cannot be overridden by a
the method that declares an SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException.
6. The overriding method can throw narrower or fewer exceptions.
don't match, you can end up with an overloaded method .
2. The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass. we say it covariant return
3. The access level can't be more restrictive than the overridden methods.
The access level CAN be less restrictive than that of the overridden method.
Instance methods can be overridden only if they are inherited by the subclass.
A subclass within the same package as the instance's superclass can override
any superclass method that is not marked private or final. A subclass in a
different package can override only those non-final methods marked public
or protected (since protected methods are inherited by the subclass).
4. The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.
5. The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
the method that declares a FileNotFoundException cannot be overridden by a
the method that declares an SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException.
6. The overriding method can throw narrower or fewer exceptions.