WikiGalaxy

Personalize

Java OOP: Encapsulation

Encapsulation:

Encapsulation involves bundling data with methods that operate on that data. It restricts direct access to some components of an object.

Private access modifiers:

Using private access modifiers is a common way to achieve encapsulation in Java.


public class Student {
    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) {
        this.age = age;
    }
}
    

Encapsulation:

The above example demonstrates encapsulation by using private fields and public getter and setter methods.

Console Output:

No direct output, encapsulation is about structure.

Java OOP: Inheritance

Inheritance:

Inheritance allows a new class to inherit properties and methods from an existing class.

extends:

The keyword extends is used to inherit from a superclass.


class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

public class TestInheritance {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}
    

In the example, Dog inherits the eat method from Animal and adds its own method bark.

Console Output:

This animal eats food. The dog barks.

Java OOP: Polymorphism

Polymorphism:

Polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name.

Method Overloading and Method Overriding:

Method Overloading and Method Overriding are two types of polymorphism.


class Shape {
    void draw() {
        System.out.println("Drawing a shape.");
    }
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle.");
    }
}

public class TestPolymorphism {
    public static void main(String[] args) {
        Shape shape = new Circle();
        shape.draw();
    }
}
    

In the example, the draw method in Circle overrides the draw method in Shape. At runtime, the appropriate method is invoked.

Console Output:

Drawing a circle.

Java OOP: Abstraction

Abstraction:

Abstraction is the concept of hiding the complex reality while exposing only the necessary parts.

Abstract classes:

Abstract classes and interfaces are used to achieve abstraction in Java.


abstract class Vehicle {
    abstract void start();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car is starting.");
    }
}

public class TestAbstraction {
    public static void main(String[] args) {
        Vehicle myCar = new Car();
        myCar.start();
    }
}
    

In the example, Vehicle is an abstract class with an abstract method start, which is implemented in Car.

Console Output:

Car is starting.

Java OOP: Interface

Interface:

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:

Interfaces specify what a class must do and not how. It is the blueprint of the class.


interface Animal {
    void sound();
}

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

public class TestInterface {
    public static void main(String[] args) {
        Animal myCat = new Cat();
        myCat.sound();
    }
}
    

In the example, the Cat class implements the Animal interface and provides its own implementation of the sound method.

Console Output:

Meow

Java OOP: Constructor

Constructor:

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created.

Constructors:

Constructors have the same name as the class and do not have a return type.


class Book {
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    void display() {
        System.out.println("Title: " + title + ", Author: " + author);
    }
}

public class TestConstructor {
    public static void main(String[] args) {
        Book book = new Book("1984", "George Orwell");
        book.display();
    }
}
    

Point Heading:

The example shows a constructor in the Book class which initializes the title and author fields.

Console Output:

Title: 1984, Author: George Orwell

Java OOP: Overloading

Method overloading:

Method overloading is a feature that 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 MathOperations {
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
}

public class TestOverloading {
    public static void main(String[] args) {
        MathOperations math = new MathOperations();
        System.out.println(math.add(5, 3));
        System.out.println(math.add(5.5, 3.3));
    }
}
    

The example shows method overloading with two add methods having different parameters.

Console Output:

8 8.8

Java OOP: Overriding

Method overriding:

Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already provided by its superclass.

The method in the subclass should have the same name and parameters as in the superclass.


class Parent {
    void show() {
        System.out.println("Parent's show()");
    }
}

class Child extends Parent {
    void show() {
        System.out.println("Child's show()");
    }
}

public class TestOverriding {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
    

In the example, the show method in Child overrides the show method in Parent.

Console Output:

Child's show()

Java OOP: Access Modifiers

Access modifiers:

Access modifiers in Java determine the visibility of a class, method, or variable. They are public, protected, default, and private.

They help in implementing encapsulation in Java.


class Demo {
    public String publicVar = "Public";
    protected String protectedVar = "Protected";
    String defaultVar = "Default";
    private String privateVar = "Private";

    public void display() {
        System.out.println(publicVar);
        System.out.println(protectedVar);
        System.out.println(defaultVar);
        System.out.println(privateVar);
    }
}

public class TestAccessModifiers {
    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.display();
    }
}
    

The example demonstrates different access levels within the same class.

Console Output:

Public Protected Default Private

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025