OOPS concept in Java with Real time examples

OOPS has been become one of the most asked questions in the Interview if you are going for the role of Tester or Developer. It plays the most important role as the examiner want the details briefly. We will be covering the OOPS concept in Java with Real-time examples in this article.

OOPS Concept in Java with Real-Time examples

OOPS Concept in Java

In Object-oriented Programming, we deal with Objects and classes. The main terms which are associated with the OOPS are  -
  • Object
  • Class
  • Inheritance
  • Abstraction
  • Encapsulation

Object-Oriented Programming

Object-oriented programming helps us to model real-life objects well. Every real-life object can be modelled into classes and then instances can be formed of them in the form of objects. OOP languages also have features like inheritance, encapsulation, abstraction, and Polymorphism.


Object

Any entity which is having some state and behaviour is an Object, for example, A car has some colour, size, shape and it has some behaviour as speed, mileage or automatic gear system. We can consider a car as an Object and a Vehicle as a class. An Object is a physical as well as a logical entity which means it utilizes some memory and has some state and behaviour. Whereas a Class in just a logical entity having no memory.

Class

Like said, a class is the collection of objects. Objects hold some memory whereas class doesn't. Classes in java always start with an uppercase letter.

Inheritance

When an object of subclass acquires the properties of the object from Parent class. This methodology is known as Inheritance. It is efficient when we need not write the same code over and over again, instead, we re-use the code. It is also used to obtain run-time polymorphism.

Multiple inheritance is not possible in java through class but using interface it is possible.

Polymorphism

To achieve polymorphism we have a concept to do the same task in multiple ways. For example, we can convince a customer in multiple ways by offering different deals based on his needs. Polymorphism can be achieved in two ways - Method overloading and method overriding.

Abstraction

Abstraction can be achieved by using an Interface or abstract class. or by using access modifiers like Public, Private and Protected. Abstraction means to hide, to show only the essential details while hiding the other details. For example - we use different jars having different classes and their methods, but the code is not known to the users as to how it works in the background.

Abstraction can be achieved by using the Abstract Class or Abstract Method.

Abstract Class

A Class that is defined Äbstract is called as an Äbstract Class. It can have Abstract methods as well as concrete methods. A normal class cannot have abstract methods.

Abstract Method

A method without a body is known as an Äbstract Method. It must be declared in an abstract class. 

An important rule to remember - If a normal class extends the Abstract class, then it must implement all the abstract methods of the parent class.

The advantage of using abstraction is to reduce the complexity of the code.

Difference between Abstraction and Encapsulation

Abstraction and Encapsulation



Encapsulation

Integrating the code or data together into a single unit is called encapsulation. Any java class is an example of encapsulation having different data types.

There are a few more terms which are used in contrast to Object-Oriented programming, these are not used comprehensively but the sound knowledge of these terms will put you one step ahead of your competitor.

Coupling

Like the name sounds, when one class is dependent on another class. We can say coupling exists between class A and class B.

Why did the OOPS come into the picture and why is it successful?

coding looks manageable when the project grows.

There exists one more term which interviewers use to confuse the candidates, difference between object-based programming and object-oriented programming. Object-based programming has no concept of inheritance, for example, Javascript and VB Script.

Difference between Abstract Class and Interface

Abstract class and Interface

Few questions you must know before appearing for an interview -

Difference between compile-time polymorphism and runtime polymorphism

Compile-time polymorphism is implemented through function overloading where the same function can be used again and again but using parameters of the different data type. Whereas Runtime polymorphism can be achieved using function overriding.

Function overriding

When a child class function overrides the base class function, it is called a function overriding.

Difference between Static and Final

The static keyword is used to define the class member that can be used independently of any object of that class, Whereas final is used to declare a constant variable, a method which cannot be overridden and a class which cannot be inherited. 




Comments

Popular posts from this blog

Azure Tutorials Series - Azure Networking

Testing in CI/CD

Azure Tutorials Series - IaaS vs PaaS vs SaaS