You are on page 1of 4

OOP (Object Oriented Programming)

 OOP stands for Object-Oriented Programming, and it is a programming


paradigm that is widely used in Java and many other programming languages.
 The main idea behind OOP is to model software as a collection of objects
that interact with each other to accomplish tasks.
 Each object represents a real-world entity or concept and encapsulates data
(attributes) and behaviors (methods) related to that entity.

Four main principles of Object-Oriented Programming:

1. Encapsulation:
2. Inheritance:
3. Polymorphism:
4. Abstraction:

Inheritance
Inheritance is one of the fundamental concepts in Object-Oriented Programming
(OOP) that allows a class (subclass or derived class) to inherit properties and
behaviors from another class (super class or base class).
Inheritance creates a IS-A relationship between classes, where the subclass is a
specialized version of the super class.
The class that is being inherited from is called the super class, and the class that
inherits from the super class is called the subclass.
The subclass extends the capabilities of the super class by inheriting its attributes and
methods. It can also define additional attributes and methods of its own.

The key benefits of inheritance are code reuse and the establishment of a hierarchical
organization of classes.
When a subclass inherits from a super class, it gains access to all the public and protected
members (attributes and methods) of the super class.
This means that the subclass can use the inherited members directly without having to redefine
them.
It can also override inherited methods to provide a specialized implementation, tailoring the
behavior to its specific needs.
The syntax for defining inheritance in most programming languages, including Java, is as
follows:
// Defining a superclass
class Superclass {
// superclass members
}

// Defining a subclass that inherits from the superclass


class Subclass extends Superclass {
// additional members and overrides
}

The main types of inheritance are as follows:

Single Inheritance
Single inheritance involves a subclass inheriting from a single superclass. In this form of
inheritance, a class can have only one direct superclass. Most programming languages,
including Java, support single inheritance.

class A {
// superclass A
}

class B extends A {
// subclass B inherits from A
}

Multilevel Inheritance
Multilevel inheritance involves a chain of inheritance, where a subclass inherits from another
subclass, and so on. In this case, a class serves as both a superclass and a subclass at
different levels of the hierarchy.

class A {
// superclass A
}

class B extends A {
// subclass B inherits from A
}

class C extends B {
// subclass C inherits from B
}

Hierarchical Inheritance
Hierarchical inheritance refers to a situation where multiple subclasses inherit from a single
superclass. In other words, a superclass has several derived classes.

class Animal {
// superclass Animal
}

class Dog extends Animal {


// subclass Dog inherits from Animal
}

class Cat extends Animal {


// subclass Cat inherits from Animal
}

Multiple inheritance is not supported in java


Java does not support multiple inheritance of classes, meaning a class cannot directly inherit
from more than one class. There are several reasons why multiple inheritance was deliberately
avoided in Java:

Diamond Problem

 One of the main issues with multiple inheritance is the "diamond problem." This
problem arises when a class inherits from two or more classes that have a common super
class.
 If both super classes provide a method with the same name and the subclass does
not override it, the compiler cannot determine which method to use, leading to ambiguity.

class A {
void display() {
System.out.println("A");
}
}

class B {
void display() {
System.out.println("B");
}
}

class C extends A, B { // This is not allowed in Java


// ...
}
In this example, if class C were allowed to inherit from both class A and class B, it would be
ambiguous which display() method should be called if C doesn't provide its own
implementation.

By not supporting multiple inheritance of classes, Java aims to strike a balance between
flexibility and maintainability, providing an object-oriented programming model that is powerful,
yet clear and manageable.
Developers can still achieve many benefits of code reuse and extensibility through interfaces,
composition, and other design patterns.

You might also like