How to implement OOPS concepts in Java?

Konfinity
March 13,2021 - 9 min read
How to implement OOPS concepts in Java?

This blog is dedicated to give a brief and concise details of the implementation of the OOPS concept in Java. However, before going into details, we need to understand the concept of object-oriented programming itself. In this blog, we are going to follow a hybrid approach where we understand the concept of an OOPS principle and then proceed to their implementation in Java. Let’s get started!

Object-Oriented Programming or OOPs refers to a programming style or paradigms that uses objects and aims to implement real-world entities like inheritance, abstraction, polymorphism among others. OOPs is a widely regarded and popular concept because it binds together the data and their functions so that no other part of the code can access that particular data. The main concept of OOPS are Polymorphism, Inheritance, Encapsulation, Abstraction. In today’s blog, we are going to learn in detail about the different characteristics of an Object-Oriented Programming language along with their implementation in Java. Let’s begun our journey by understanding Polymorphism.

  • Polymorphism

Polymorphism is the ability of OOPS to efficiently differentiate between entities with the same name. The implementation of Java in polymorphism is done by the signature and declaration of these entities. Let us take an example of implementation of polymorphism in Java.

  • public class Sample {
  • // This is our first function where we take two integer parameters
  • public int add (int i, int j)
  • { return (i + j); }
  • public int add (int i, int j, int k)
  • // This is our second function sum () where we take three int parameters
  • {
  • return (i + j + k);
  • }
  • public double add (double i, double j)
  • // This is our second function sum () where we take two double parameters
  • {
  • return (i + j);
  • } }

The above was the main Java program that describes Polymorphism. There is a class by the name of Sample which contains three methods with the same name. We will see in the driver code that the program still compiles and runs successfully. Let’s write our main code also.

  • public static void main (String args[])
  • {
  • Sample s = new sample ();
  • System.out.println(s.add(100, 200));
  • System.out.println(s.add(100, 200, 300));
  • System.out.println(s.add(100.5, 200.5));
  • }
  • }

The output of the program above will be -

300

600

301.0

In Java, polymorphism is implemented in 2 ways. The first type is overloading and second is overriding in Java. It is time we move on to our next OOPS principle.

  • Inheritance

Inheritance is another aspect of object-oriented Programming by which one class is allowed to inherit the features of another class. There are some significant terminologies associated with inheritance, let’s understand some of them.

Super Class is the class whose features are inherited. It is also known as the base or the parent class. Another feature is the sub class, it is the class that inherits features from a particular class, it is known as derived, extended class or child class. This subclass can also add its own fields and methods in addition to the inherited or derived ones.

One important concept supported by inheritance is reusability. It simply means that when a new class has to be created, it can be derived from an existing class and reuse some of the code or particular fields and methods of the existing class. In Java, the keyword used for inheritance is ‘extends’. Let’s look at a simple syntax to create a class.

class child-class extends parent-class

  • {
  • //methods and fields
  • }
  • Encapsulation

Encapsulation is the mechanism of OOPS that binds together code and the data it manipulates. Encapsulation is like a protective shield that prevents the data from being accessed by the code outside this shield. In other words, data of one class is hidden from any other class. It can only be accessed through any member function of the class in which they are originally declared. Encapsulation is also known as data hiding as the data in one particular class is hidden from other classes. Talking about its implementation, encapsulation is achieved by setting all the variables in the class as private and writing public methods in the class to set and get the values of variables.

  • Abstraction

After encapsulation, we must progress to data abstraction in OOPS. With the help of abstraction, only the essential details are displayed to the user and the rest is not. For example, a car is not viewed as its individual components. It is basically the process of identifying only the required characteristics of an object and ignoring the irrelevant details.

We can understand abstraction with the help of an example of a driving car. The person driving a car only knows that accelerators are used to increase the speed of car and brakes are used to stop the car. However, the user does not know that the inner mechanism by which the accelerator speeds up a car or a break stops a car. This is the exact concept of abstraction. Implementation of abstraction in Java is done by interfaces as well as abstract classes.

With this we have discussed the basic concept and implementation of the major pillar of Object-oriented programming in Java, that is, encapsulation, inheritance, abstraction and polymorphism. It is now time; we discuss some crucial and widely used terms in object OOPS.

  • Terms to Know

One of the most important terms in OOPS is Class. It is widely known as a user defined blueprint or a prototype from which objects are created. A class represents a set of properties as well as methods and these are common to all objects of the class. When declaring a class, there are a lot of components that come in use. Let’s understand each of them.

The first one is modifier which sets class to have either public or default access. The second is the class name which should always begin with a letter. Next thing is the name of superclass or the parent class if there is any. It is preceded by the keyword ‘extends’. Another is a comma-separated list of interfaces, if there is any. It is preceded by the keyword ‘implements.’ After these keywords and components comes the body of the class surrounded by curly braces.

After the class is defined, the object of a class comes into picture. Objects are widely known as the basic unit of Object Oriented Programming. It basically represents real life entities.

Many objects are created in a general Java program which interact by invoking methods. An object has a state, behaviour and identity. The state of an object is represented by attributes of an object and reflects the properties of an object. An object also has a behaviour that defines the response of an object. It is represented by methods of a class. The identity of the object is the unique name of the object.

A method performs a specific task and might return the result to the caller. They are used to reuse the code and eliminates the need to retype the same code again. In the implementation of classes in Java, one important thing to note is that every method must be part of some class. The concept of method saves time as we are able to reuse the code without retyping.

When we discuss methods, method declaration also comes into picture. Method declarations has an access modifier, name of the method, exception list, parameter list and the body of the method. W have enlisted the basic definition of each of these components in brief.

An access Modifier is used to specify that from where the method can be accessed in the application. There 4 type of the access specifiers in Java, that is, public, protected, private and default. If a method is public, it means that it can be accessible in all classes of the application. Protected means that it can be accessed only in its subclasses or within the package in which it is defined. Private means that the method can be accessed only in the class in which it is defined. Also, if the method is declared or defined without using any modifier, it means that it can be accessed within same class and package in which it is defined.

The return type of the method is the data type of the value returned by the method and it is void if the method does not return any value. Also, the name of the method should follow all the rules and conventions of the language it is defined in.

In the components of a class, we talked about parameter and exception list of a method. The parameter list is a comma separated list of the input parameters which is preceded with their data type, within the enclosed parenthesis and there is a set of empty parentheses if there are no parameters. Similarly, the exception list contains some general or obvious exceptions that a method can throw. Lastly, the body of the method is enclosed between braces and contains the code that needs to be executed.

The blog above was a compilation of all the basic principles of OOPS and their implementation in Java along with general terms used in this programming paradigm. We hope you found it useful, if you did, we expect you to check out our other blogs and augment your technical knowledge. Keep learning, keep growing!

Also, if you wish to start your journey in web development, we have a professional web development course that will address all your concerns regarding web development and help you ace your skills. Make sure you check out a course that can be a stepping stone to your dream of becoming a proficient web developer.

In case you didn’t know, Konfinity ‘s Web Development Course is developed by experts from IIT DELHI in collaboration with tech companies like Google, Amazon and Microsoft. It is trusted by students and graduates from IIT, DTU, NIT, Amity, DU and more.

Join the course to master the art of creating web applications by learning the latest technologies, right from basic HTML to advanced and dynamic websites, in just a span of a few months.

Konfinity is a great platform that helps you launch a lucrative tech career. We will get you started by helping you get placed in a high paying job. You know what’s amazing - no prior coding experience is required to take up our courses. Start your free trial here .