Any time you have a class that inherits a method from a superclass, you have the
opportunity to override the method (unless, as you learned earlier, the method is
marked final).
The key benefit of overriding is the ability to define behavior that's
specific to a particular subclass type.
The sub class acess specifier must be greater than super class i.e.
if super class has default access then sub class must have public/default access.

See the code:
class Animal{
void talk()("animals can not speak English");
void legs(){System.out.println("animal has legs");}
class Dog extends Animal{
void legs(){System.out.println("Dog has four legs");}
void bark(){System.out.println("dog can bark");}
super.legs();//invokes the legs() of Animal class
class Crow extends Animal{
void legs(){System.out.println(crow has two legs);}
class Demo{
Animal d=new Dog();//animal reference but dog object
Animal c=new Crow();

In the above program object reference 'd' can access the methods
legs()//This override the legs() of Animal So prints "dog has four legs"
But the reference d can not invoke the method bark() because it is not in animal class.

■ The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method you didn't intend.
■ The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass.
■ The access level can't be more restrictive than the overridden method's.
■ 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).
■ The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.
■ The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
method that declares a FileNotFoundException cannot be overridden by a
method that declares a SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException.
■ You cannot override a method marked final.
■ You cannot override a method marked static.

you're allowed to change the return type in the
overriding method as long as the new return type is a subtype of the declared return
type of the overridden (superclass) method.
Let's look at a covariant return in action:
class Alpha {
Alpha doStuff(char c) {
return new Alpha();
class Beta extends Alpha {
Beta doStuff(char c) { // legal override in Java 1.5
return new Beta();

As of Java 5, this code will compile. If you were to attempt to compile this code
with a 1.4 this is compilation error.

No comments:

Post a Comment