0% found this document useful (0 votes)
2 views6 pages

OOP 7 Mark Theory Detailed All

The document provides a detailed explanation of Object-Oriented Programming (OOP) concepts, including the four pillars: encapsulation, abstraction, inheritance, and polymorphism, along with their examples. It also covers various topics such as method overloading vs. overriding, types of inheritance, access modifiers, and the differences between classes and objects. Additionally, it discusses constructors, garbage collection, and the use of 'super' and 'this' keywords in Java.

Uploaded by

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

OOP 7 Mark Theory Detailed All

The document provides a detailed explanation of Object-Oriented Programming (OOP) concepts, including the four pillars: encapsulation, abstraction, inheritance, and polymorphism, along with their examples. It also covers various topics such as method overloading vs. overriding, types of inheritance, access modifiers, and the differences between classes and objects. Additionally, it discusses constructors, garbage collection, and the use of 'super' and 'this' keywords in Java.

Uploaded by

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

OOP 7-Mark Theory Answers (Detailed)

1. Explain the four pillars of Object-Oriented Programming with examples.


Object-Oriented Programming (OOP) is based on four main concepts:

1. Encapsulation:
Wrapping data and methods into a single unit (class). It hides internal data from the
outside world.
Example: Making variables private and using public getters and setters.

2. Abstraction:
Hiding complex details and showing only the essential parts. It helps in reducing
complexity.
Example: Using a phone – we can dial a number without knowing the internal working.

3. Inheritance:
One class (child) inherits the properties and behaviors of another class (parent).
Example: A Dog class inheriting from Animal class.

4. Polymorphism:
One thing behaves in multiple ways. Example: a method can have multiple forms using
overloading/overriding.

These pillars make Java code reusable, flexible, and maintainable.

2. What is the difference between method overloading and method overriding?


Explain with examples.
Both are ways to implement polymorphism.

Method Overloading:
- Same method name with different parameter lists.
- Happens in the same class.
- Example: sum(int a, int b) and sum(double a, double b).

Method Overriding:
- A subclass redefines a method from its parent class.
- Signature must be the same.
- Example: Animal has speak(); Dog overrides it.

Overloading = Compile-time polymorphism.


Overriding = Run-time polymorphism.
3. What is inheritance in Java? Explain types of inheritance supported in Java.
Inheritance lets a subclass reuse code from a superclass.

Java supports:
1. Single Inheritance – one class inherits another.
2. Multilevel – a class inherits from another which is itself a subclass.
3. Hierarchical – multiple classes inherit from one class.

Java doesn't support multiple inheritance via classes to avoid confusion.

Inheritance increases code reuse and modularity.

4. What is encapsulation? How does it help in data hiding?


Encapsulation binds data and related methods together in a class and restricts direct access.

It is achieved using private variables and public getter/setter methods.

Benefits:
- Data protection
- Improved control
- Modular and clean code

Encapsulation supports the principle of data hiding in OOP.

5. What is abstraction in Java? How is it achieved?


Abstraction hides internal details and shows only necessary features.

Achieved using:
1. Abstract classes: contain abstract and concrete methods.
2. Interfaces: contain only abstract methods (Java 7), and default/static from Java 8.

Example: Driving a car – you don’t need to know how the engine works.

Abstraction reduces complexity and focuses on essential behavior.

6. What is constructor in Java? Explain types with examples.


Constructor is a special method that runs automatically when an object is created.

Types:
1. Default constructor – takes no arguments.
2. Parameterized constructor – takes parameters to initialize variables.
3. Copy constructor – manually copies another object's data.
Constructor has no return type and the same name as the class.

7. What is the difference between abstract class and interface?


Abstract class:
- Can have both abstract and concrete methods.
- Can have constructors and instance variables.

Interface:
- Only abstract methods (until Java 7), static/default methods (Java 8+).
- Cannot have constructors.
- All variables are public, static, and final.

Use abstract class for partial abstraction, interface for full abstraction and multiple
inheritance.

8. What is polymorphism? Explain its types with examples.


Polymorphism means one name, many forms. In Java:

1. Compile-time (Static): method overloading.


2. Run-time (Dynamic): method overriding.

Example:
- Overloading: print(int), print(String)
- Overriding: Animal's speak() vs Dog's speak()

Polymorphism improves flexibility and reusability.

9. Explain the types of access modifiers in Java with examples.


Java has 4 access modifiers:
1. private – only inside class
2. default – only inside package
3. protected – package + subclasses
4. public – everywhere

They are used to control visibility and protect data.

10. What is the difference between class and object in Java?


Class is a blueprint or template. Object is a real entity created from the class.

Class defines structure (variables, methods), Object stores real values and behavior.

Example: Car is a class. 'myCar' is an object of Car.


11. What is static keyword in Java? Explain with uses.
'static' means the member belongs to the class, not instances.

Usage:
- static variable – shared by all objects
- static method – called without object
- static block – used for static initialization

Static members save memory and are useful for utility code.

12. What is package in Java? What are the types of packages?


Package is a group of related classes and interfaces.

Types:
1. Built-in packages: java.util, java.io, etc.
2. User-defined packages: created by programmers.

Packages help organize code, avoid name conflicts, and control access.

13. What is garbage collection in Java? Explain its working.


Garbage Collection (GC) is the process of removing unused objects from memory
automatically.

When an object is no longer referenced, JVM considers it for GC.

Java’s GC helps in memory management and avoids memory leaks.

14. Explain the difference between instance and static variables.


Instance variable:
- Each object has its own copy.

Static variable:
- Shared across all objects. Belongs to class.

Use static when value is common to all objects.


Example: School name for all students.

15. Differentiate between abstract class and interface in Java.


Abstract class:
- Can have both abstract and normal methods.
- Can have constructors.

Interface:
- Only abstract methods (Java 7). From Java 8: default, static methods.
- No constructors.

Interface is used for full abstraction and multiple inheritance.

16. Differentiate between method overloading and method overriding.


Overloading:
- Same method name, different parameters.
- Same class.
- Compile-time polymorphism.

Overriding:
- Same method signature in subclass.
- Parent-child class relation.
- Run-time polymorphism.

17. Explain the life cycle of an object in Java.


Object life cycle:
1. Creation – using 'new'
2. Usage – calling methods
3. Unreachable – no reference
4. Destruction – GC reclaims memory

Java automates object destruction using Garbage Collector.

18. Real-world examples of OOP concepts


Encapsulation – Mobile (you use buttons, don’t access internals)
Abstraction – ATM (you withdraw cash, don’t see code)
Inheritance – Child inherits traits from parent
Polymorphism – One person plays multiple roles.

19. What is the difference between constructor and method in Java?


Constructor:
- Initializes object
- No return type
- Called automatically

Method:
- Performs action
- Has return type
- Called manually

Constructor runs once; methods run multiple times.


20. Explain the concept of ‘super’ and ‘this’ keyword in Java.
'this' keyword:
- Refers to current object
- Used to access class variables and methods

'super' keyword:
- Refers to parent class object
- Used to access superclass methods or constructors

Both help in managing scope and inheritance.

You might also like