Skip to main content

Object Oriented Programming: Class, Object, Instantiation, Inheritance, Encapsulation, Abstract Class, Polymorphism

 Object Oriented Programming: Class, Object, Instantiation, Inheritance, Encapsulation, Abstract Class, Polymorphism

Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into objects, which are instances of classes. OOP encourages the use of concepts such as inheritance, encapsulation, and polymorphism to create reusable and maintainable code. Let's explore each of these concepts in detail:

1. Class:
A class is a blueprint or template for creating objects. It defines the attributes (data members) and behaviors (member functions) that the objects of the class will have. Classes act as user-defined data types that encapsulate data and methods related to a specific entity or concept.

Example:

```java
class Person {
    String name;
    int age;

    void introduce() {
        System.out.println("Hi, I am " + name + " and I am " + age + " years old.");
    }
}
```

2. Object:
An object is an instance of a class. It represents a specific occurrence of the class and contains its own set of data members and methods defined in the class. Objects are created from classes using a process called instantiation.

Example:

```java
public static void main(String[] args) {
    Person person1 = new Person();  // Creating an object of the class Person
    person1.name = "John";
    person1.age = 30;
    person1.introduce();  // Output: Hi, I am John and I am 30 years old.
}
```

3. Instantiation:
Instantiation is the process of creating an object from a class. When you instantiate a class, memory is allocated to store the object, and the constructor (a special method) is called to initialize the object.

Example:

```java
Person person1 = new Person();  // Instantiating the class Person
```

4. Inheritance:
Inheritance is a fundamental concept in OOP that allows a class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). It promotes code reusability and enables the creation of a hierarchy of classes.

Example:

```java
class Student extends Person {
    int rollNumber;

    void study() {
        System.out.println(name + " is studying.");
    }
}
```

In this example, the class `Student` inherits the attributes and behaviors of the class `Person` (e.g., `name` and `introduce()` method) and adds its own unique attribute `rollNumber` and method `study()`.

5. Encapsulation:
Encapsulation is the practice of hiding the internal details of an object and exposing only necessary functionality through methods. It protects the data and ensures that it can only be accessed or modified through controlled interfaces.

Example:

```java
class BankAccount {
    private double balance;

    public void deposit(double amount) {
        // Validation and logic to update balance
    }

    public double getBalance() {
        // Logic to retrieve balance
        return balance;
    }
}
```

In this example, the `balance` data member is made private to prevent direct access from outside the class. Access to the balance is provided through public methods `deposit()` and `getBalance()`.

6. Abstract Class:
An abstract class is a class that cannot be instantiated on its own and typically contains one or more abstract methods (methods without implementation). Abstract classes act as blueprints for other classes to inherit from and provide common functionality.

Example:

```java
abstract class Shape {
    abstract void draw();

    void displayArea() {
        // Common implementation for displaying area
    }
}
```

7. Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling them to respond to the same method calls differently based on their individual implementations. It can be achieved through method overriding and method overloading.

Example (Method Overriding):

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

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

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

public static void main(String[] args) {
    Animal animal1 = new Dog();
    Animal animal2 = new Cat();

    animal1.sound();  // Output: Dog barks.
    animal2.sound();  // Output: Cat meows.
}
```

In this example, the method `sound()` is overridden in the subclasses `Dog` and `Cat`, allowing polymorphism when calling the method on objects of the superclass `Animal`.

These concepts form the core of Object-Oriented Programming and are widely used to design robust, modular, and maintainable software systems.

Comments

Popular posts from this blog

Gujarati Keyboard layout (terafont-varun), Computer Short cut key, Tally short cut key

Terafont-Varun (Gujarati Typing) Keyboard Layout by "Sama Soyab"

  For Gujarati Typing : Required : Terafont-Varun Font  After Successfully Installed Terafont Varun Open Any Text Editor or any program. Select Font Terafont-Varun -> Ok For more detail please watch below video. Search Topics : Learn terafont varun, Learn terafont chandan, Learn terafont gujarati to english translation, Learn terafont varun keyboard, Learn terafont converter, Learn terafont varun zip, Learn terafont keyboard, Learn terafont kinnari, Learn terafont akash, Learn terafont aakash, Learn terafont akash ttf, Learn terafont aakash gujarati download, Learn terafont akash keyboard, Learn terafont akash download for windows 10, Learn terafont akash font download, Learn terafont arun, Learn terafont border, Learn terafont chandan keyboard, Learn terafont-chandan font, Learn tera font chandana, Learn convert terafont to shruti, Learn convert terafont varun to shruti, Learn terafont varun chart, Learn terafont download, Learn terafont download for windows 10, Learn terafont down

Word , Excel , Power Point Shortcut Key in Gujarati