Created
July 6, 2026 15:32
-
-
Save thinkphp/55916ce5abaff9e6c4e17e7b6aeb7a3b to your computer and use it in GitHub Desktop.
Abstractizare 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
| abstract class Shape { | |
| abstract double calculateArea(); //metoda abstracta, FARA implementare | |
| void display() { | |
| System.out.println("Aria formei este: " + calculateArea()); | |
| } | |
| } | |
| //subclasa 1: implementare concreta | |
| class Circle extends Shape { | |
| double radius; | |
| Circle(double radius) { | |
| this.radius = radius; | |
| } | |
| @Override | |
| double calculateArea() { | |
| return Math.PI * radius * radius; | |
| } | |
| } | |
| //subclasa 2: implementare concreata clasa Patrat | |
| class Square extends Shape { | |
| double side; | |
| Square(double side) { | |
| this.side = side; | |
| } | |
| @Override | |
| double calculateArea() { | |
| return side * side; | |
| } | |
| } | |
| public class Abstractizare { | |
| public static void main(String[] args) { | |
| Shape circle = new Circle( 4 ); | |
| Shape square = new Square( 5 ); | |
| circle.display(); | |
| square.display(); | |
| } | |
| } | |
| //Animal: Caine, Pisica, Pelican | |
| //MAsina: BMW, Audo, Dacia, Porsche | |
| 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 Square implements Shape { | |
| double side; | |
| Square(double width, double height) { | |
| this.width = width; | |
| this.height = height; | |
| } | |
| @Override | |
| public double calculateArea() { | |
| return side * side | |
| } | |
| @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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment