Object-Oriented Programming and Java OOP Concepts

Dan Dias Abeyesinghe
Javarevisited
Published in
6 min readJul 12, 2021

--

Photo by Fotis Fotopoulos on Unsplash

Object-oriented programming is a fundamental programming paradigm used by almost every developer. This is considered to be one of the most famous programming paradigms that is taught to a programmer as a standard way of coding from the early days of their careers.

The concept of OOP is a concept that can be seen in our day to day lives but what we fail to see or make a note of it. Object-Oriented Programming basically deals with real-life objects and how they are incorporated and replicated in the programming world. This concept mainly relies on the concept of Classes and Objects.

Classes

Classes can be simply defined as simple, abstract code which acts as a blueprint to create different instances of an object of that respective class, it can also be termed as the broader concept from which objects are instantiated. These classes have attributes and functions which are inherited to the objects when instantiating. These methods are only available to that respective Object type and not for other types.

Objects

Objects can be defined as an instance of a Class. An Object can contain attributes and relevant methods that are in the Class and it is mostly a software representation of real-life objects.

Attributes

Attributes are the information that is stored in the class. These attributes will hold data of the Objects that are instantiated and the data held by these attributes can differ from object to objects that are created using the respective class.

Methods

Methods represent the behavior of a Class. They can perform actions or even update Objects of the respective Class.

If I was to explain the concepts of Classes and Objects through a real-life example, we can take pet, and to be more precise we can take a dog, a dog has common attributes that are common to all the dogs like the Breed, Age, Size, Color, and some functionalities that common as well like Bark, Run, Sit, Eat. So, in order to represent this real-life model in a software entity we can use the dog as Class which acts as blueprint of which dog objects can be created from.

As attributes of the Class, we can assign the dog attributes mentioned above and we can do the same by assigning the methods of the class the functionalities of the dog which we discussed earlier.

OOP Concepts

There are 3 main Object-Oriented Concepts when It comes to Object-Oriented Programming. These Can be simply listed as,

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

Abstraction

Abstraction can be simply explained as the form of hiding all unnecessary data and only shows the essential data of an Object. A-Class is the perfect example for an abstract representation of an Object as it will only showcase the relevant fields and methods of the class and will omit the unwanted details of an Object.

Example:

In the above example, the Dog class that was created contained characteristics and functionalities of all dogs. Here the Class Dog serves as an abstract model for the concept for the dog.

Encapsulation

Encapsulation can define as the process of connecting and binding together the data and objects and keeps them safe from outside interference and misuse. This can be implemented or achieved by making the fields of the class Private and providing access to those fields using public methods. The below code snippet will make you understand this more clearly.

In the above code snippet, we can see that all the attributes of the class are made private, hence these attributes cannot be accessed directly outside of the class. To access them these getters and setter methods. The setter methods initialize or change the value of an attribute and getters return the value of an attribute.

Access Modifiers

Java supports 4 types of access modifiers that we can use to define the visibility of classes, attributes or even methods. We can use these modifiers according to our logic and the requirements of the system.

Inheritance

Inheritance can be defined as the process where one objects acquire the properties of another. with the use of this information is made manageable in a hierarchical order. This concept helps in reducing duplication of code by taking out the common codes in multiple classes and assigning it to a one super class.

There are 3 types of inheritance

1. Single Inheritance

2. Hierarchical Inheritance

3. Multiple Inheritance

However Multiple inheritances cannot be implemented in Java due to the Diamond problem.

In the above code snippet, we can see that the GermanShepherd Class is created by extending the Dog class, by doing this the sub-class which is the GermanShepherd Class inherits all the attributes and methods of the parent class which is the Dog class in this instance. Here the constructor of the parent class is mentioned as “super” in the subclass. The number / types of parameters passed to super must match parameters of one of the superclass’s constructors. If present, must be the first statement.

Polymorphism

Polymorphism can be defined as the process which allows different objects to react to the same message in different ways.

This can be achieved through method Overloading and Method Overriding.

Method Overloading

Method Overloading is the process of methods having the same method name but different types of parameters and number of parameters. These methods may or may not have return types. See the code below for better understanding.

This above code snipper depicts the use of Method overloading. Here the same method with the same name is used but with different parameters and number of parameters. The complier treats overloaded methods as different methods altogether and the complier knows which method to call based on the number and type of parameters used.

Method Overriding

Method Overriding is the process that is used if a subclass wants to override a method defined in the super class. This can be done by writing the same exact method in the sub class and changing the body of the method accordingly. The method written in the sub class to override the method in the super class should have the same method name, same arguments, same return types as the method of the super class.

In this above code snippet, we can see that the Bark () method is overridden in the GermanShepherd. At the method call, the method defined in the subclass is invoked and executed instead of the one in the superclass.

I hope this article helped you to get a better understanding of all the basics and concepts explained. Please do feel free to provide feedback on the above. That would be much appreciated.

Thank you!

--

--