May 10, 2024

Object-Oriented Programming concepts: classes, objects, inheritance, polymorphism, encapsulation, abstraction

Object-oriented programming concepts: classes, objects, inheritance, polymorphism, encapsulation, abstraction

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields (also known as attributes), and code, in the form of procedures (commonly known as methods).

Classes are the specifications of objects. They define the properties and behaviors common to all objects of a certain kind.

Objects are instances of classes. They represent real-world entities and have states and behaviors.

Inheritance allows one class (subclass) to inherit the properties and behaviors of another class (superclass). It promotes code reusability and establishes a relationship between classes.

Polymorphism enables objects to be treated as instances of their parent class, allowing for more flexibility in programming.

Encapsulation is the bundling of data and methods that operate on the data into a single unit. It hides the internal state of an object from the outside world and only exposes the necessary functionalities.

Abstraction is the process of hiding the implementation details and showing only the essential features of an object. It simplifies a complex system by modeling only it's relevant aspects.

Examples

// Example 1: Creating a class and an object
class Car {
    String brand;
    int year;

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

Car myCar = new Car("Toyota", 2024);

// Example 2: Inheritance
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

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

Dog myDog = new Dog();
myDog.sound();

// Example 3: Polymorphism
Animal animal = new Dog();
animal.sound();

// Example 4: Encapsulation
class Person {
    private String name;

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

    public String getName() {
        return name;
    }
}

Person person = new Person();
person.setName("John");
System.out.println(person.getName());

// Example 5: Abstraction
abstract class Shape {
    abstract void draw();
}

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

Shape shape = new Circle();
shape.draw();

FAQ (interview questions and answers)

  1. What is inheritance in Java?
    A way to hide data within a class
    A method of code reuse
    The ability of a class to inherit properties and behaviors from another class
  2. What is encapsulation?
    Bundling of data and methods into a single unit and restricting access to the data
    The process of creating objects from classes
    The ability of a subclass to inherit from a superclass
  3. What is the purpose of abstraction?
    To bundle data and methods into a single unit
    To hide the implementation details and show only the essential features of an object
    To inherit properties and behaviors from another class
  4. Can you create an object of an abstract class?
    Yes, but only in a static method
    No, abstract classes cannot be instantiated
    Yes, abstract classes can be instantiated like regular classes
  5. What is the main advantage of polymorphism?
    It allows flexibility and dynamic behavior in programming
    It hides the internal state of an object
    It bundles data and methods into a single unit

Your Total Score: 0 out of 5

Remember to just comment if you have any doubts or queries.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.