OBJECT ORIENTED PROGRAMING IN JAVA

OBJECT ORIENTED PROGRAMING IN JAVA

Introduction:

Java is an object oriented programming language which is designed to program using classes and objects.

An object is desgined as an entity which has state and behavior and a object is an instance of a class. Eg a building (Object) which is constructed based on a blueprint. 

Here: the state is defined by the variable in the class and the methods defines the object’s behavior.

A class is a blueprint which defines the state and behavior to its instance ie., Object. Eg. The blueprint of a building which has all the information of a building.

Inheritance:

Inheritance a technique which a sub/child class acquire all the properties of the parent/super class.

It is achieved by using "extends" keyword.

class Vehicle{

  int noOfWheels;

  int capacityOfFuelTank;

  String fuelType;

  public void start(){

    System.out.println("Vroom Vroom");

  }

}

// here vehicle is a super class. It has all the common properties of a vehicle.

class MotorCycle extends Vehicle{

  int topSpeed;

  int mileage;

}

This class inherits the vehicle super class by using extends keyword and this class also has some properties of its own.

class Car extends Vehicle{

  boolean isAc;

  boolean sunScreen;

}

Like the MotorCycle class, this Car class also inherits the vehicle super class by using extends keyword and this class also has some properties of its own.

Type of Inheritance:

No alt text provided for this image

There are 4 types,

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Multiple Inheritance is not supported in Java which make Interface a powerful one.

Polymorphism:

Polymorphism, Like the word suggest one action is performed in many ways.

Types :

  • Compile time polymorphism
  • Run time polymorphism

Compile time polymorphism :

Class Main{

public static void main(String[] args)

{

add(5.0,6.0);

add(5,6);

}

public static void add(int i, int j) {

System.out.println(i+j);

}

public static void add(double i,double j)

{

System.out.println(i+j);

}

}

Here there are 2 methods with the same name. The compiler decides which method to call based on the type of argument / number of argument.

In the first case it decides to call the second method and the second calls the first method respectively.

This is also called as method overloading.

There are two ways to overload the method in java

  • By changing number of arguments
  • By changing the data type

Run time polymorphism :

class Vehicle{

  int noOfWheels;

  int capacityOfFuelTank;

  String fuelType;

  public void start(){

    System.out.println("Vroom Vroom");

  }

}

// here vehicle is a super class. It has all the common properties of a vehicle.

class MotorCycle extends Vehicle{

  int topSpeed;

  int mileage;

public void start(){

System.out.println("ta ta ta troom");

}

}

public static void main(String[] args){

Vehicle myBike = new MotorCycle();

myBike.start();

}

The myBike.start() call the method from the MotorCycle class. This is decided during the run time which method to be called ie., (Method Dispatch).

This is also called as method overriding. And follows these below points

  • The method must have the same name as in the parent class.
  • The method must have the same parameter as in the parent class.

Abstraction:

The process of hiding the implementation and showing the functionality. An abstract class is created using abstract keyword. An abstract class cannot be instantiated like a normal class. A class does need to inherit the abstract class and instantiate an object.

An abstract class may have abstract method, while an interface only have abstract method.

Eg. While using self-start in a motorcycle. It just starts. Inside a big process goes.

There are 2 ways to achieve abstraction

abstract class and interface.

Eg:

abstract class Vehicle{

  public void start();

}

class MotorCycle extends Vehicle{  

void start(){

System.out.println("Started Sucessfully");}  

public static void main(String args[]){  

 Vehicle myBike = new MotorCycle();  

 myBike.run();  

}  

}  

Encapsulation:

The process of wrapping data to prevent the accessing of data which is achieved by using access modifiers.

Access modifiers:

  • Public (The variable or method can be accessed between different packages)
  • Default (The variable or method can be accessed within a packages)
  • Protected (The variable or method can be accessed within a package and outside the package through child class.)
  • Private (The variable or method can be accessed within one Class)

class MotorCycle extends Vehicle{

  private int topSpeed;

  int mileage;

public void start(){

System.out.println("ta ta ta troom");

}

}

public static void main(String[] args){

Vehicle myBike = new MotorCycle();

myBike.topSpeed = 0; // this is not possible because of private keyword.

myBike.start();

}

mostly we use getters and setters to change/ access the private data.

Static Keyword:

Static keyword is used for memory management mainly. Static keyword can be used with variables, methods, blocks. The static keyword belongs to the class than an instance of the class.

We cannot access a static method from a non static method using polymorphism.

It changes how run time polymorphism works.

Final Keyword:

The Final keyword in java is used to restriction. Final keyword can be used along variable, method, class. The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

Conclusion

Object oriented programming is a powerful concept used by programming language to mimic the real life entities. Classes and objects are the fundamental building blocks of OOP in Java. Classes define the properties and methods of an object, while objects are instances of classes that have a state and behavior. We can write code that is easier to understand, test, and maintain over time.

To view or add a comment, sign in

More articles by Vignesh Muniyappan

  • Real-Time Web Communication Simplified with WebSockets and STOMP

    In today's web applications, real-time communication is a must. Users expect instant updates and interactive features.

  • Spring Boot - P5

    Title: Building User Registration and Login System using Spring Boot Introduction: In this tutorial, we'll guide you…

  • Angular - P1

    What is Angular? Angular is a popular open-source web application framework developed by Google. It's used for building…

  • Spring boot P-4

    An Introduction to Spring Boot MVC In the realm of web development, Spring Boot has emerged as a powerful framework…

  • SpringBoot - P3

    Exploring Annotations in Springboot: Introduction: Annotations play a pivotal role in modern Java programming…

  • SpringBoots - P2

    What and Why Spring Initializer? Spring Initializer is a web tool for quickly creating Spring Boot projects. It offers…

  • SpringBoots - P1

    What Is SpringBoots? A popular framework to build Java applications. Built on Spring Framework.

  • Manual Testing vs Automation Testing

    What is Software Testing? Process of evaluating and verifying that a software product or application does what it is…

    3 Comments
  • Introduction to Servlet and JSP Development

    Web development forms the backbone of the modern digital world. From simple websites to complex web applications…

  • Normalization in DBMS

    What is Normalization? The process by which a bigger dataset is further broken down into well-structured smaller…

Others also viewed

Explore content categories