Java OOPs concept with examples

Object-Oriented Programming in Java

Understanding Object-Oriented Programming in Java

Classes and Objects

A class in Java is a blueprint for creating objects. Objects are instances of classes. Let's look at an example:


    class Car {
        String brand;
        String model;
        int year;

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

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

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

Output:


        Brand: Toyota
        Model: Camry
        Year: 2021
        

Inheritance

Inheritance allows a class to inherit properties and methods from another class. Here's an example:


    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 Main {
        public static void main(String[] args) {
            Dog myDog = new Dog();
            myDog.eat();
            myDog.bark();
        }
    }
    

Output:


        This animal eats food.
        The dog barks.
        

Polymorphism

Polymorphism enables objects of different classes to be treated as objects of a common superclass. Here's an example:


    // Java program to demonstrate polymorphism
    class Animal {
        void sound() {
            System.out.println("Animal makes a sound.");
        }
    }

    class Dog extends Animal {
        @Override
        void sound() {
            System.out.println("Dog barks.");
        }
    }

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

    public class Main {
        public static void main(String[] args) {
            Animal myAnimal1 = new Dog();
            Animal myAnimal2 = new Cat();

            myAnimal1.sound();
            myAnimal2.sound();
        }
    }
    

Output:


        Dog barks.
        Cat meows.
        

Encapsulation

Encapsulation is the practice of hiding internal details of an object and providing a public interface. Example:


    // Java program to demonstrate encapsulation
    class Student {
        private String name;
        private int age;

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(int age) {
            if (age >= 0) {
                this.age = age;
            }
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }
    }

    public class Main {
        public static void main(String[] args) {
            Student student = new Student();
            student.setName("Alice");
            student.setAge(20);

            System.out.println("Name: " + student.getName());
            System.out.println("Age: " + student.getAge());
        }
    }
    

Output:


        Name: Alice
        Age: 20
        

Abstraction

Abstraction focuses on showing only essential features of an object. Example:


    // Java program to demonstrate abstraction
    abstract class Shape {
        abstract void draw();
    }

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

    class Rectangle extends Shape {
        @Override
        void draw() {
            System.out.println("Drawing a rectangle");
        }
    }

    public class Main {
        public static void main(String[] args) {
            Shape circle = new Circle();
            Shape rectangle = new Rectangle();

            circle.draw();
            rectangle.draw();
        }
    }
    

Output:


        Drawing a circle
        Drawing a rectangle
        

Comments