Java Topic

Java Object-Oriented Programming (OOP)

Java is an object-oriented programming (OOP) language, which means it uses objects and classes to structure code. The core principles of OOP in Java are encapsulation, inheritance, polymorphism, and abstraction.

1. Encapsulation

Encapsulation is the practice of wrapping data (variables) and methods (functions) into a single unit, called a class. It restricts direct access to some of an object's components and can prevent the accidental modification of data. Access is typically controlled through access modifiers.

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

In this example, the Person class uses private fields to protect the data and provides public getter and setter methods to access and modify the data.

2. Inheritance

Inheritance is a mechanism where a new class, known as a subclass or derived class, inherits properties and behavior (methods) from an existing class, known as a superclass or base class. It promotes code reusability and establishes a natural hierarchy between classes.

public class Animal {
    public void eat() {
        System.out.println("Animal eats");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat(); // Inherited method
        myDog.bark(); // Subclass method
    }
}

In this example, the Dog class inherits from the Animal class and can use its eat method while also defining its own bark method.

3. Polymorphism

Polymorphism allows objects to be treated as instances of their parent class rather than their actual class. It provides the ability to perform a single action in different forms. Polymorphism can be achieved through method overriding and method overloading.

Method Overriding

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

public class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myCat = new Cat();
        myCat.makeSound(); // Outputs "Cat meows"
    }
}

Here, Cat overrides the makeSound method of Animal. At runtime, the Cat version of the method is called.

Method Overloading

public class Printer {
    public void print(String text) {
        System.out.println(text);
    }

    public void print(int number) {
        System.out.println(number);
    }
}

Method overloading allows multiple methods with the same name but different parameters within the same class.

4. Abstraction

Abstraction is the concept of hiding the complex implementation details and showing only the essential features of an object. In Java, abstraction is achieved using abstract classes and interfaces.

Abstract Classes

abstract class Shape {
    abstract void draw();

    public void fillColor() {
        System.out.println("Color filled");
    }
}

public class Circle extends Shape {
    @Override
    void draw() {
        System.out.println("Circle drawn");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape myCircle = new Circle();
        myCircle.draw(); // Outputs "Circle drawn"
        myCircle.fillColor(); // Outputs "Color filled"
    }
}

In this example, Shape is an abstract class with an abstract method draw. The concrete class Circle provides an implementation for draw.

Interfaces

interface Drawable {
    void draw();
}

public class Rectangle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Rectangle drawn");
    }
}

public class Main {
    public static void main(String[] args) {
        Drawable myRectangle = new Rectangle();
        myRectangle.draw(); // Outputs "Rectangle drawn"
    }
}

Lecturre learning image

Here, the Drawable interface defines the draw method, and Rectangle implements this interface.

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?