Question 7.1: Create a class with a method that prints "This is parent class" and its subclass with another method that prints "This is child class". Now, create an object for each of the class and call
1 - method of parent class by object of parent class
2 - method of child class by object of child class
3 - method of parent class by object of child class
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 {
public void print(){
System.out.println("this is parent class");
}
}