OOP's Concept in JAVA
OOP's Concept in JAVA
As the name suggests, Object-Oriented Programming or Java OOPs concept refers to languages that use
objects in programming. The different characteristics of an Object-Oriented Programming Language are
as follows,
1. Class
2. Object
3. Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
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.
Example:
Create a class named "Main" with a variable x:
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.
Example:
Create an object called "myObj" and print the value of x:
public class Main {
int x = 5;
3. Abstraction:
Data abstraction is the process of hiding certain details and showing only essential information
to the user. Abstraction can be achieved with either abstract classes or interfaces. 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 behaviors of an object differentiate it from other
objects of similar type and also help in classifying the object.
Example:
An abstract class can have both abstract and regular methods:
4. Encapsulation:
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To
achieve this, you must:
Declare class variables/attributes as private.
Provide public get and set methods to access and update the value of a private variable.
Example:
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}
}
5. Polymorphism:
Polymorphism is considered one of the important features of Object-Oriented Programming.
Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means
many and “morphs” means forms.
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two types:
Compile-time Polymorphism
Runtime Polymorphism
Method Overloading
When there are multiple functions with the same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by changes in the number of
arguments or/and a change in the type of arguments.
Example:
class Helper {
// Main class
class Main {
public static void main(String[] args)
{
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
Method overriding
Method overriding, on the other hand, occurs when a derived class has a definition for one of
the member functions of the base class. That base function is said to be overridden.
Example:
class Parent {
void Print()
{
System.out.println("parent class");
}
}
class Main {
Single Inheritance:
Example:
import java.io.*;
import java.lang.*;
import java.util.*;
// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}
Multilevel Inheritance:
Example:
import java.io.*;
import java.lang.*;
import java.util.*;
Hierarchical Inheritance:
Example:
class A {
public void print_A() {
System.out.println("Class A");
}
}
class B extends A {
public void print_B() {
System.out.println("Class B");
}
}
class C extends A {
public void print_C() {
System.out.println("Class C");
}
}
class D extends A {
public void print_D() {
System.out.println("Class D");
}
}
// Driver Class
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
Hybrid Inheritance:
Example:
class C
{
public void disp()
{
System.out.println("C");
}
}
class A extends C
{
public void disp()
{
System.out.println("A");
}
}
class B extends C
{
public void disp()
{
System.out.println("B");
}
class D extends A
{
public void disp()
{
System.out.println("D");
}
public static void main(String args[]){