Question 7.2: In the above example, declare the method of the parent class as private and then repeat the first two operations (You will get error in the third).
Solution:
//Main Class
package com.company;
public class Main {
public static void main(String[] args) {
parent obj = new parent();
child obj1 = new child();
obj.print();
obj1.print2();
obj1.print();
}
}
//Child Class
package com.company;
public class child extends parent {
public void print2 (){
System.out.println("this is child class");
}
}
//Parent Class
package com.company;
public class parent {
private void print(){
System.out.println("this is parent class");
}
}