Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaFour Main Object Oriented Programming Concepts of Java

Four Main Object Oriented Programming Concepts of Java

Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are listed below. TheseĀ conceptsĀ aim to implement real-world entities in programs.

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Abstraction is a process of hiding implementation details and exposes only the functionality to the user. In abstraction, we deal with ideas and not events. This means the user will only know ā€œwhat it doesā€ rather than ā€œhow it doesā€.

There are two ways to achieve abstraction in Java

  1. Abstract class (0 to 100%)
  2. Interface (100%)

Real-Life Example: A driver will focus on the car functionality (Start/Stop -> Accelerate/ Break), he/she does not bother about how the Accelerate/ brake mechanism works internally. And this is how the abstraction works.

Certain key points should be remembered regarding this pillar of OOPS as follows:

  • The class should be abstract if a class has one or many abstract methods
  • An abstract class can have constructors, concrete methods, static method, and final method
  • Abstract class canā€™t be instantiated directly with the new operator. It can be possible as shown in pre tag below:
A b = new B();
  • The child class should override all the abstract methods of parent else the child class should be declared with abstract keyword

Example:

Java




// Abstract class
public abstract class Car {
Ā Ā Ā Ā public abstract void stop();
}
Ā Ā 
// Concrete class
public class Honda extends Car {
Ā Ā Ā Ā // Hiding implementation details
Ā Ā Ā Ā @Override public void stop()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Honda::Stop");
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Mechanism to stop the car using break");
Ā Ā Ā Ā }
}
Ā Ā 
public class Main {
Ā Ā Ā Ā public static void main(String args[])
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Car obj
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new Honda(); // Car object =>contents of Honda
Ā Ā Ā Ā Ā Ā Ā Ā obj.stop(); // call the method
Ā Ā Ā Ā }
}


Pillar 2: Encapsulation

Encapsulation is theĀ process of wrapping code and data together into a single unit.

Real-Life Example:

A capsule which is mixed of several medicines. The medicines are hidden data to the end user.

In order to achieve encapsulation in java follow certain steps as proposed below:

  • Declare the variables as private
  • Declare the setters and getters to set and get the variable values

Note: There are few advantages of encapsulation in java as follows:

  1. Control Over Data: We can write the logic in the setter method to not store the negative values for an Integer. So by this way we can control the data.
  2. Data Hiding: The data members are private so other class canā€™t access the data members.
  3. Easy to test: Unit testing is easy for encapsulated classes

Example:

Java




// AĀ JavaĀ classĀ whichĀ isĀ aĀ fullyĀ encapsulatedĀ class.
publicĀ class Car
{
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // privateĀ variableĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā privateĀ StringĀ name;
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // getterĀ methodĀ forĀ nameĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā publicĀ StringĀ getName()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  returnĀ name;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // setterĀ methodĀ forĀ nameĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā publicĀ voidĀ setName(StringĀ name)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  this.name = name;Ā Ā 
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā 
}
Ā Ā 
Ā Ā 
// JavaĀ classĀ toĀ testĀ theĀ encapsulatedĀ class.Ā Ā 
public class Test
{
Ā Ā Ā Ā Ā Ā  publicĀ staticĀ voidĀ main(String[]Ā args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // creatingĀ instanceĀ ofĀ theĀ encapsulatedĀ classĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā CarĀ car
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newĀ Car();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // settingĀ valueĀ inĀ theĀ nameĀ memberĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā car.setName("Honda");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // gettingĀ valueĀ ofĀ theĀ nameĀ memberĀ Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(car.getName());
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā 
}


Pillar 3: Inheritance

Inheritance is the process of one class inheriting properties and methods from another class in Java. Inheritance is used when we have is-a relationship between objects. Ā Inheritance in Java is implemented usingĀ extendsĀ keyword.

Real-life Example:

The planet Earth and Mars inherits the super class Solar System and Solar system inherits the Milky Way Galaxy. So Milky Way Galaxy is the top super class for Class Solar System, Earth and Mars.

Let us do discuss the usage of inheritance in java applications with a generic example before proposing the code. So consider an example extending the ExceptionĀ class to create an application-specific Exception class that contains more information like error codes. For exampleĀ NullPointerException.

There are 5 different types of inheritance in java as follows:

