Java OOP(Object Oriented Programming) Concepts
Java OOP(Object Oriented Programming) Concepts
Programming) Concepts
Last Updated : 07 Mar, 2025
Example:
// Use of Object and Classes in Java
import java.io.*;
class Numbers {
// Properties
private int a;
private int b;
// Methods
public void sum() { System.out.println(a +
b); }
Output
3
-1
Java 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. This includes classes for objects
occurring more than once in your code. In general,
class declarations can include these components in
order:
Example:
// Class Declared
public class GFG {
// Properties Declared
static String Employee_name;
static float Employee_salary;
// Methods Declared
static void set(String n, float p) {
Employee_name = n;
Employee_salary = p;
}
// Main Method
public static void main(String args[]) {
GFG.set("Rathod Avinash", 10000.0f);
GFG.get();
}
}
Output
Employee name is: Rathod Avinash
Employee CTC is: 10000.0
class Student {
// Properties Declared
int id;
String name;
// Printing Student
public void printStudent()
{
System.out.println("Id:" + id);
System.out.println("Name:" + name);
}
}
class GFG {
public static void main(String[] args)
{
Student obj = new Student();
obj.id = 1;
obj.name = "ABC";
obj.printStudent();
}
}
Output
Id:1
Name:ABC
4 Pillars of Java OOPs Concepts
1. Abstraction
Example:
// abstract class
abstract class GFG {
// abstract methods declaration
abstract void add();
abstract void mul();
abstract void div();
}
2. Encapsulation
Example:
// Setter methods
public void set_id(int empid) {
this.empid = empid;
}
public void set_name(String ename)
{
this.ename = ename;
}
// Getter methods
public int get_id() {
return empid;
}
Output
Employee id: 78
Employee Name: John
To learn more about topic refer to Encapsulation in
Java article.
3. Inheritance
Example:
// base class or parent class or super class
class A{
// parent class methods
void method1(){}
void method2(){}
}
4. Polymorphism
Example:
sleep(1000) //millis
sleep(1000,2000) //millis,nanos
Types of Polymorphism
1. Method Overloading
2. Method Overriding
// Parent Class
class Parent {
// Method Declared
public void func(){
System.out.println("Parent Method func");
}
// Method Overloading
public void func(int a){
System.out.println("Parent Method func "
+ a);
}
}
// Child Class
class Child extends Parent {
// Method Overriding
@Override
public void func(int a){
System.out.println("Child Method " + a);
}
}
// Main Method
public class Main {
public static void main(String args[]){
Parent obj = new Child();
obj.func(4);
}
}
Output
Child Method 4
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.