Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

In Java, classes are the fundamental building blocks for creating objects and defining their behaviors. A class is a blueprint that defines the attributes (fields) and behaviors (methods) that the objects created from the class can have. Here is a detailed explanation of how to define classes in Java:

Basic Structure of a Class

A class in Java is defined using the class keyword, followed by the class name and a pair of curly braces containing the class body. Here’s a basic example:

				
					public class Car {
    // Fields (or instance variables)
    String color;
    String model;
    int year;

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

    // Methods
    public void startEngine() {
        System.out.println("The engine is starting.");
    }

    public void stopEngine() {
        System.out.println("The engine is stopping.");
    }
}

				
			

Fields

Fields (also called instance variables) hold the data or state of an object. They are defined within the class but outside any method or constructor. Fields can have various access modifiers such as private, protected, public, or package-private (default).

				
					private String color;
public String model;
int year;  // package-private by default

				
			

Constructors

Constructors are special methods that are called when an object is instantiated. They initialize the object’s state. A constructor has the same name as the class and does not have a return type.

				
					public Car(String color, String model, int year) {
    this.color = color;
    this.model = model;
    this.year = year;
}

				
			

Methods

Methods define the behavior of the objects created from the class. Methods can perform operations, modify the object’s state, or return values.

				
					public void startEngine() {
    System.out.println("The engine is starting.");
}

public void stopEngine() {
    System.out.println("The engine is stopping.");
}

				
			

Access Modifiers

Access modifiers control the visibility of classes, fields, methods, and constructors. The most common access modifiers are:

  • public: The member is accessible from any other class.
  • private: The member is accessible only within its own class.
  • protected: The member is accessible within its own package and by subclasses.
  • Package-private (default): The member is accessible only within its own package.

Encapsulation

Encapsulation is a principle where the internal state of an object is hidden from the outside world and can only be accessed through public methods. This is typically achieved by making fields private and providing public getter and setter methods.

				
					public class Car {
    private String color;
    private String model;
    private int year;

    public Car(String color, String model, int year) {
        this.color = color;
        this.model = model;
        this.year = year;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void startEngine() {
        System.out.println("The engine is starting.");
    }

    public void stopEngine() {
        System.out.println("The engine is stopping.");
    }
}

				
			

Inheritance

Inheritance is a mechanism where one class (subclass) inherits the fields and methods of another class (superclass). This promotes code reuse.

				
					public class ElectricCar extends Car {
    private int batteryLife;

    public ElectricCar(String color, String model, int year, int batteryLife) {
        super(color, model, year);  // Call the constructor of the superclass
        this.batteryLife = batteryLife;
    }

    public int getBatteryLife() {
        return batteryLife;
    }

    public void setBatteryLife(int batteryLife) {
        this.batteryLife = batteryLife;
    }

    public void chargeBattery() {
        System.out.println("The battery is charging.");
    }
}

				
			

Polymorphism

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. This can be achieved through method overriding and interfaces.

				
					public class Car {
    public void startEngine() {
        System.out.println("The car engine is starting.");
    }
}

public class ElectricCar extends Car {
    @Override
    public void startEngine() {
        System.out.println("The electric car engine is starting silently.");
    }
}

public class TestCar {
    public static void main(String[] args) {
        Car myCar = new ElectricCar();
        myCar.startEngine();  // Output: The electric car engine is starting silently.
    }
}

				
			

Abstract Classes and Interfaces

Abstract classes and interfaces are used to define methods that must be implemented by subclasses or implementing classes.

				
					public abstract class Vehicle {
    public abstract void startEngine();  // Abstract method

    public void stopEngine() {
        System.out.println("The engine is stopping.");
    }
}

public interface Drivable {
    void drive();  // Interface method (implicitly public and abstract)
}

public class Car extends Vehicle implements Drivable {
    @Override
    public void startEngine() {
        System.out.println("The car engine is starting.");
    }

    @Override
    public void drive() {
        System.out.println("The car is driving.");
    }
}

				
			

Summary

Defining classes in Java involves specifying the class name, fields, constructors, and methods. Access modifiers control the visibility of these members, and principles like encapsulation, inheritance, and polymorphism help in creating robust and reusable code. Abstract classes and interfaces provide a way to enforce certain behaviors across different classes.

Scroll to Top