If
a subclass defines a class method (static method) with the same signature as a class
method in the super class, the method in the subclass hides the one in
the super class.
Distinction
between Hiding and Overriding
class Animal{ static void printMsg(){ System.out.println("I am the super class static method"); } void showMsg(){ System.out.println("I am the super class instance method"); } }
class Tiger extends Animal{ static void printMsg(){ System.out.println("I am the sub class static method"); } void showMsg(){ System.out.println("I am the sub class instance method"); } public static void main(String args[]){ Animal ref = new Tiger(); ref.printMsg(); ref.showMsg(); } }
Output
I
am the super class static method
I
am the sub class instance method
As
you observe the output, run time polymorphism not happened for the
static method. So the message “I am the super class static method”
printed.
The
showMsg() method of class Tiger overrides the showMsg() of super
class. Where as, the printMsg() of subclass hides the printMsg() of
super class.
Remember,A
sub class static method always hides the super class static method.
Where as sub class instance method overrides the super class instance
method.
Some
points to remember
1.
Can a sub class instance method hides the super class static method?
No,
Compiler error thrown.
Example
class Animal{ static void printMsg(){ System.out.println("I am the super class static method"); } }
class Tiger extends Animal{ void printMsg(){ System.out.println("I am the sub class static method"); } }
When
you try to compile the class Tiger, compiler throws the below error.
You will get a compile-time error if you attempt to change an
instance method in the superclass to a class method in the subclass,
and vice versa.
Tiger.java:2:
error: printMsg() in Tiger cannot override printMsg() in Animal
void
printMsg(){
^
overridden
method is static
1
error
2.
What is wrong with the below program ?
class Animal{ public static void printMsg(){ System.out.println("I am the super class static method"); } }
class Tiger extends Animal{ static void printMsg(){ System.out.println("I am the sub class static method"); } }
The
access specifier for an hiding method can allow more, but not less,
access than the hidden method. For example, a public static method in
the superclass can be made public, but not private, protected and
default.
When
you tries to compile the class Tiger, compiler throws the below
error.
Tiger.java:2:
error: printMsg() in Tiger cannot override printMsg() in Animal
static
void printMsg(){
^
attempting
to assign weaker access privileges; was public
1
error
3.
What is wrong with the below program ?
class Animal{ static void printMsg(){ System.out.println("I am the super class static method"); } }
class Tiger extends Animal{ static void printMsg()throws Exception{ System.out.println("I am the sub class static method"); } }
The
hiding method cannot throw any exceptions that are not thrown by the
overridden method.
When
you tries to compile the above program compiler throws the below
error.
Tiger.java:2:
error: printMsg() in Tiger cannot override printMsg() in Animal
static
void printMsg()throws Exception{
^
overridden
method does not throw Exception
1
error
4.
Can I specify weaker access specifier to the overriding method in the
sub class ?
No
No comments:
Post a Comment