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

Java OOP Concepts

The document outlines key object-oriented programming concepts in Java, including encapsulation, inheritance, polymorphism, and abstraction. Encapsulation focuses on data hiding through private fields and public methods, while inheritance allows classes to inherit properties and behaviors from other classes. Polymorphism enables methods to behave differently based on context, and abstraction hides implementation details, exposing only essential features to users.

Uploaded by

ec9274172
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 views7 pages

Java OOP Concepts

The document outlines key object-oriented programming concepts in Java, including encapsulation, inheritance, polymorphism, and abstraction. Encapsulation focuses on data hiding through private fields and public methods, while inheritance allows classes to inherit properties and behaviors from other classes. Polymorphism enables methods to behave differently based on context, and abstraction hides implementation details, exposing only essential features to users.

Uploaded by

ec9274172
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

Object-Oriented Programming Concepts in Java

1. Encapsulation
Encapsulation is the process of wrapping data (variables) and the methods (functions)
that operate on that data into a single unit called a class, while restricting direct access
to the internal details of an object from outside the class. It is achieved in Java by
declaring the variables of a class as private and providing public getter and setter
methods to access and update the values of these variables.
The main idea behind encapsulation is data hiding — protecting the internal state of an
object from unauthorized access or modification. This improves security, allows
validation logic to be added before data is changed, and makes the code easier to
maintain because the internal implementation can be changed without affecting other
parts of the program that use the class.

Example:
public class Account {
private double balance; // private field, hidden from outside

public double getBalance() {


return balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
} else {
[Link]("Invalid deposit amount");
}
}
}
Here, balance cannot be accessed directly from outside the class (e.g., [Link]
= -500; would not compile). Instead, the deposit() method controls how the value
changes, ensuring only valid updates occur.

Benefits:
• Improved security
• Controlled access to data
• Easier maintenance
• Increased flexibility
• Better code organization
2. Inheritance
Inheritance is a mechanism that allows one class (called the subclass or child class) to
acquire the properties (fields) and behaviors (methods) of another class (called the
superclass or parent class). It is implemented in Java using the extends keyword and
represents an “is-a” relationship between classes.
Inheritance promotes code reusability because common functionality can be written
once in a superclass and reused by multiple subclasses, instead of rewriting the same
code repeatedly. It also allows for the creation of a class hierarchy, where general
behaviors are defined at a higher level and more specific behaviors are added in
subclasses.

Example:
class Animal {
void eat() {
[Link]("This animal eats food");
}
}

class Dog extends Animal {


void bark() {
[Link]("The dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog myDog = new Dog();
[Link](); // inherited from Animal
[Link](); // defined in Dog
}
}
In Java, a class can only extend one superclass (single inheritance for classes), but a
class can implement multiple interfaces, which allows Java to support a form of multiple
inheritance through interfaces.

Benefits:
• Code reusability
• Establishment of a relationship between classes
• Ability to extend and customize existing functionality
3. Polymorphism
Polymorphism means “many forms” and refers to the ability of an object, method, or
reference variable to behave differently depending on the context. In Java,
polymorphism is implemented in two main ways: compile-time (static) polymorphism
and runtime (dynamic) polymorphism.

a) Compile-time Polymorphism (Method Overloading)


This occurs when multiple methods in the same class have the same name but different
parameter lists (different number, type, or order of parameters). The compiler
determines which method to call based on the method signature at compile time.

class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

b) Runtime Polymorphism (Method Overriding)


This occurs when a subclass provides its own specific implementation of a method that
is already defined in its superclass, using the same method name, return type, and
parameters. The decision about which method to execute is made at runtime, based on
the actual object type, not the reference type. This is also known as dynamic method
dispatch.

class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Cat extends Animal {


@Override
void sound() {
[Link]("Cat meows");
}
}

public class Main {


public static void main(String[] args) {
Animal myAnimal = new Cat(); // reference type Animal, object type
Cat
[Link](); // Output: Cat meows
}
}

Benefits:
• Increases flexibility and extensibility of code
• Allows a single interface to represent different underlying forms (data types)
• Supports writing code that works on superclass types while behaving correctly for
subclass objects
4. Abstraction
Abstraction is the process of hiding the internal implementation details of an object and
showing only the essential features or functionalities to the user. It focuses on what an
object does rather than how it does it. In Java, abstraction is achieved using abstract
classes and interfaces.

a) Abstract Classes
An abstract class is declared using the abstract keyword and cannot be instantiated
directly. It can contain both abstract methods (without a body, which must be
implemented by subclasses) and concrete methods (with a body).

abstract class Shape {


abstract double area(); // abstract method, no body

void display() {
[Link]("This is a shape");
}
}

class Circle extends Shape {


double radius;

Circle(double radius) {
[Link] = radius;
}

@Override
double area() {
return [Link] * radius * radius;
}
}

b) Interfaces
An interface is a completely abstract type that defines a contract of methods that
implementing classes must provide. All methods in an interface are implicitly abstract
(unless declared default or static), and a class implements an interface using the
implements keyword.

interface Drawable {
void draw(); // abstract method
}

class Square implements Drawable {


@Override
public void draw() {
[Link]("Drawing a square");
}
}
Benefits:
• Reduces complexity by hiding unnecessary details
• Increases security by exposing only relevant information
• Improves code maintainability
• Allows changes to implementation without affecting code that uses the
abstraction
Summary Table
Concept Key Mechanism Main Purpose
Encapsulation private fields + Data hiding and protection
getters/setters
Inheritance extends keyword Code reusability and hierarchy
Polymorphism Method overloading & One interface, multiple behaviors
overriding
Abstraction abstract classes & interfaces Hiding implementation, showing
essentials

You might also like