OOPS Concepts in JAVA

On Social Media

Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a
methodology or paradigm to design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:


o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation

  1. Object: Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

An object has three characteristics:
o state: represents data (value) of an object.
o behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
o identity: Object identity is typically implemented via a unique ID. The value of the ID is not
visible to the external user.

Every Object is an instance of a class. 

2. Class: A class is a group of objects which have common properties. It is a template or blueprint from which
objects are created. It is a logical entity. It can’t be physical.

A class in Java can contain:

o fields
o methods
o constructors
o blocks
o nested class and interface

Syntax to declare a class:

  1. class classname{  
  2.     field;  
  3.     method;  }

3. Inheritance: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. 

Terms used in Inheritance:

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

The syntax of Java Inheritance:

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

Types of inheritance in java:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance

4. Polymorphism: Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word “poly” means many and “morphs” means forms. So polymorphism means many forms.

There are mainly two types of polymorphism are as follows:

a) Compile time polymorphism

b) Runtime polymorphism

a) Compile time Polymorphism: Compile-time polymorphism is also called static polymorphism. It allows users to define multiple methods with the same name but different parameters. It is beneficial because it helps write cleaner and more efficient code. 

Method Overloading: In method overloading, the methods with the same name can have different numbers of parameters, different types of parameters, or different order of parameters in methods. 

// Java program to demonstrate working of method 
// overloading in Java 

public class Sum { 
	// 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) 
	{ 
		return (x + y); 
	} 

	// 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

b) Runtime Polymorphism: Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.

Method Overriding: If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

class Vehicle{  
  void run(){System.out.println("Vehicle is running");}  
}  
//Creating a child class  
class Bike extends Vehicle{  
  public static void main(String args[]){  
  //creating an instance of child class  
  Bike obj = new Bike();  
  //calling the method with child class instance  
  obj.run();  
  }  
}  
Output:
Vehicle is running

5) Abstraction: Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Abstract class in Java

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of Abstract class that has an abstract method as follows :

abstract class Bike{  
  abstract void run();  
}  
class Honda4 extends Bike{  
void run(){System.out.println("running safely");}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  
}  

Output:
running safely

6) Encapsulation: Encapsulation in Java is a process of wrapping code and data together into a single unit.

Advantages of Encapsulation are as follows:

Example of java Encapsulation:

class Area {

  // fields to calculate area
  int length;
  int breadth;

  // constructor to initialize values
  Area(int length, int breadth) {
    this.length = length;
    this.breadth = breadth;
  }

  // method to calculate area
  public void getArea() {
    int area = length * breadth;
    System.out.println("Area: " + area);
  }
}

class Main {
  public static void main(String[] args) {

    // create object of Area
    // pass value of length and breadth
    Area rectangle = new Area(5, 6);
    rectangle.getArea();
  }
}

Output: 
Area: 30

On Social Media

Leave a Reply

Your email address will not be published. Required fields are marked *