OOPs Concept in Java
OOPs Concept in Java
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception Handling Java Programs Java Project
OOPS in Java
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
Got It !
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 1/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
Object Oriented
Programming (OOPs)
1. Class
2. Object
3. Method and method passing
4. Pillars of OOPs
Abstraction
Encapsulation
Inheritance
Polymorphism
Compile-time polymorphism
Runtime polymorphism
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 5/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
Java
Output
Pillar 1: Abstraction
Data Abstraction is the property by virtue of which
only the essential details are displayed to the user.
The trivial or non-essential units are not displayed to
the user. Ex: A car is viewed as a car rather than its
individual components.
Data Abstraction may also be defined as the process
of identifying only the required characteristics of an
object, ignoring the irrelevant details. The properties
and
We use cookies to ensure you have behaviors
the best of an object
browsing experience differentiate
on our website. it site,
By using our from youother
acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 9/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
Java
//abstract class
abstract class GFG{
//abstract methods declaration
abstract void add();
We use cookies to ensure you have the best browsing experience
abstract voidonmul();
our website. By using our site, you acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 10/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
abstract void div();
}
Pillar 2: Encapsulation
It is defined as the wrapping up of data under a single
unit. It is the mechanism that binds together the code
and the data it manipulates. Another way to think
about encapsulation is that it is a protective shield
that prevents the data from being accessed by the
code outside this shield.
Java
Pillar 3: Inheritance
Inheritance is an important pillar of OOP (Object
Oriented Programming). It is the mechanism in Java by
which one class is allowed to inherit the features
(fields and methods) of another class. We are
achieving inheritance by using extends keyword.
Inheritance is also known as “is-a” relationship.
Demonstration of Inheritance :
Java
Pillar 4: Polymorphism
It refers to the ability of object-oriented programming
languages to differentiate between entities with the
same name efficiently. This is done by Java with the
help of the signature and declaration of these entities.
The ability to appear in many forms is called
polymorphism.
E.g.
Java
sleep(1000) //millis
sleep(1000,2000) //millis,nanos
2. Overriding
Example
Java
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum().
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum().
// This sum takes two double parameters
public double sum(double x, double y)
We use cookies to ensure you have the best browsing
{ experience on our website. By using our site, you acknowledge that you have read
return
and understood (x +Policy
our Cookie y); & Privacy Policy
} Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 15/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20,
30));
System.out.println(s.sum(10.5,
20.5));
}
}
Output
30
60
31.0
Conclusion
The Object Oriented Programming (OOPs) concept in
Java is a powerful way to organize and write code. It
uses key ideas like classes, objects, inheritance,
polymorphism, encapsulation, and abstraction to
create flexible and reusable code. By using the Java
OOPs concept, programmers can build complex
We use cookies to ensure you have the best browsing
applications experience
more on our website.
efficiently, makingBy using
theourcode
site, youeasier
acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
to manage, understand, andSkip modify. Overall, Java’s
to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 16/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 18/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
R Risha…
Similar Reads
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
Skip to content
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 20/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
Databases
Preparation Competitive More Free Online Write & Earn
SQL Corner Exams Tutorials
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
Tools Write an Article
MYSQL Improve an
and understood our Cookie Policy & Privacy Policy JEE Advanced
PostgreSQL
Software Typing Test
Article
Skip to content UGC NET Development Image Editor
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 23/24
6/26/24, 11:51 AM Object Oriented Programming (OOPs) Concept in Java - GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read
and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/ 24/24