Java OOP(Object Oriented Programming) Concepts
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects that
contain data (fields) and behavior (methods). It focuses on designing software that closely represents real-
world entities. It is used to:
Improves code reusability
Enhances maintainability and scalability
Makes programs easier to understand and manage
Closely models real-world entities
1. Class
A Class is a user-defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type. Using classes, you can create multiple
objects with the same behavior instead of writing their code multiple times. In general, class declarations
can include these components in order:
Modifiers: A class can be public or have default access (Refer to this for details).
Class name: The class name should begin with the initial letter capitalized by convention.
Body: The class body is surrounded by braces, { }.
Example: A Car represents a class (blueprint), while BMW, Mercedes, and Audi represent objects
(instances) created from that class.
class-object
2. Object
An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical Java
program creates many objects, which as you know, interact by invoking methods. The objects are what
perform your code, they are the part of your code visible to the viewer/user. An object mainly consists of:
State: It is represented by the attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object to
other objects.
Identity: It is a unique name given to an object that enables it to interact with other objects.
Method: A method is a collection of statements that perform some specific task and return the
result to the caller.
3. Abstraction
Abstraction in Java is the process of hiding implementation details and showing only the essential features
of an object. It helps users focus on what an object does rather than how it does it.
Hides complexity: Internal implementation details are hidden from the user.
Improves maintainability: Changes in implementation do not affect the user code.
Enhances flexibility: Supports loose coupling through abstract classes and interfaces.
Example: An ATM or a coffee machine represents abstraction, where the user interacts with simple
operations while the internal working and implementation details remain hidden.
Abstraction
How to Achieve Abstraction
It is achieved in Java using abstract classes and interfaces.
Interfaces can provide 100% abstraction, while abstract classes provide partial abstraction.
4. Encapsulation
Encapsulation is the process of wrapping data and methods into a single unit, usually a class, and restricting
direct access to the data. It acts as a protective shield that prevents data from being accessed directly from
outside the class.
Data members are hidden using the private access modifier.
Access to data is provided through public getter and setter methods.
It improves data security, maintainability, and controlled access.
5. Inheritance
Inheritance is a core OOP concept in Java that allows one class to acquire the fields and methods of
another class using the extends keyword. It represents an “is-a” relationship between classes.
The class being inherited is called the superclass, and the inheriting class is the subclass.
A subclass can use existing features of the superclass and also add its own.
Inheritance promotes code reusability and reduces redundancy.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.
Types of Inheritance in Java
Java supports the following types of inheritance:
Single Inheritance: One subclass inherits from one superclass.
Multilevel Inheritance: A class is derived from another derived class, forming a chain.
Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
Multiple Inheritance (through Interface): A class inherits from multiple interfaces since Java does
not support multiple inheritance using classes.
Hybrid Inheritance (through Interface): A combination of two or more types of inheritance,
achievable using interfaces.
6. Polymorphism
The word polymorphism means having many forms, and it comes from the Greek words poly
(many) and morph (forms), this means one entity can take many forms. In Java, polymorphism allows the
same method or object to behave differently based on the context, specially on the project's actual runtime
class.
Polymorphism means that the same operation may behave differently for different classes.
• For E.g. move operation behaves differently for a pawn than for the queen in a chess game
Types of Polymorphism
Polymorphism in Java is mainly of 2 types as mentioned below:
1. Compile-time Polymorphism(Method Overloading) :Achieved when multiple methods have the same
name but different parameters. The method call is resolved at compile time.
2. Runtime Polymorphism (Method Overriding ): Achieved when a subclass provides a specific
implementation of a method already defined in its superclass. The method call is resolved at runtime based
on the object
Association
Association is a relationship between two objects. In other words, association defines the multiplicity
between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these
words define an association between objects. Aggregation is a special form of association. Composition is a
special form of aggregation.
Example: A Student and a Faculty are having an association.
Generalization
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure
and behaviour are used from the specializtion to the generalized class. At a very broader level you can
understand this as inheritance. Why I take the term inheritance is, you can relate this term very well.
Generalization is also called a “Is-a” relationship.
Example: Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore
here the relationship between student and person, similarly faculty and person is generalization.
Association: This is the most general relationship, indicating that two classes have a logical
connection or communicate with each other. Objects in an association can exist independently. An
example is a Student and a Teacher; they interact, but one can exist without the other.
Aggregation: A specific form of association that models a "whole-part" hierarchy, but with a weak
ownership. The key is that the "part" object has its own independent lifecycle and can exist even if
the "whole" object is destroyed.
o Example: A Department has Professors. If the Department is removed,
the Professors (people) still exist.
o A stronger form of aggregation, composition, implies a critical dependency where the part
cannot exist without the whole (e.g., a House and a Room).
Weak Aggregation (Aggregation/Association):
o Definition: A "has-a" relationship where child objects can exist independently of the
parent.
o Lifespan: If the parent is destroyed, the child survives.
o UML: Represented by an open diamond ( ◇ ).
o Example: A Team has Players . If the team is deleted, players still exist.
Strong Aggregation (Composition):
o Definition: A "part-of" relationship where the child object cannot exist without the
parent.
o Lifespan: If the parent is destroyed, the child is also destroyed.
o UML: Represented by a filled diamond ( ◆ ).
o Example: A House has Rooms . If the house is destroyed, the rooms cease to exist.
Generalization: This is a conceptual process in the analysis and design phase where common
attributes and behaviors from multiple classes are abstracted into a higher-level superclass. It
structures a taxonomy of classes and represents an "is-a-kind-of" relationship.
o Example: Car, Truck, and Boat can be generalized into a Vehicle class, as they all share
common attributes like speed and behavior like move().
Inheritance: The concrete programming implementation of generalization. It allows a subclass to
inherit the features (attributes and operations) of its superclass, promoting code reuse and
polymorphism. When a Car class extends a Vehicle class in code, it is using inheritance to
implement the generalization relationship.
UML Class Diagram
A UML class diagram shows the structure of a system by displaying its classes, their attributes, methods,
and the relationships between them. It helps the team understand how the system is organized and how
components interact.
Represents classes, attributes, methods, and relationships
Helps communicate and document the software structure
Makes it easier for developers and designers to understand the system
UML Class Notation
Classes are depicted as boxes, each containing three compartments for the class name, attributes, and
methods.
1. Class Name: The name of the class is typically written in the top compartment of the class box and
is centered and bold.
2. Attributes: Attributes, also known as properties or fields, represent the data members of the class.
They are listed in the second compartment of the class box and often include the visibility (e.g.,
public, private) and the data type of each attribute.
3. Methods: Methods, also known as functions or operations, represent the behavior or functionality
of the class. They are listed in the third compartment of the class box and include the visibility (e.g.,
public, private), return type, and parameters of each method.
4. Visibility Notation: Visibility notations indicate the access level of attributes and methods. Common
visibility notations include:
+ for public (visible to all classes)
- for private (visible only within the class)
# for protected (visible to subclasses)
~ for package or default visibility (visible to classes in the same package)