WikiGalaxy

Personalize

Java Classes and Objects

Understanding Java Classes

A class in Java is a blueprint for creating objects. It defines properties (fields) and behaviors (methods) that the objects created from the class can have. Classes are fundamental to object-oriented programming in Java.

Creating a Simple Class

To create a class in Java, use the 'class' keyword followed by the class name. Inside the class, you can define fields and methods.


public class Car {
    String color;
    int year;

    void displayInfo() {
        System.out.println("Color: " + color + ", Year: " + year);
    }
}
    

Instantiating Objects

Objects are instances of classes. You can create an object using the 'new' keyword followed by the class constructor.


public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.year = 2020;
        myCar.displayInfo();
    }
}
    

Console Output:

Color: Red, Year: 2020

Constructor in Java

Defining Constructors

Constructors are special methods invoked when an object is created. They initialize the object's fields and are named the same as the class.


public class Car {
    String color;
    int year;

    Car(String c, int y) {
        color = c;
        year = y;
    }

    void displayInfo() {
        System.out.println("Color: " + color + ", Year: " + year);
    }
}
    

Using Constructors

When creating an object, pass the required parameters to the constructor to initialize the object.


public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Blue", 2021);
        myCar.displayInfo();
    }
}
    

Console Output:

Color: Blue, Year: 2021

Encapsulation in Java

Understanding Encapsulation

Encapsulation is a principle of wrapping data (variables) and code (methods) together as a single unit. It restricts direct access to some of an object's components, which can prevent the accidental modification of data.


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

    public String getColor() {
        return color;
    }

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

    public int getYear() {
        return year;
    }

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

Accessing Private Fields

Use getter and setter methods to access and update private fields from outside the class.


public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.setColor("Green");
        myCar.setYear(2019);
        System.out.println("Color: " + myCar.getColor() + ", Year: " + myCar.getYear());
    }
}
    

Console Output:

Color: Green, Year: 2019

Inheritance in Java

Understanding Inheritance

Inheritance allows a new class to inherit the properties and methods of an existing class. The new class is called the subclass, and the existing class is the superclass.


class Vehicle {
    protected String brand = "Ford";
    public void honk() {
        System.out.println("Beep beep!");
    }
}

class Car extends Vehicle {
    private String modelName = "Mustang";

    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.honk();
        System.out.println(myCar.brand + " " + myCar.modelName);
    }
}
    

Console Output:

Beep beep! Ford Mustang

Polymorphism in Java

Understanding Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. It is achieved through method overriding and method overloading.


class Animal {
    public void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    public void sound() {
        System.out.println("Woof");
    }
}

class Cat extends Animal {
    public void sound() {
        System.out.println("Meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myAnimal.sound();
        myDog.sound();
        myCat.sound();
    }
}
    

Console Output:

Animal makes a sound Woof Meow

Abstraction in Java

Understanding Abstraction

Abstraction is the concept of hiding the complex reality while exposing only the necessary parts. It can be achieved using abstract classes and interfaces.


abstract class Animal {
    abstract void makeSound();

    public void sleep() {
        System.out.println("Zzz");
    }
}

class Pig extends Animal {
    public void makeSound() {
        System.out.println("Oink Oink");
    }
}

public class Main {
    public static void main(String[] args) {
        Pig myPig = new Pig();
        myPig.makeSound();
        myPig.sleep();
    }
}
    

Console Output:

Oink Oink Zzz

Interfaces in Java

Understanding Interfaces

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors.


interface Animal {
    void makeSound();
}

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Bark");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();
    }
}
    

Console Output:

Bark

Method Overloading in Java

Understanding Method Overloading

Method overloading allows a class to have more than one method having the same name, if their parameter lists are different. It increases the readability of the program.


class Display {
    void show(int a) {
        System.out.println("Integer: " + a);
    }

    void show(String b) {
        System.out.println("String: " + b);
    }
}

public class Main {
    public static void main(String[] args) {
        Display obj = new Display();
        obj.show(5);
        obj.show("Hello");
    }
}
    

Console Output:

Integer: 5 String: Hello

Static Members in Java

Understanding Static Members

Static members belong to the class instead of any specific instance. They can be accessed without creating an instance of the class.


class Counter {
    static int count = 0;

    Counter() {
        count++;
        System.out.println("Counter: " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();
    }
}
    

Console Output:

Counter: 1 Counter: 2 Counter: 3

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025