Object-Oriented Programming in Java with Examples

Introduction

Java is a high-level object-oriented programming language, which is widely used for building software applications. It was introduced by James Gosling and a team of researchers at Sun Microsystems in 1995. The initial purpose of introducing Java was to create a platform-independence language for consumer electronic devices such as set-top box, televisions, and embedded devices.

In this article, we’ll cover the core concept in Java programming that is object-oriented programming that organizes software design around data, or object rather than functions and logic. It aims at implementing real-world entities in a program by modeling things as objects with data and behaviors.

What is Object-Oriented Programming in Java?

Java object-oriented programming is a way of designing programs using classes and objects. A class is a template or blueprint for creating an object and an object is a fundamental, self-contained unit of code that combines data and functions that operate on that data.

Classes and objects can be explained using real-life examples like a book represent a class (blueprint), a specific book represent an object, and properties like the name of the author, title of the book and number of pages represent attributes.

Object Oriented Programming in Java

Why Java is based on Object-Oriented Programming?

  • Code Reusability: Inheritance allows new classes to reuse the logic and methods of existing ones.
  • Data Security: Encapsulation and abstraction provide data hiding and restrict direct access to internal data.
  • Flexibility and Scalability: Polymorphism allows objects of different classes to be treated as objects of a common superclass.
  • Collaboration: The modular nature of OOP fosters smoother collaboration on large projects.

Important Concepts of Object-Oriented Programming

1. Class and Object

A class is a blueprint for creating an object and an object is a real-world instance of a class.

Example:

class industry {
   String name;
   double rating;
   
   void display() {
       System.out.println("Name of the film: " + name);
       System.out.println("Rating of the film: " + rating);
    }
}
public class Main {
   public static void main(String[] args) {
       industry f1 = new industry(); // object creation
       f1.name = "Titanic";
       f1.rating = 8.2;
       f1.display();
   }
}

Output:

Name of the film: Titanic
Rating of the film: 8.2

2. Encapsulation

Encapsulation refers to the mechanism of wrapping of variables and methods into a single unit. It provides data hiding, better control over values, and increased security. Given below is an example of performing encapsulation.

Example

class industry {
  private String name;
  private double rating;

  public void setName(String name) {
       this.name = name;
  }

  public void setRating(double rating) {
       this.rating = rating;
  }

  public String getName() {
       return name;
  }

  public double getRating() {
       return rating;
  }
}
public class Main {
   public static void main(String[] args) {
       industry s = new industry();
       s.setName("Titanic");
       s.setRating(8.2);

       System.out.println(s.getName());
       System.out.println(s.getRating());
   }
}

Output:

Titanic
8.2

3. Inheritance

Inheritance in java is the mechanism of inheriting the properties of one class (parent class) by another class (child class). It promotes code reusability and extend keyword is used to declare the child class to indicate which parent class it is inheriting from. Given below is the program in Java to perform inheritance.

Example

class Automobile {
   void drive() {
       System.out.println("Vehicle is Automobile");
   }
}
class tata extends Automobile {
   void brand() {
       System.out.println("Brand of a car is Tata");
   }
}
public class Main {
   public static void main(String[] args) {
       tata obj = new tata();
       obj.drive(); // inherited method
       obj.brand(); // own method
   }
}

Output:

Vehicle is Automobile
Brand of a car is Tata

4. Polymorphism

Polymorphism is the core concept in object-oriented programming that allow single object to take many forms. It enables a single method to be performed in different ways in different scenarios. It is relatable to real-life examples like a person behaving as a father to his son, a husband to his wife, a teacher to his students and as a friend among his colleague.

Polymorphism promotes code reusability, making the code more flexible and maintainable. Polymorphism is implemented in Java through method overloading and method overriding. Given below is the description of two implementation with examples.

Method Overriding Example:

When a subclass provides a specific implementation for a method that is already defined in its parent class, it is called method overriding.

Example

class Student {
   int getMarks() {
       return 67;
   }
}
class school extends Student {
   int getMarks() {
       return 50; 
   }
}
public class Main {
   public static void main(String[] args) {
       Student obj = new school();
       System.out.println(obj.getMarks());
   }
}

Output:

50

Method Overloading Example:

This feature allows a class to have multiple methods with the same name but different parameters. Method overloading can be explained with a real-life example such as the camera of a smart phone that captures photos but it also has different parameters like nightmode, 1080px, FullHD, portraiMode, etc.

Example

class MathOperation {
   int add(int a, int b) {
       return a+b;
   }
   int add(int a, int b, int c) {
       return a+b+c;
   }
}
public class Main {
   public static void main(String[] args) {
       MathOperation obj = new MathOperation();
       System.out.println(obj.add(5, 10));
       System.out.println(obj.add(5, 10, 15));
   }
}

5. Abstraction

Abstraction is the process of hiding complex internal information from the user and knowing only the required knowledge of using that tool. Abstraction can also be explain with a real-life example like for driving a car, a driver knows about acceleration, break, cluth, and shifting gears and knowledge of how the engine works, the mechanism of gear changing and internal working of car is not required. Given below is an example of abstraction.

Example

abstract class Vehicle {
   abstract void start();
}
class Car extends Vehicle {
   void start() {
       System.out.println("Car starts with key");
   }
}
public class Main {
   public static void main(String[] args) {
       Vehicle obj = new Car();
       obj.start();
   }
}

Advantages of OOP

  1. Code reusability
  2. Easy maintenance
  3. Improved Security
  4. Scalability for large systems

Conclusion

Object-Oriented Programming is the core concept of Java programming based on classes, objects, encapsulation, inheritance, polymorphism, and abstraction. These concepts allow Java developers to build robust, secure, and scalable applications. Mastering the concept of Object-Oriented Programming is essential for anyone who wants to become a professional developer in Java.

Best Platforms to Learn Coding for Kids and Teens
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment