Object-Oriented Programming (OOP) in Java
Chapter 7: Object-Oriented Programming (OOP) in Java
Java is a powerful object-oriented programming language. It emphasizes designing programs using real-world entities like objects and classes. OOP helps create modular, maintainable, reusable, and scalable software systems.
🔹 Four Main Principles of OOP
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
🛡️ 1. Encapsulation in Java
Encapsulation is the concept of wrapping data (variables) and methods (code) into a single unit called a class. It helps hide the internal implementation of the object from the outside world.
Example:
class Person {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Benefits: Data hiding, code maintainability, security, and flexibility.
🧬 2. Inheritance in Java
Inheritance allows a class (child or subclass) to acquire fields and methods of another class (parent or superclass).
Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
Types of Inheritance:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Note: Java does not support multiple inheritance with classes but supports it through interfaces.
🔁 3. Polymorphism in Java
Polymorphism means "many forms." It allows methods to behave differently based on the object or parameters.
➤ Method Overloading (Compile-Time Polymorphism)
class MathUtils {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
➤ Method Overriding (Run-Time Polymorphism)
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
🎭 4. Abstraction in Java
Abstraction is the process of hiding implementation details and showing only the essential features of an object.
➤ Using Abstract Class
abstract class Shape {
abstract void draw();
void display() {
System.out.println("This is a shape.");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
➤ Using Interface
interface Printable {
void print();
}
class Document implements Printable {
public void print() {
System.out.println("Printing document...");
}
}
📘 Summary
Concept | Description |
---|---|
Encapsulation | Binding data and methods into a class |
Inheritance | Acquiring properties from a parent class |
Polymorphism | Multiple behaviors with the same method name |
Abstraction | Hiding internal details, showing only essentials |
❓ FAQs with Answers
Q1: What is encapsulation in Java?
A: It is the process of wrapping variables and methods into a single unit. It helps in data hiding and securing code.
Q2: What is inheritance in Java?
A: Inheritance allows one class to acquire properties and methods of another class, enhancing code reusability.
Q3: What is the difference between method overloading and overriding?
- Overloading: Same method name, different parameters in the same class.
- Overriding: Redefining the parent method in a subclass.
Q4: What is abstraction and how is it implemented?
A: Abstraction hides complexity and exposes only relevant parts. It is implemented via abstract classes and interfaces.
Q5: Interface vs Abstract Class?
Feature | Abstract Class | Interface |
---|---|---|
Methods | Abstract and Concrete | Only Abstract (till Java 7), default/static (from Java 8) |
Multiple Inheritance | Not Supported | Supported |
Constructor | Yes | No |
Access Modifiers | Any | Public Only |
Q6: Why is OOP important in Java?
A: It promotes modularity, scalability, and maintainability. It models real-world problems more naturally.
📌 Related Links: Complete Java Course
- Chapter 1: Introduction to Java
- Chapter 2: Java Environment Setup
- Chapter 3: Variables and Data Types
- Chapter 4: Operators in Java
- Chapter 5: Control Statements
- Chapter 6: Loops in Java
- Chapter 7: Arrays and Strings
- Chapter 8: Object-Oriented Programming (OOP)
- Chapter 9: Inheritance and Polymorphism
- Chapter 10: Exception Handling
- Chapter 11: Multithreading
- Chapter 12: File Handling in Java
- Chapter 13: Applets and GUI (AWT/Swing)
- Chapter 14: Java Project Ideas & Practice Questions
Comments
Post a Comment