0% found this document useful (0 votes)
19 views5 pages

What Are The Main Features of OOPs

Uploaded by

BARUN SINGH
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)
19 views5 pages

What Are The Main Features of OOPs

Uploaded by

BARUN SINGH
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/ 5

1. 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.

final finally finalize


final is the keyword and access finally is the block in Java finalize is the method in Java
modifier which is used to apply Exception Handling to execute the which is used to perform clean up
restrictions on a class, method or important code whether the processing just before object is
variable. exception occurs or not. garbage collected.
Final keyword is used with the Finally block is always related to finalize() method is used with the
classes, methods and variables. the try and catch block in objects.
exception handling.
(1) Once declared, final variable (1) finally block runs the important finalize method performs the
becomes constant and cannot be code even if exception occurs or cleaning activities with respect to
modified. not. the object before its destruction.
(2) final method cannot be (2) finally block cleans up all the
overridden by sub class. resources used in try block
(3) final class cannot be inherited.
Final method is executed only Finally block is executed as soon finalize method is executed just
when we call it. as the try-catch block is executed. before the object is destroyed.
It's execution is not dependant on
the exception.

4. What is Compile time Polymorphism and how is it different from Runtime Polymorphism?

Compile-time Polymorphism vs. Runtime Polymorphism


Compile-time Polymorphism (Static Polymorphism):
• Resolution Time: Determined at compile time.
• Mechanism: Achieved through method overloading (same method name with different parameters)
and operator overloading.
• Performance: Generally faster due to early resolution.
• Example: Multiple add methods in a class that handle different data types (e.g., integers and floats).
Runtime Polymorphism (Dynamic Polymorphism):
• Resolution Time: Determined at runtime.
• Mechanism: Achieved through method overriding (subclass provides a specific implementation of a
method defined in a superclass).
• Performance: Slightly slower due to dynamic resolution.
• Example: A base class Animal with a speak method overridden in subclasses Dog and Cat, where the
actual method called depends on the object type at runtime.
Key Differences:

Feature Compile-time Polymorphism Runtime Polymorphism

Resolution Time Compile time Runtime

Mechanism Method Overloading, Operator Overloading Method Overriding

Performance Generally faster Slightly slower

Flexibility Less flexible More flexible


5. What are the various types of constructors in java?
In Java, constructors are special methods used to initialize objects. They have the same name as the class and
do not have a return type. There are several types of constructors in Java, which can be categorized as
follows:
1. Default Constructor: A default constructor is a constructor that does not take any parameters. If no
constructor is defined in a class, the Java compiler automatically provides a default constructor.
Example:1class Dog { * 2 String name; * 3. // Default constructor *4. Dog() { *5. name = "Unknown"; *6. }}
public class Main { public static void main(String[] args) {Dog dog = new Dog();
System.out.println(dog.name); // Output: Unknown* }}
2. Parameterized Constructor: A parameterized constructor is a constructor that takes one or more
parameters. It allows you to initialize an object with specific values at the time of its creation.
Example:class Dog { String name; // Parameterized constructor* Dog(String name) {* this.name = name;*}}
public class Main { public static void main(String[] args) {*Dog dog = new Dog("Buddy");*
System.out.println(dog.name); // Output: Buddy* }}
3. No-Argument Constructor: A no-argument constructor is similar to a default constructor, but it can be
explicitly defined. It does not take any parameters and is used to initialize objects with default values.
Example:class Dog { String name; // No-argument constructor * Dog() { * name = "Default Dog";* }}
public class Main { public static void main(String[] args) { * Dog dog = new Dog();
System.out.println(dog.name); // Output: Default Dog * } }
4. Copy Constructor: A copy constructor is a constructor that creates a new object as a copy of an existing
object. Java does not provide a built-in copy constructor, but you can define one manually.
Example:class Dog { *String name; // Parameterized constructor* Dog(String name) {* this.name = name;
} // Copy constructor *Dog(Dog dog) { * this.name = dog.name; *}} *public class Main {public static void
main(String[] args) { * Dog dog1 = new Dog("Buddy"); * Dog dog2 = new Dog(dog1); *
System.out.println(dog2.name); // Output: Buddy * } *}

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.

2) Abstract class doesn't support multiple Interface supports multiple inheritance.


inheritance.

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()); *} *}

8. Differences Between Object Identity and Polymorphism in OOP


• Definition: Refers to the uniqueness of an object in memory.
• Characteristics: Each object has a distinct identity, even if they have the same state.
• Comparison: Checked using reference comparison (e.g., == in Java).
• Example:
1Person person1 = new Person("Alice");
2Person person2 = new Person("Alice");
3System.out.println(person1 == person2); // Output: false (different identities)
Polymorphism:
• Definition: Allows objects of different classes to be treated as objects of a common superclass.
• Characteristics: Achieved through method overriding and interfaces; the method executed is
determined at runtime.
• Example
1Animal myDog = new Dog();
2Animal myCat = new Cat();
3myDog.speak(); // Output: Woof!
4myCat.speak(); // Output: Meow!

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.

It is less secure than OOPs. Data hiding is possible in object-oriented


programming due to abstraction. So, it is more
secure than procedural programming.

It follows a top-down approach. It follows a bottom-up approach.


In procedural programming, data moves freely within In OOP, objects can move and communicate with
the system from one function to another. each other via member functions.
It is structure/procedure-oriented. It is object-oriented.
There are no access modifiers in procedural The access modifiers in OOP are named as
programming. private, public, and protected.
Procedural programming does not have the concept of There is a feature of inheritance in object-
inheritance. oriented programming.
There is no code reusability present in procedural It offers code reusability by using the feature of
programming. inheritance.
Overloading is not possible in procedural programming. In OOP, there is a concept of function overloading
and operator overloading.
It gives importance to functions over data. It gives importance to data over functions.
In procedural programming, there are no virtual classes. In OOP, there is an appearance of virtual classes
in inheritance.
It is not appropriate for complex problems. It is appropriate for complex problems.
There is not any proper way for data hiding. There is a possibility of data hiding.
In Procedural programming, a program is divided into In OOP, a program is divided into small parts that
small programs that are referred to as functions. are referred to as objects.
Examples of Procedural programming include C, Fortran, The examples of object-oriented programming
Pascal, and VB. are -
.NET, C#, Python, Java, VB.NET, and C++.

You might also like