Write an application that illustrates method overriding.


Write an application that illustrates method overriding.

class A
{
void show()
{
System.out.println("Inside Class-A");
}
}
class B extends A
{
// Overriding method show of class A
void show()
{
System.out.println("Inside Class-B");
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();

obj.show();
//show() method of class-B will be called.

}
}
OUTPUT:-

Inside Class-B









Also demonstrate accessibility rules in inside and outside packages.

package p1;
public class Protection
{
int n = 1;
public int n_pub = 2;
}
public class Example
{
public static void main(String[] args)
{
Protection p1 = new Protection();
System.out.println("n = " + p1.n);
System.out.println("n_pub = " +p1.n_pub);
}
}
OUTPUT:-

n = 1
n_pub = 2

Comments