// A Simple Java program to demonstrate

// Overriding and Access-Modifiers

source file name:  OverrideDemo.java

class Parent {

// private methods are not overridden

void m1()

{

System.out.println("From parent m1()");

}


protected void m2()

{

System.out.println("From parent m2()");

}

}


class Child extends Parent {

// new m1() method

// unique to Child class

void m1()

{

System.out.println("From child m1()");

}

// overriding method  with more accessibility

@Override

public void m2()

{

System.out.println("From child m2()");

}

}

class OverrideDemo

 {

public static void main(String[] args)

{

Parent ob1 = new Parent();

ob1.m1();

ob1.m2();

Parent ob2 = new Child();

ob2.m2();

ob2.m2();

}

}

output:

D:\JavaPrograms>javac OverrideDemo.java

D:\JavaPrograms>java OverrideDemo
From parent m1()
From parent m2()
From child m2()
From child m2()

after using Private Access-Modifier 
D:\JavaPrograms>javac OverrideDemo.java
OverrideDemo.java:40: error: m1() has private access in Parent
                ob1.m1();
                   ^
1 error

D:\JavaPrograms>javac OverrideDemo.java

D:\JavaPrograms>java OverrideDemo
From parent m2()
From child m2()
From child m2()