Created
July 6, 2026 15:32
-
-
Save thinkphp/99cf9cbd1f16d3c31829d9fee5b6f66c to your computer and use it in GitHub Desktop.
Interfata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Shape { | |
| double calculateArea(); //public | |
| double calculatePerimeter(); | |
| } | |
| class Circle implements Shape { | |
| double radius; | |
| Circle(double radius) { | |
| this.radius = radius; | |
| } | |
| @Override | |
| public double calculateArea() { | |
| return Math.PI * radius * radius; | |
| } | |
| @Override | |
| public double calculatePerimeter() { | |
| return 2 * Math.PI * radius; | |
| } | |
| } | |
| class Rectangle implements Shape { | |
| double width, height; | |
| Rectangle(double width, double height) { | |
| this.width = width; | |
| this.height = height; | |
| } | |
| @Override | |
| public double calculateArea() { | |
| return width * height; | |
| } | |
| @Override | |
| public double calculatePerimeter() { | |
| return 2 * (width + height); | |
| } | |
| } | |
| //O interfata este un CONTRACT care spune ce metode trebuie sa aiba o clasa, fara sa spuna cum sunt implementate. | |
| //O forma pura de abstractizare | |
| public class Interfata { | |
| public static void main(String[] args) { | |
| Shape c = new Circle(3); | |
| Shape d = new Rectangle(4,5); | |
| System.out.println("Cerc -> Arie: " + c.calculateArea() + ", Perimetru: " + c.calculatePerimeter()); | |
| System.out.println("Dreptunghi -> Arie: " + d.calculateArea() + ", Perimetru: " + d.calculatePerimeter()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment