Advertisement
vvccs

E6_Shape/Rectangle/Circle

Nov 8th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. class Shape {
  2.     public void printShape() {
  3.         System.out.println("This is shape");
  4.     }
  5. }
  6.  
  7. class Rectangle extends Shape {
  8.     @Override
  9.     public void printShape() {
  10.         System.out.println("This is rectangular shape");
  11.     }
  12. }
  13.  
  14. class Circle extends Shape {
  15.     @Override
  16.     public void printShape() {
  17.         System.out.println("This is circular shape");
  18.     }
  19. }
  20.  
  21. class Square extends Rectangle {
  22.     public void printSquare() {
  23.         System.out.println("Square is a rectangle");
  24.     }
  25. }
  26.  
  27. public class Main {
  28.     public static void main(String[] args) {
  29.         Square squareObj = new Square();
  30.  
  31.         // Call the method of the Shape class through the Square object
  32.         squareObj.printShape(); // This will print "This is rectangular shape"
  33.  
  34.         // Call the method of the Rectangle class through the Square object
  35.         squareObj.printSquare(); // This will print "Square is a rectangle"
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement