What are the four pillars of OOP?

Encapsulation: Hiding data and implementation details.

Achieved through access modifiers (public, private, protected).

Inheritance: Creating new classes based on existing classes.

Promotes code reuse.

Polymorphism: Allowing objects to take on multiple forms.

Method overloading (same name, different parameters).

Method overriding (derived class provides a different implementation of a base class method).

Abstraction: Focusing on essential features and hiding unnecessary details.

Abstract classes and interfaces.

Abstract Class vs. Interface:

Example:

Abstract Class:

Can have concrete and abstract methods.

Can have fields.

Can have constructors.

Single inheritance.

Interface:

Can only have abstract methods (or default implementations in modern C#).

Cannot have fields.

Cannot have constructors.

Multiple inheritance.

abstract class Shape { public abstract double Area(); }
interface IDrawable { void Draw(); }

Leave a Reply

Your email address will not be published. Required fields are marked *