Chapter 6 :- 1st Look at Classes

6.1 Introduction to Object-Oriented Programming (OOP)

  • Object-Oriented Programming: A paradigm where data and behaviors are encapsulated into objects.
  • Key Concepts:
    1. Class: A blueprint for creating objects.
    2. Object: An instance of a class.
    3. Encapsulation: Bundling data (fields) and methods that operate on the data into a single unit (class).
    4. Inheritance: Mechanism to create a new class from an existing one.
    5. Polymorphism: Ability to take many forms (e.g., method overloading/overriding).

6.2 Defining Classes

Class Syntax

public class ClassName {
    // Fields (variables)
    // Methods
}
Example
public class Person {
    String name;
    int age;
}
Creating Objects
Person person1 = new Person();

6.3 Instance Fields and Methods

Instance Fields

  • Variables defined in a class but outside methods.
  • Access: Through objects using the dot operator.
  • name = “John”;
person1.age = 25;

Instance Methods

  • Functions defined in a class to operate on its fields.
  • Example
  • public class Person {
  • String name;
  • int age;
  • public void displayInfo() {
  • out.println(“Name: ” + name + “, Age: ” + age);
  • }
}
person1.displayInfo();

6.4 Constructors

  • Special methods invoked at the time of object creation.
  • Purpose: Initialize fields.
  • Syntax
  • public ClassName(parameters) {
  • // Initialize fields
}
Example
public class Person {
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
Person person2 = new Person(“Alice”, 30);

6.5 Overloading Constructors

  • Multiple constructors with different parameter lists.
  • Example
  • public class Person {
  • String name;
  • int age;
  •  
  • public Person() {
  • name = “Unknown”;
  • age = 0;
  • }
  • public Person(String name, int age) {
  • name = name;
  • age = age;
  • }
}

6.6 The this Keyword

  • Refers to the current instance of the class.
  • Common Uses:
    1. Distinguishing between instance fields and parameters.
    2. Calling other constructors.
Example
public class Person {
    String name;
 
    public Person(String name) {
        this.name = name;
    }
}

6.7 Access Modifiers

  1. Public: Accessible from any class.
  2. Private: Accessible only within the class.
  3. Protected: Accessible within the package and subclasses.
  4. Default: Accessible only within the package.

6.8 Encapsulation

  • Definition: Restricting direct access to fields; using methods (getters/setters) for controlled access.
  • Benefits:
    • Protects data integrity.
    • Enhances maintainability.
Example
public class Person {
    private String name;
    private int age;
    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;
        }
    }
}

6.9 Static Members

Static Fields and Methods

  • Belong to the class rather than any object.
  • Access using the class name.
Example
public class MathUtils {
    public static double pi = 3.14159;
 
    public static double square(double x) {
        return x * x;
    }
}
System.out.println(MathUtils.pi);
System.out.println(MathUtils.square(4));

6.10 Final Keyword

  • Final Field: Cannot be modified after initialization.
public final int MAX_VALUE = 100;
  • Final Method: Cannot be overridden.
public final void display() {}
  • Final Class: Cannot be subclassed.
public final class Constants {}

6.11 Passing Objects as Arguments

  • Objects are passed by reference, but the reference itself is passed by value.
  • Example
  • public void modifyPerson(Person p) {
  • name = “Modified”;
}

6.12 Arrays of Objects

  • Use an array to store multiple objects.
  • Example
  • Person[] people = new Person[3];
  • people[0] = new Person(“John”, 25);
  • people[1] = new Person(“Alice”, 30);
people[2] = new Person(“Bob”, 20);

6.13 Object Collaboration

  • Objects can interact with each other by calling methods.
  • Example
  • public class Engine {
  • public void start() {
  • out.println(“Engine started”);
  • }
  • }
  •  
  • public class Car {
  • Engine engine = new Engine();
  • public void drive() {
  • start();
  • out.println(“Car is moving”);
  • }
}

Summary

  • Classes and objects are the foundation of OOP.
  • Encapsulation ensures data security.
  • Use constructors for initialization and access modifiers for control.
  • Static members belong to the class, while instance members belong to objects.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.