What's new

Java Pasok kanaman

G O Z A R U

Forum Expert
Elite
Joined
Apr 7, 2017
Posts
7,988
Solutions
1
Reaction
4,323
Points
2,361
pano ba to ma run gamut Java compiler
Java:
interface Polygon {
  void getArea();

  // default method
  default void getSides() {
    System.out.println("I can get sides of a polygon.");
  }
}

// implements the interface
class Rectangle implements Polygon {
  public void getArea() {
    int length = 6;
    int breadth = 5;
    int area = length * breadth;
    System.out.println("The area of the rectangle is " + area);
  }

  // overrides the getSides()
  public void getSides() {
    System.out.println("I have 4 sides.");
  }
}

// implements the interface
class Square implements Polygon {
  public void getArea() {
    int length = 5;
    int area = length * length;
    System.out.println("The area of the square is " + area);
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of Rectangle
    Rectangle r1 = new Rectangle();
    r1.getArea();
    r1.getSides();

    // create an object of Square
    Square s1 = new Square();
    s1.getArea();
    s1.getSides();
  }
}
 
Last edited:
OOP sir? ano pong Problem?
1615816504837.png
 

Attachments

Java:
public class Main
{
    public static void main(String[] args) {
        // create an object of Rectangle
        Rectangle r1 = new Rectangle();
        r1.getArea();
        r1.getSides();

        // create an object of Square
        Square s1 = new Square();
        s1.getArea();
        s1.getSides();
    }
}
        interface Polygon {
            void getArea();

            // default method
            default void getSides() {
            System.out.println("I can get sides of a polygon.");
    }
}

            // implements the interface
        class Rectangle implements Polygon {
        public void getArea() {
        int length = 6;
        int breadth = 5;
        int area = length * breadth;
            System.out.println("The area of the rectangle is " + area);
    }

        // overrides the getSides()
        public void getSides() {
            System.out.println("I have 4 sides.");
    }
}

        // implements the interface
        class Square implements Polygon {
        public void getArea() {
        int length = 5;
        int area = length * length;
            System.out.println("The area of the square is " + area);
    }
}

patry po to..
 

Similar threads

Back
Top