Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Object Oriented Programming (OOP)

Classes and Objects

Classes:

In object-oriented programming (OOP), a class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects created from the class will have. Classes encapsulate data for the object and define the operations that can be performed on the object.

Objects:

An object is an instance of a class. When a class is instantiated, it creates an object in memory. Objects represent real-world entities and can have state (attributes) and behavior (methods). Objects interact with each other by invoking methods and accessing attributes.

Example of a Class and Object in Java:

Class Definition:

public class Car {
    // Attributes (Instance Variables)
    String brand;
    String model;
    int year;

    // Constructor (Initialization)
    public Car(String brand, String model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }

    // Method (Behavior)
    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
}

In this example, the Car class has three attributes (brand, model, and year) and a constructor that initializes these attributes. It also has a method displayInfo() that prints the car's information.

Object Creation and Usage:

public class Main {
    public static void main(String[] args) {
        // Creating Objects of Car Class
        Car car1 = new Car("Toyota", "Corolla", 2022);
        Car car2 = new Car("Honda", "Civic", 2021);

        // Invoking Methods on Objects
        car1.displayInfo(); // Output: Brand: Toyota, Model: Corolla, Year: 2022
        car2.displayInfo(); // Output: Brand: Honda, Model: Civic, Year: 2021
    }
}

Explanation:

  1. Class Definition (Car class):

    • The Car class defines the attributes (brand, model, year) and behavior (displayInfo() method) of a car.

  2. Object Creation (car1 and car2 objects):

    • Car car1 = new Car("Toyota", "Corolla", 2022); creates a new Car object with the specified attributes.

    • Car car2 = new Car("Honda", "Civic", 2021); creates another Car object.

  3. Object Usage (displayInfo() method):

    • car1.displayInfo(); invokes the displayInfo() method on the car1 object, printing its information.

    • car2.displayInfo(); does the same for the car2 object.

When you run the Main class, it creates two Car objects and displays their information using the displayInfo() method. This demonstrates the fundamental concepts of classes and objects in Java's object-oriented programming paradigm. Each object encapsulates its own state and behavior, providing a modular and organized way to structure code.

Constructors and Destructors:

  • Constructors: Constructors are special methods used to initialize objects. They have the same name as the class and are invoked when an object is created. Constructors can be used to set initial values for object attributes.

    public class Car {
        String brand;
    
        // Constructor
        public Car(String brand) {
            this.brand = brand;
        }
    }
  • Destructors: Java doesn't have explicit destructors. Objects are automatically garbage collected when they are no longer referenced, freeing the memory they occupy. You don't need to explicitly destroy objects; Java's garbage collector handles memory management.

Instance and Class Variables:

  • Instance Variables: Also known as object variables, these belong to instances of a class. Each object has its own copy of instance variables. They are declared inside a class but outside any method, constructor, or block.

    class Car {
        String brand;  // Instance variable
    }
  • Class Variables (Static Variables): These variables are shared among all instances of a class. They are declared with the static keyword. Class variables are initialized only once, at the start of the execution, and a single copy exists for the entire class.

    class Car {
        static int numberOfCars;  // Class variable
    }

In this example, brand is an instance variable unique to each Car object, while numberOfCars is a class variable shared by all instances of the Car class.

Understanding classes, objects, constructors, and variables is fundamental to Java programming. Classes define the structure, and objects represent the real entities in your applications. Constructors initialize objects, and variables define the state and behavior of objects, making them crucial concepts in object-oriented programming.

PreviousNaming ConventionNextInheritance and Polymorphism

Last updated 1 year ago