[Go to site: main page, start]

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

Java

The document explains key Object-Oriented Programming (OOP) concepts in Java: Abstraction, Encapsulation, Polymorphism, and Inheritance. It provides examples and analogies for each concept, illustrating how they simplify code, promote reusability, and establish relationships between classes. These concepts are essential for creating clean, scalable, and maintainable systems in Java.

Uploaded by

shivang8
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)
2 views9 pages

Java

The document explains key Object-Oriented Programming (OOP) concepts in Java: Abstraction, Encapsulation, Polymorphism, and Inheritance. It provides examples and analogies for each concept, illustrating how they simplify code, promote reusability, and establish relationships between classes. These concepts are essential for creating clean, scalable, and maintainable systems in Java.

Uploaded by

shivang8
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

Abstraction in Java: Simplified

Abstraction in Java hides complex details and shows only essential features. It’s
achieved using abstract classes and interfaces. Let’s see an example.

Example: Vehicle System

Step 1: Abstract Class

An abstract class defines a blueprint with abstract methods (no implementation) and
concrete methods (with implementation).

abstract class Vehicle {

public abstract void start();

public abstract void stop();

public void honk() {

[Link]("Honk! Honk!");

Step 2: Concrete Subclasses

Subclasses provide specific implementations for the abstract methods

class Car extends Vehicle {

@Override

public void start() {

[Link]("Car starts with a key.");

@Override

public void stop() {

[Link]("Car stops by braking.");

}
class Bike extends Vehicle {

@Override

public void start() {

[Link]("Bike starts with a kick.");

@Override

public void stop() {

[Link]("Bike stops by braking.");

Step 3: Use Abstraction

Create objects and use them without worrying about internal details.

public class Main {

public static void main(String[] args) {

Vehicle myCar = new Car();

Vehicle myBike = new Bike();

[Link](); // Output: Car starts with a key.

[Link](); // Output: Bike stops by braking.

[Link](); // Output: Honk! Honk!

Key Points

1. Hides Complexity: Shows only what’s needed.


2. Promotes Reusability: Share common behavior.
3. Supports Flexibility: Add new types easily.

Real-World Analogy

Like using a TV remote—you press buttons without knowing how it works internally.
Conclusion

Abstraction simplifies code by focusing on what an object does, not how it does it.
It’s essential for clean, scalable systems.

Q5

Encapsulation in Java: Simplified

Encapsulation is an OOP concept that bundles data (attributes) and methods


(behavior) into a single unit (class) and restricts direct access to the data by making
fields private. It’s achieved using getters and setters to control access.

Example: Encapsulation

Step 1: Create a Class with Private Fields

Make fields private to restrict direct access.

class Person {

private String name; // Private field

private int age; // Private field

Step 2: Use Getters and Setters

Provide public methods to access and modify private fields.

class Person {

private String name;

private int age;

// Getter for name

public String getName() {


return name;

// Setter for name

public void setName(String name) {

[Link] = name;

// Getter for age

public int getAge() {

return age;

// Setter for age

public void setAge(int age) {

if (age > 0) { // Validation

[Link] = age;

Step 3: Use the Encapsulated Class

Access and modify data using getters and setters.

public class Main {

public static void main(String[] args) {

Person person = new Person();

[Link]("John");

[Link](25);

[Link]("Name: " + [Link]()); // Output: Name: John

[Link]("Age: " + [Link]()); // Output: Age: 25

Key Points
1. Data Hiding: Fields are private, preventing direct access.
2. Controlled Access: Getters and setters manage how data is read or modified.
3. Validation: Setters can include logic to validate data.

Real-World Analogy

Think of encapsulation like a bank account. You can’t directly access your balance;
you use methods (e.g., ATM, online banking) to interact with it.

Conclusion

Encapsulation protects data and ensures controlled access, making your code more
secure and maintainable. It’s a fundamental concept in Java and OOP.

Q-6
Polymorphism in Java: Simplified

Polymorphism means "many forms." It allows objects to behave differently


based on their type. In Java, it’s achieved through method
overloading (compile-time) and method overriding (runtime).

Types of Polymorphism

1. Compile-Time Polymorphism (Method Overloading):


▪ Multiple methods with the same name but different
parameters.
▪ Resolved during compile time.

class Math {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
Runtime Polymorphism (Method Overriding):
• A subclass provides a specific implementation of a method already defined
in its superclass.
• Resolved during runtime.
• Example:
class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
@Override
void sound() { [Link]("Bark"); }
}

Key Differences

Aspect Compile-Time Polymorphism Runtime Polymorphism

Mechanism Method overloading Method overriding

Resolution Compile time Runtime

Example add(int, int) vs add(double, double) [Link]() vs [Link]()

Real-World Analogy

• Compile-Time: Like choosing a tool (e.g., hammer or screwdriver) based on


the task.
• Runtime: Like a dog barking or a cat meowing—behavior depends on the
object.

Conclusion
Polymorphism allows flexibility in Java. Compile-time polymorphism is resolved
early, while runtime polymorphism is resolved during execution. Both are essential
for writing reusable and dynamic code.

Q-8
Inheritance in Java: Simplified

Inheritance is an OOP concept where a subclass (child class) inherits properties and
behaviors from a superclass (parent class). It promotes code reusability and
establishes an "is-a" relationship.

Types of Inheritance

1. Single Inheritance:
o A subclass inherits from one superclass.
o Example:

class Animal {
void eat() { [Link]("Eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("Barking..."); }
}
Multilevel Inheritance:
• A subclass inherits from another subclass, forming a chain.
• Example:
class Animal {
void eat() { [Link]("Eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("Barking..."); }
}
class Puppy extends Dog {
void weep() { [Link]("Weeping..."); }
}
Hierarchical Inheritance:
• Multiple subclasses inherit from one superclass.
• Example:
class Animal {
void eat() { [Link]("Eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("Barking..."); }
}
class Cat extends Animal {
void meow() { [Link]("Meowing..."); }
}

Multiple Inheritance (Not supported in Java):


• A subclass inherits from multiple superclasses.
• Java doesn’t support this directly but achieves it using interfaces.
• Example

interface A { void showA(); }


interface B { void showB(); }
class C implements A, B {
public void showA() { [Link]("A"); }
public void showB() { [Link]("B"); }
}

Key Points

• Code Reusability: Inherit fields and methods from a superclass.


• "Is-A" Relationship: A subclass is a type of its superclass (e.g., Dog is
an Animal).
• Extends Keyword: Used for class inheritance.
• Implements Keyword: Used for interface inheritance.

Real-World Analogy

Think of inheritance like a family tree. A child inherits traits from their parents, and
those traits can be passed down further.

Conclusion

Inheritance in Java allows you to build hierarchical relationships between classes,


promoting code reuse and organization. It’s a fundamental concept in OOP.

You might also like