J
Classes & POO
Java Syntax Guide
Programmation orientée objet avec des classes en Java
Classes & POO
Programmation orientée objet avec des classes en Java
Java classes & poo (java)
// Class definition
public class Person {
// Instance variables
private String name;
private int age;
private static int personCount = 0; // Class variable
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
personCount++;
}
// Default constructor
public Person() {
this("Unknown", 0);
}
// Copy constructor
public Person(Person other) {
this(other.name, other.age);
}
// Instance methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
}
}
public String toString() {
return name + " (" + age + " years old)";
}
// Static method
public static int getPersonCount() {
return personCount;
}
}
// Inheritance
class Employee extends Person {
private String jobTitle;
private double salary;
public Employee(String name, int age, String jobTitle, double salary) {
super(name, age); // Call parent constructor
this.jobTitle = jobTitle;
this.salary = salary;
}
// Method override
@Override
public String toString() {
return super.toString() + " - " + jobTitle + " ($" + salary + ")";
}
// Additional method
public void work() {
System.out.println(getName() + " is working as " + jobTitle);
}
}
// Abstract class
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
// Abstract method
public abstract double getArea();
// Concrete method
public String getColor() {
return color;
}
}
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// Usage
public class Main {
public static void main(String[] args) {
Person person = new Person("John", 25);
Employee employee = new Employee("Jane", 30, "Developer", 75000);
Circle circle = new Circle("Red", 5.0);
System.out.println(person);
System.out.println(employee);
System.out.println("Circle area: " + circle.getArea());
System.out.println("Total persons: " + Person.getPersonCount());
}
}
Explanation
Java est purement orienté objet. Les classes prennent en charge l'héritage, le polymorphisme, l'encapsulation et l'abstraction. Les classes abstraites et les interfaces définissent des contrats.
Common Use Cases
- Applications à grande échelle
- Développement Android
- Logiciel d'entreprise
- Applications web
Related Java Syntax
Master Classes & POO in Java
Understanding Classes & POO is fundamental to writing clean and efficient Java code. This comprehensive guide provides you with practical examples and detailed explanations to help you master this important concept.
Whether you're a beginner learning the basics or an experienced developer looking to refresh your knowledge, our examples cover real-world scenarios and best practices for using Classes & POO effectively in your Java projects.
Key Takeaways
- Applications à grande échelle
- Développement Android
- Logiciel d'entreprise