J

Classes & OOP

Java syntax guide

Object-oriented programming with classes in Java

Classes & OOP

Object-oriented programming with classes in Java

Java classes & oop (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 is purely object-oriented. Classes support inheritance, polymorphism, encapsulation, and abstraction. Abstract classes and interfaces define contracts.

Common Use Cases

  • Large-scale applications
  • Android development
  • Enterprise software
  • Web applications

Related Java Syntax

Master Classes & OOP in Java

Understanding classes & oop 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 & oop effectively in your Java projects.

Key Takeaways

  • Large-scale applications
  • Android development
  • Enterprise software