What Are The Main Features of OOPs
What Are The Main Features of OOPs
Object-oriented programming (OOP) is a programming paradigm that uses "objects" to represent data and
methods to manipulate that data. The main features of OOP can be summarized as follows:
1. Encapsulation : Encapsulation is the bundling of data (attributes) and methods (functions) that operate on
the data into a single unit, known as a class. It restricts direct access to some of the object's components, which
is a means of preventing unintended interference and misuse of the methods and data.
Example: In a class representing a bank account, the balance might be a private attribute, and methods like
deposit and withdraw would be public methods that control how the balance is modified.
2. Abstraction: Abstraction is the concept of hiding the complex reality while exposing only the necessary parts.
It allows a programmer to focus on interactions at a higher level without needing to understand the intricate
details of the implementation.
Example: A car can be driven using a steering wheel, accelerator, and brake. The driver does not need to
understand the internal workings of the engine or the transmission system.
3. Inheritance: Inheritance is a mechanism that allows one class (the child or subclass) to inherit the attributes
and methods of another class (the parent or superclass). This promotes code reusability and establishes a
hierarchical relationship between classes.
Example: If you have a base class Animal, you can create subclasses like Dog and Cat that inherit common
properties (like species and age) and methods (like speak) from Animal.
4. Polymorphism: Polymorphism allows methods to do different things based on the object it is acting upon,
even if they share the same name. This can be achieved through method overriding (in subclasses) and method
overloading (same method name with different parameters).
Example: A function draw can be defined in a base class Shape, and subclasses like Circle and Square can
provide their own implementations of draw. When you call draw on a Shape reference, the appropriate method
for the specific shape is executed.
5. Composition: Composition is a design principle where a class is composed of one or more objects from other
classes, allowing for a "has-a" relationship. This is often preferred over inheritance for creating complex types.
Example: A Car class might be composed of Engine, Wheel, and Transmission classes. This allows for more
flexible designs and easier maintenance.
6. Message Passing: In OOP, objects communicate with each other through messages (method calls). This
promotes loose coupling between objects, as they interact through well-defined interfaces rather than direct
references.
Example: An Order object might send a message to a Payment object to process a payment, without needing
to know the details of how the payment is processed.
2. Explain the concept of abstraction function and its significance in the context of Abstract Data
Types (ADTs).
Abstraction Function in Abstract Data Types (ADTs)
Abstraction Function is a concept that defines the relationship between the internal representation of an
Abstract Data Type (ADT) and its abstract model. It maps the concrete implementation to a meaningful
abstract view, allowing users to interact with the ADT without needing to understand its internal workings.
Key Points:
1. Mapping: It translates the internal state of an ADT into its logical representation.
2. Encapsulation: Hides implementation details, allowing changes without affecting user interaction.
3. Specification: Serves as a contract that describes the expected behaviour of the ADT.
4. Correctness: Ensures that operations maintain the relationship defined by the abstraction function.
Example:
• Abstract Representation: A stack with push and pop operations.
• Abstraction Function: Maps the internal state (e.g., an array) to the abstract stack (e.g., top element).
• Operations: push(x) adds x to the stack, and pop() removes the top element.
3. Differentiate between these keywords (a) final (b) finally (c ) finalise.
4. What is Compile time Polymorphism and how is it different from Runtime Polymorphism?
6.
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since Java
abstract methods. 8, it can have default and static methods also.
3) Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.
5) The abstract keyword is used to declare abstract The interface keyword is used to declare interface.
class.
6) An abstract class can extend another Java class An interface can extend another Java interface only.
and implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
7. Describe the key principles of encapsulation in object-oriented programming and provide an
example to illustrate its usage.
Key Principles of Encapsulation in Object-Oriented Programming
1. Data Hiding: Protects the internal state of an object from direct access by outside code.
2. Access Modifiers: Uses keywords like private, protected, and public to control visibility of class
members.
3. Getter and Setter Methods: Provides controlled access to private attributes, allowing validation and
logic during data retrieval and modification.
4. Improved Maintainability: Changes to the internal implementation can be made without affecting
external code that uses the class.
5. Increased Security: Protects data integrity by preventing unauthorized access and modifications.
Example of Encapsulation
class BankAccount { *private String accountNumber; *private double balance; *public BankAccount(String
accountNumber) { *this.accountNumber = accountNumber; *this.balance = 0.0; *}public double getBalance()
{ *return balance; *} *public void deposit(double amount) { *if (amount > 0) balance += amount; *} *public
void withdraw(double amount) { *if (amount > 0 && amount <= balance) balance -= amount; *} *}*
public class Main { *public static void main(String[] args) {
BankAccount account = new BankAccount("123456789"); *account.deposit(500);
*System.out.println("Balance: " + account.getBalance()); *account.withdraw(200);
System.out.println("Balance: " + account.getBalance()); *} *}
6. Explain the concept of inheritance in object-oriented design and provide a scenario where it is
beneficial.
Inheritance in Object-Oriented Design
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class
(subclass) to inherit properties and behaviors (attributes and methods) from an existing class (superclass).
This promotes code reusability, establishes a hierarchical relationship between classes, and allows subclasses
to override or extend the functionality of the superclass.
Benefits of Inheritance:
1. Code Reusability: Subclasses can reuse code from the superclass, reducing redundancy.
2. Hierarchical Classification: Organizes related classes in a natural hierarchy.
3. Extensibility: New functionality can be added without modifying existing code.
Example Scenario: Vehicle Management System
• Superclass: Vehicle (common attributes like make, model, year).
• Subclasses: Car, Truck, Motorcycle (specific attributes and methods).
Procedural Programming Object-oriented programming
It is a programming language that is derived from Object-oriented programming is a computer
structure programming and based upon the concept of programming design philosophy or methodology
calling procedures. It follows a step-by-step approach in that organizes/ models software design around
order to break down a task into a set of variables and data or objects rather than functions and logic.
routines via a sequence of instructions.