0% found this document useful (0 votes)
8 views

Differences in OOP

The document outlines key differences in object-oriented programming concepts such as classes vs objects, encapsulation vs abstraction, method overloading vs overriding, interfaces vs abstract classes, and inheritance vs composition. Each section provides definitions, examples, and characteristics that distinguish these concepts. It serves as a comprehensive guide for understanding the foundational elements of object-oriented programming.

Uploaded by

Luccha Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Differences in OOP

The document outlines key differences in object-oriented programming concepts such as classes vs objects, encapsulation vs abstraction, method overloading vs overriding, interfaces vs abstract classes, and inheritance vs composition. Each section provides definitions, examples, and characteristics that distinguish these concepts. It serves as a comprehensive guide for understanding the foundational elements of object-oriented programming.

Uploaded by

Luccha Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Made by CodeWithCurious.

com with ❤️

ALL COMMONLY
ASKED DIFFRENCES IN
OBJECT ORIENTED
PROGRAMMING

Store.CodeWithCurious.com
For More Coding Books & Handwritten Notes © 2024 All Rights Reserved CodeWithCurious
Class vs Objects

Class Object

A blueprint or template for creating An instance of a class. It represents a


objects. real entity.

No memory is allocated until objects


Takes up memory when instantiated.
are created.

Abstract concept. Does not exist at


Exists at runtime.
runtime.

Defines properties (attributes) and Holds specific data and behaves as per
behaviors (methods). the class definition.

myCar = Car() (a specific car object,


Car (a general concept of cars)
say a Honda).

Declared once and can be reused Can be created as many times as


multiple times. needed.

Not initialized with values, only defines Initialized with specific values for its
the structure. properties.

Stored as a definition in the code. Stored in memory with data.

Used to interact with real data and


Used to define the type of objects.
invoke methods.

Curious_.Programmer
Encapsulation Vs Abstraction

Encapsulation Abstraction
Hiding internal data and allowing Hiding complex details and showing
access through methods. only important parts.

To protect data from unauthorized To simplify usage by showing only


access. what’s needed.

By using private variables and public


By using abstract classes or interfaces.
methods (getters/setters).

Simplifying how you interact with


Controlling access to data.
something.

A remote control hiding internal wiring, A car’s interface (steering, pedals)


only showing buttons. hides engine details.

Provides a way to create a high-level


Provides a way to safeguard an object's
view of an object without showing how
state by restricting direct access.
it works inside.

Focuses on showing only what’s


Controls data visibility using access
necessary for the task, leaving out
modifiers like private, protected, public.
unnecessary details.

Ensures the integrity of the object's Allows working with complex systems
state by allowing controlled without needing to know how they work
modification. internally.

A Shape interface that only has a draw()


A class with private variables and
method, hiding how different shapes are
public methods to set/get values.
drawn.

Curious_.Programmer
Overloading Vs Overriding

Method Overloading Method Overriding

Defining multiple methods with the


Redefining a method in a subclass that
same name but different parameters in
already exists in the parent class.
the same class.

To perform similar tasks with different To provide a specific implementation of


types or numbers of inputs. a method in the subclass.

Between parent and child classes


In the same class.
(inheritance).

Must have different parameter lists Must have the same method signature
(number, type, or order). (name and parameters).

Must have the same return type as the


Can have different return types.
parent method.

Access modifier must be the same or


Access modifier can be different. more permissive than in the parent
class.

Resolved during runtime (dynamic


Resolved during compile-time.
binding).

add(int a, int b) and add(double a, A draw() method in Circle class


double b) in the same class. overriding draw() in Shape class.

Curious_.Programmer
Interface Vs Abstract Class

Interface Abstract Class

A collection of abstract methods that a A class that can have both abstract
class must implement. and concrete (implemented) methods.

To provide a common base class with


To define a contract for what a class some shared functionality and force
must do, without specifying how. subclasses to implement certain
methods.

Can have both abstract methods (no


Only abstract methods (no
implementation) and concrete
implementation allowed).
methods (with implementation).

A class can implement multiple A class can extend only one abstract
interfaces. class (single inheritance).

Use when you want to provide some


Use when you want to specify what
shared code along with abstract
methods a class must implement.
methods.

Methods are public and cannot have Can have any access modifier (public,
any other access modifier. private, protected).

Cannot have instance variables (only Can have instance variables and
constants). constants.

Cannot have constructors. Can have constructors.

Curious_.Programmer
Inheritance Vs Composition

Inheritance Composition

A relationship where a class (subclass) A relationship where a class contains


inherits properties and behavior from objects of other classes to reuse
another class (superclass). functionality.

"Is-a" relationship (e.g., Dog is a type of "Has-a" relationship (e.g., Car has an
Animal). Engine).

Involves parent-child (superclass- No hierarchy, focuses on including other


subclass) hierarchy. classes as fields.

Reuses code from the parent class by Reuses code by including objects of
extending it. other classes.

Less flexible, as the subclass is tightly More flexible, as classes are loosely
coupled with the superclass. coupled.
Copyright: CodeWithCurious.com
Use when you want to include
Use when there's a clear hierarchical
functionality from multiple classes
relationship.
without inheritance.

Changes in the superclass can affect Changes in a contained class won’t


all subclasses. affect the parent class as directly.

class Dog extends Animal class Car { Engine engine; }

Curious_.Programmer

You might also like