What are the main object-oriented programming concepts in JAVA.

Kaveesh Manuja
5 min readDec 21, 2022

--

Object-oriented programming started with the Simula language since 1967, and also added information hiding to ALGOL. Likewise influential object-oriented language was Smalltalk since 1980, in which a program was a set of objects that interacted by sending messages to one another. However Object-oriented languages help to manage complexity in large programs and programming easier by allowing a programmer to think about each part of the program in isolation. Java has been one of the most attractive and successful language in 1990s to now.

There are six concepts, is that true?

1. HAS-A and IS-A Relationship

2. Overloading

3. Polymorphism

4. Abstraction

5. Encapsulation

6. Association, Agrégation and Composition

1. HAS-A and IS-A Relationship

HAS-A

In Java, a Has-A relationship is otherwise called composition. It is additionally utilized for code reusability in Java. And also in Java, a Has-A relationship essentially implies that an example of one class has a reference to an occasion of another class or another occurrence of a similar class. For instance, a vehicle has an engine, a dog has a tail, etc. In Java, there is no such keyword that executes a Has-A relationship. Yet, we generally utilize new catchphrases to actualize a Has-A relationship in Java. Has-a relationship is composition relationship which is a productive way of code reuse.

e. g. (Dog has a flea)

IS-A

IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance and also Inheritance is uni- directional. For instance, Orange is a Fruit, Van is a Vehicle etc.

e.g. (Van is a Vehicle.)

2. Overloading

Method overloading is a concept of Java in which we can create multiple methods of the same name in the same class, and all methods work in different ways and also when more than one method of the same name is created in a Class, this type of method is called the Overloaded Method. (Same name different parameter list and Smallest Possible)

e.g.

3. Polymorphism

Polymorphism is one of the core concepts of object-oriented programming (OOP) and describes situations in which something occurs in several different forms. In computer science, it describes the concept that you can access objects of different types through the same interface. Each type can provide its own independent implementation of this interface.

e.g. (Animal is a superclass with a method named animal Sound (). Animal subclasses include pigs, cats, dogs, and birds, each with its unique animal sound implementation.)

4. Abstraction

Data Abstraction is the property by virtue of which only the essential details are displayed to the user and may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.

Abstract classes and Abstract methods:

· An abstract class is a class that is declared with an abstract keyword.

· An abstract method is a method that is declared without implementation.

· An abstract class may or may not have all abstract methods. Some of them can be concrete methods

· A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make the subclass itself abstract.

· Any class that contains one or more abstract methods must also be declared with an abstract keyword.

· There can be no object of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator.

e.g.

5. Encapsulation

Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of its own class in which it is declared and also Encapsulation is used to ensure that “sensitive” data is concealed from users. Private variables are only accessible inside the same class (an outside class has no access to it). However, if we give public get and set methods, we can get access to them.

e.g.

6. Association

Usually, Association is a connection or relationship between two separate classes. It shows how objects of two classes are associated with each other. The Association defines the multiplicity between objects. It can be one-to-one, one-to-many, many-to-one, many-to-many.

There are two types in Association:

· Aggregation

· Composition

Aggregation

Aggregation in Java is a special kind of association. It represents the Has-A relationship between classes. Java Aggregation allows only one-to-one relationships.

e.g. (Man has-a car)

Composition

The composition is another form of aggregation which is considered as the restricted form of Association. When the entities are completely dependent on each other, unlike the aggregation. Composition allows for one-to-many relationships between objects.

e.g. (Man has-a heart)

--

--

Kaveesh Manuja
0 Followers

Without data, we cannot do a single task.