Hello there, I hope you and your loved ones are doing great in these unprecedented Times of COVID-19.
In this blog, I have tried to cover one of the most common asked Concepts in all the Tech Interviews i...e OOPS. I'll be explaining all the building blocks of OOPS and Advantages of OOPS.
Let's get started!!!!
SO First what is basically OOPs (Object-Oriented Programming System) is ????
Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts which is defined below
The 4 main pillars of OOPS are:
- Inheritance
- polymorphism
- Encapsulation
- Abstraction.
I'll discuss each in great detail here.
What is an Object ???
An object is a real-world entity that has 3 Characteristics:
State: Represents the data (value) of an object.
Behaviour: represents the behaviour (functionality) of an object such as deposit, withdrawal, etc.
Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
What is Class ???
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
The class has the following Characteristic:
How to Declare the Class and what is Inheritance???
Practical Implementation
Now Finally we gonna discuss INHERITANCE.
When one object acquires all the properties and behaviours of a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.
what the heck is this Parent(Super) class??
Sub /Child Class: A class that inherits the other class. It is also called a derived class, extended class, or child class.
Super /Parent Class: A class from where a subclass inherits the features. It is also called a base class.
For Example, Your parents act as a base class and you will act as a base class...As in the real world, you can take/use whatever your parents own but they can't use what you own.
Same here Base class can access all the methods defined in Parent Class..but the Parent Class can't access methods defined inside Base Class.
The syntax of Java Inheritance.
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that u r making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
we'll try to code this Scenario.
Code:
OUTPUT:
Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
It's of 2 types:
- Method Overloading .
- Method Overriding.
Method Overloading If a class has multiple methods having the same name but different in parameters, it is known as Method Overloading.
Method Overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent classes, it is known as method overriding.
ENCAPSULATION:
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.
ADVANTAGES:
- It provides you with the control over the data.
- It is a way to achieve data hiding.
- The encapsulate class is easy to test. So, it is better for unit testing.
ABSTRACTION:
Hiding internal details and showing functionality is known as abstraction. EX: phone call, we don't know the internal processing.
In Java, we use abstract class and interface to achieve abstraction. (Will be discussing how to achieve Abstraction...stay connected)
That's it for Now.....in the Next blog I'll be discussing how to Acheive Abstraction in java and we'll be going in complete details of what Abstraction is....till then stay healthy and safe..
Thanks for reading it out....For more in-depth Java Tutorials Follow me on:
Twitter : twitter.com/arsh14_ali