Classes Objects Relationships

What is a Class?

A class is a blueprint or template for creating objects. It defines properties (fields) and behaviors (methods) that objects will have.

class Car {
    // Fields (properties)
    String color;
    String model;
    int year;

    // Methods (behaviors)
    void start() {
        System.out.println("Car started");
    }

    void stop() {
        System.out.println("Car stopped");
    }
}

What is an Object?

An object is an instance of a class. It is a real-world entity created from the class blueprint.

// Creating objects
Car myCar = new Car();
Car yourCar = new Car();

// Setting properties
myCar.color = "Red";
myCar.model = "Tesla";

// Calling methods
myCar.start();

Class Structure

class ClassName {
    // 1. Fields (variables)
    dataType fieldName;

    // 2. Constructor
    ClassName() {
        // initialization
    }

    // 3. Methods
    returnType methodName() {
        // code
    }
}

Complete Example

class Student {
    // Fields
    String name;
    int rollNo;
    double marks;

    // Method to display details
    void display() {
        System.out.println("Name: " + name);
        System.out.println("Roll No: " + rollNo);
        System.out.println("Marks: " + marks);
    }

    // Method to check pass/fail
    void checkResult() {
        if (marks >= 50) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Create object
        Student s1 = new Student();

        // Set values
        s1.name = "John";
        s1.rollNo = 101;
        s1.marks = 85.5;

        // Call methods
        s1.display();
        s1.checkResult();
    }
}

Relationships Between Classes

1. Association (“uses-a”)

One class uses another class.

class Teacher {
    String name;

    void teach(Student student) {
        System.out.println(name + " is teaching " + student.name);
    }
}

class Student {
    String name;
}

public class Main {
    public static void main(String[] args) {
        Teacher t = new Teacher();
        t.name = "Mr. Smith";

        Student s = new Student();
        s.name = "John";

        t.teach(s);  // Teacher uses Student
    }
}

2. Aggregation (“has-a”, weak)

One class contains another class. Objects can exist independently.

class Address {
    String city;
    String state;
}

class Student {
    String name;
    Address address;  // Student has an Address

    void display() {
        System.out.println("Name: " + name);
        System.out.println("City: " + address.city);
    }
}

public class Main {
    public static void main(String[] args) {
        Address addr = new Address();
        addr.city = "Mumbai";
        addr.state = "Maharashtra";

        Student s = new Student();
        s.name = "John";
        s.address = addr;

        s.display();
    }
}

3. Composition (“has-a”, strong)

One class owns another class. If parent is destroyed, child is also destroyed.

class Engine {
    String type;

    void start() {
        System.out.println("Engine started");
    }
}

class Car {
    String model;
    Engine engine;  // Car has an Engine

    Car(String model) {
        this.model = model;
        this.engine = new Engine();  // Engine created with Car
    }

    void start() {
        System.out.println(model + " starting...");
        engine.start();
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("Tesla");
        car.start();
    }
}

4. Inheritance (“is-a”)

One class inherits from another class.

class Animal {
    void eat() {
        System.out.println("Eating...");
    }
}

class Dog extends Animal {  // Dog is an Animal
    void bark() {
        System.out.println("Barking...");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();   // Inherited from Animal
        dog.bark();  // Own method
    }
}

Class vs Object

ClassObject
Blueprint/TemplateInstance of class
Defined onceCan create many
No memory allocatedMemory allocated
class Car { }Car myCar = new Car();

Key Points

  1. Class = Template, Object = Instance
  2. Create object: ClassName obj = new ClassName();
  3. Access members: obj.field or obj.method()
  4. Multiple objects can be created from one class
  5. Each object has its own copy of fields

Exam Tips

Remember:

  • Class: Blueprint for objects
  • Object: Instance created using new
  • Association: Uses-a relationship
  • Aggregation: Has-a (weak)
  • Composition: Has-a (strong)
  • Inheritance: Is-a relationship
  • Access: object.field, object.method()

Common Questions:

  • What is class and object?
  • Difference between class and object?
  • Types of relationships?
  • What is has-a relationship?
  • What is is-a relationship?