OOP 7 Mark Theory Detailed All
OOP 7 Mark Theory Detailed All
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.
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.
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.
Benefits:
- Data protection
- Improved control
- Modular and clean code
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.
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.
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.
Example:
- Overloading: print(int), print(String)
- Overriding: Animal's speak() vs Dog's speak()
Class defines structure (variables, methods), Object stores real values and behavior.
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.
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.
Static variable:
- Shared across all objects. Belongs to class.
Interface:
- Only abstract methods (Java 7). From Java 8: default, static methods.
- No constructors.
Overriding:
- Same method signature in subclass.
- Parent-child class relation.
- Run-time polymorphism.
Method:
- Performs action
- Has return type
- Called manually
'super' keyword:
- Refers to parent class object
- Used to access superclass methods or constructors