Friday, 15 April 2016

Method overriding Example in easy way

Method overriding Example in easy way


Problem without method overriding.
Here i am unable to provide specific implementation for subclass (Test).


class A{
void run(){
System.out.println("hi running in superclass");
}
}

public class Test extends A{

public static void main(String[] args) {

Test b= new Test();
b.run();
}

}

Output:
hi running in superclass
--------------------------------------------------------------------------------------------------------

By using method overriding i give specific implementation for subclass(Test).

class A{
void run(){
System.out.println("hi running in superclass");
}
}

public class Test extends A{
void run(){
System.out.println("hi running in Test");
}

public static void main(String[] args) {

Test b= new Test();
b.run();
}

}

Output:
hi running in Test

No comments:

Post a Comment