  1. Single Inheritance: Class B inherits Class A using extends keyword
  2. Multilevel Inheritance: Class C inherits class B and B inherits class A using extends keyword
  3. Hierarchy Inheritance: Class B and C inherits class A in hierarchy order using extends keyword
  4. Multiple Inheritance: Class C inherits Class A and B. Here A and B both are superclass and C is only one child class. Java is not supporting Multiple Inheritance, but we can implement using Interfaces.
  5. Hybrid Inheritance: Class D inherits class B and class C. Class B and C inherits A. Here same again Class D inherits two superclass, so Java is not supporting Hybrid Inheritance as well.

Example:

Java




// super class
class Car {
Ā Ā Ā Ā // the Car class have one field
Ā Ā Ā Ā public String wheelStatus;
Ā Ā Ā Ā public int noOfWheels;
Ā Ā 
Ā Ā Ā Ā // the Car class has one constructor
Ā Ā Ā Ā public Car(String wheelStatus, int noOfWheels)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā this.wheelStatus = wheelStatus;
Ā Ā Ā Ā Ā Ā Ā Ā this.noOfWheels = noOfWheels;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // the Car class has three methods
Ā Ā Ā Ā public void applyBrake()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā wheelStatus = "Stop" System.out.println(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Stop the car using break");
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // toString() method to print info of Car
Ā Ā Ā Ā public String toString()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return ("No of wheels in car " + noOfWheels + "\n"
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "status of the wheels " + wheelStatus);
Ā Ā Ā Ā }
}
Ā Ā 
// sub class
class Honda extends Car {
Ā Ā 
Ā Ā Ā Ā // the Honda subclass adds one more field
Ā Ā Ā Ā public Boolean alloyWheel;
Ā Ā 
Ā Ā Ā Ā // the Honda subclass has one constructor
Ā Ā Ā Ā public Honda(String wheelStatus, int noOfWheels,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Boolean alloyWheel)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // invoking super-class(Car) constructor
Ā Ā Ā Ā Ā Ā Ā Ā super(wheelStatus, noOfWheels);
Ā Ā Ā Ā Ā Ā Ā Ā alloyWheel = alloyWheel;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // the Honda subclass adds one more method
Ā Ā Ā Ā public void setAlloyWheel(Boolean alloyWheel)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā alloyWheel = alloyWheel;
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā // overriding toString() method of Car to print more
Ā Ā Ā Ā // info
Ā Ā Ā Ā @Override public String toString()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return (super.toString() + "\nCar alloy wheel "
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + alloyWheel);
Ā Ā Ā Ā }
}
Ā Ā 
// driver class
public class Main {
Ā Ā Ā Ā public static void main(String args[])
Ā Ā Ā Ā {
Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Honda honda = new Honda(3, 100, 25);
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(honda.toString());
Ā Ā Ā Ā }
}


Pillar 4: Polymorphism in javaĀ 

Polymorphism is the ability to perform many things in many ways. The word Polymorphism is from two different Greek words- poly and morphs. ā€œPolyā€ means many, and ā€œMorphsā€ means forms. So polymorphism means many forms. The polymorphism can be present in the case of inheritance also. The functions behave differently based on the actual implementation.

Real-life Example:

A delivery person delivers items to the user. If itā€™s a postman he will deliver the letters. If itā€™s a food delivery boy he will deliver the foods to the user. Like this polymorphism implemented different ways for the delivery function.

There are two types of polymorphism as listed below:

  1. Static or Compile-time Polymorphism
  2. Dynamic or Run-time Polymorphism

Static or Compile-time Polymorphism when the compiler is able to determine the actual function, itā€™s called compile-time polymorphism. Compile-time polymorphism can be achieved by method overloading in java. When different functions in a class have the same name but different signatures, itā€™s calledĀ method overloading. A method signature contains the name and method arguments. So, overloaded methods have different arguments. The arguments might differ in the numbers or the type of arguments.

Example 1: Static Polymorphism

Java




public class Car{
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā public void speed() {
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā public void speed(String accelerator) {
Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā public int speed(String accelerator, int speedUp) {
Ā Ā Ā Ā Ā Ā Ā Ā return carSpeed;
Ā Ā Ā Ā }
}


Dynamic or Run-time Polymorphism occurs when the compiler is not able to determine whether itā€™s superclass method or sub-class method itā€™s called run-time polymorphism. The run-time polymorphism is achieved by method overriding. When the superclass method is overridden in the subclass, itā€™s called method overriding.

Example 2: Dynamic Polymorphism

Java




import java.util.Random;
Ā Ā 
class DeliveryBoy {
Ā Ā 
Ā Ā Ā Ā public void deliver() {
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Delivering Item");
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā DeliveryBoy deliveryBoy = getDeliveryBoy();
Ā Ā Ā Ā Ā Ā Ā Ā deliveryBoy.deliver();
Ā Ā Ā Ā }
Ā Ā 
Ā Ā Ā Ā private static DeliveryBoy getDeliveryBoy() {
Ā Ā Ā Ā Ā Ā Ā Ā Random random = new Random();
Ā Ā Ā Ā Ā Ā Ā Ā int number = random.nextInt(5);
Ā Ā Ā Ā Ā Ā Ā Ā return number % 2 == 0 ? new Postman() : new FoodDeliveryBoy();
Ā Ā Ā Ā }
}
Ā Ā 
class Postman extends DeliveryBoy {
Ā Ā Ā Ā @Override
Ā Ā Ā Ā public void deliver() {
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Delivering Letters");
Ā Ā Ā Ā }
}
Ā Ā 
class FoodDeliveryBoy extends DeliveryBoy {
Ā Ā Ā Ā @Override
Ā Ā Ā Ā public void deliver() {
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Delivering Food");
Ā Ā Ā Ā }
}


Output

Delivering Letters
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?