Question 7.6:Create a class named 'Shape' with a method to print "This is This is shape". Then create two other classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to print "This is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of 'Rectangle' having a method to print "Square is a rectangle". Now call the method of 'Shape' and 'Rectangle' class by the object of 'Square' class.
Solution:
//Main Class
package com.company;
public class Main {
public static void main(String[] args) {
Rectangle obj = new Rectangle();
Circle obj1 = new Circle();
Square obj2 = new Square();
obj2.printR();
}
}
//rectangle
package com.company;
public class Rectangle extends Circle {
public void printR(){
System.out.println("this is rectanglular shape");
}
}
//Square
package com.company;
public class Square extends Rectangle{
public void printS(){
System.out.println("Square is a rectangle");
}
}
//Circle class
package com.company;
public class Circle {
public void printC(){
System.out.println("this is circular shape");
}
}