2 OOPS Concepts Overview

OOPS concepts in the simplest way for VIVA.

[ ENCAPSULATION: Wrapping up of characteristics and behavior into a single unit known as 'class' is called Encapsulation. ]

[ OBJECT: You can imagine an object as a pointer to a structure variable in C.

They are very similar to each other.

Working with objects have more functionality.

Object is just a reference.

It does not contain any value.

It points to the memory location that holds the values.

The allocation of memory is done using 'new' operator.

The constructor which follows the new operator helps in assigning the memory places for each data members for the new operator in the 'heap' part of primary memory.

Note: Everything in Java is Dynamic memory Allocation.

Note: Here, Garbage is automatically collected. ]

[ CLASS: The blueprint from which objects are created.

Objects of a Class has access to 'EVERYTHING' in that class.

Class is similar to data type.

Sometimes, it is also referred to as an 'Abstract data type'.

Everything in Java must belong to a class. Except obvious stuff like import statements, package statements, etc. ]

[ CONSTRUCTOR: Each class has a constructor by default.

This is known as Default Constructor.

You can initialize data members in the Constructor.

You can have parameterized constructor.

You can overload constructors.

You can use 'this' keyword for accessing data members and member functions of the object accessing the function using '.' dot operator depending on their access specifier.

Eg,

this.a = 5;

this.method();

You can use 'super' keyword for accessing data members and member functions of the super class of the class from which the object is created depending on the access specifier.

Eg,

super.a;

super.method();

You can use this(ParameterList) to call constructor of the same class.

You can use super(ParameterList) to call Constructor of the super class.

Make all constructor public. It is a good programming practice. ]

[ ACCESS SPECIFIERS:

Refer to that table given by sir. Note that you need to import when using members of classes of other packages.]

[ INHERITANCE: When one class is 'born from' another.

There are two classes here.

Super class/Parent class/Base class: The class 'from' which the characteristics and behaviors are being 'inherited'.

Sub class/Child class/Derived class: The class 'to' which the characteristics and behaviors are being 'inherited'.

Basically, Sub class can access data members, objects, methods and constructor of the Super class and not the other way around. ]

[ COMPOSITION: When one class 'contains' or 'comprises of' other classes. It can be done by instancing. ]

[ AGGREGATION: When one class(aggregate class) has a reference to another class. It is said that the aggregate class has the ownership of the another class. ]

[ METHOD OVERLOADING: A class can have more than one method having the same name, if their argument lists are different. ]

[ METHOD OVERRIDING: A subclass or child class CAN provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

So when an object of a subclass is created and the method is called, the method implemented by the subclass is called. Not the one in the super class. The one in super class is hidden. ]

avataravatar