MY mENU


Monday 28 January 2013

OOPS Concept


1. Encapsulation: the process of hiding internal data from the outside world; and accessing it only through publicly exposed methods is known as data encapsulation.
It means restricting direct access of class variables to outside class members and providing accessibility to those variables through public methods is called encapsulation.
In java encapsulation can be implemented. Ultimately through encapsulation we are hiding implementation details from user.
By declaring variables as private, to restrict direct access, and
By declaring one pair of public setter and getter methods to give access private variables.

2. Inheritance: the process of reusing class members from another class as if it were defined in that class is called inheritance. It can also be defined as it is a process of obtaining one object property to another object.
As the name suggests, inheritance means to take something that is already made. It is the concept that is used for reusability purpose.
Inheritance can be implemented in java by using below two keywords:
  • Extends
  • Implements

Extends” is used for developing inheritance between two classes or two interfaces, and “

"implements” keyword is used to developed inheritance between interface, class.

Class sample extends example{}

The class followed by extends keyword is called super class/base class, here example is super class, and the class that follows extends keyword is called subclass/derived class, here sample is sub class.

Basically by implementing inheritance we are establishing a relation between two classes and then extending one class scope to another class.

When subclass is loaded its entire super classes are loaded, and also when subclass object is created all its super class non-static variables memory is created. Invoked method is executed from  sub class.

Super class is loaded using extends keyword.

Super class object is created by calling super class constructor from sub class constructor using super() method.

Super() method is used to call super class constructor from sub class constructor to initialize super class non-static variables when subclass object is created. Compiler places super() method call in all constructors at the time of compilation if there is no super() or this() method call is placed explicitly.

Invoked super class constructor means not create an object of it, it means super class non static variables gets memory in subclass object region.

We can initialize super class non-static variables by using parameterized constructor so we must place “super” call explicitly with argument. The argument type should be the constructor parameter type.

Inheritance can only be implemented if super calss has a visible constructor. It means other than private constructor.

Empty Super Class:
    Class Example{}
             Class Sample extends Example{}

Super class with Explicit no-arg constructor::
Class Example{
Example (){}
}
             Class Sample extends Example{}

Super class with Explicit parameterized constructor::
Class Example{
Example (int a){}
}
             Class Sample extends Example{}

Super and sub class with Explicit parameterized constructor::
Class Example{
Example (int a){}
}
             Class Sample extends Example{
Sample(int b){}
}

Super is used to access the members of the super class. To access the hidden data variables of the super class hidden by the sub class. Super keyword is most useful to handle situations where the local members of a subclass hide the members of a super class having the same name.

Hence super keyword can be defined as “it’s a non-static variable used to store super class non-static member’s memory reference through current sub class object for separating super class members from subclass members”.

When we must define subclass for a class?
Sub classes must be written only if we want to extend the functionality of the existed class. To treat the new class as existed class type, for implementing runtime polymorphism.

What are the benefits of inheritance?
one of the key benefits of inheritance is to minimize the amount of duplicate code in an application by sharing common code amongst several subclasses. Where equivalent code exists in two related classes, the hierarchy can usually be refractored to move the common code up to a mutual super class. This also tends to result in a better organization of code and smaller, simpler compilation units.

Inheritance can also make application code more flexible to change because classes that inherit from a common super class can be used interchangeably. If the return type of a method is super class then the application can be adapted to return any class that is descend from that super class.

3. Polymorphism: having multiple forms of a method with same name is called polymorphism. The meaning of polymorphism is something like one name, many forms. One name may refers to different methods. We can develop polymorphism is using below concept.
Method overriding
Method overloading

Types of polymorphisms:

Compile-time polymorphism: when a method is invoked, if its method definition which is bind at compilation time by compiler and only executed by JVM at runtime, than it is called compile-time or static binding. Method overloading is the best example.

Run-time polymorphism: when a method is invoked, if its method definition which is bind at compilation time and is not executed by JVM at runtime, instead if it is executed from the subclass based on the object stored in the reference variable is called runtime polymorphism. It can only be implemented through method overriding and up-casting.
Run-time polymorphism not only provides automatic garbage collection it also allows you to change the behavior of a class without changing even a single line of code in a class. Hence we can say runtime polymorphism based application provides pluggability nature and means loose coupling.

What is meant by loose coupling and tight coupling?
If a product is functioning correctly even after changing one of its parts with another company given party, it is called loosely coupled way of manufacturing. Else it is called tightly coupled way of manufacturing.

4. Abstraction: the process of removing or hiding unnecessary information is called abstraction. The process of providing necessary method prototype by removing/ hiding unnecessary method logic/ body is called abstraction. Here unnecessary details mean method body and logic. It only provides us method prototypes and hides method implementation details.
Hiding non essential details can be developed using concrete methods.
Removing non essential details can be developed using abstract methods.

How can you force subclass to override super class method?
By creating method as abstract method

Procedure to create abstract methods in a class:
Create a method without body. A method created without  body is called abstract method. Rule of abstract method is it should contain abstract keyword and should ends with semicolon.
Abstract void m1();

Why method should has abstract keyword if it does not have body?
In a class we are allowed only to define methods with body. Since we are changing its default property- means removing its body- it must has abstract keyword in its prototype.
If method does not have body it should be declared as abstract using abstract modifier keyword else leads to CE:

If a class have abstract method, it should be declared as abstract class using abstract keyword else it leads to CE:

If a class is declared as abstract it cannot be instantiated violation to CE:

Why abstract class cannot be instantiated?
Because it’s abstract method cannot be executed. If compiler allows us to create object for abstract class, we can invoke abstract method using that object which cannot be executed by JVM at runtime. Hence to restrict calling abstract methods compiler does not allow us to instantiate abstract class.

We are not allowed to declare abstract method as static. It leads to compiler time error illegal combination of modifiers abstract and static.

Only three modifiers allowed with abstract they are:
Native, protected, public.

In projects development we come across three types of persons:

Service specifier—the person or company who develops specification is called service specifier.

Service provider—the person or company who implements specification is called service 
provider.

Service user—who uses the specification, further its implementation is called service user.

What we achieve via abstraction in java?
By implementing abstraction we can develop contract document between service providers, service users. This contract document is called specification, developed using interface in java.
This specification is defined by a common company with all abstract methods.
Specification provides the below information to both service provider and service user.

Why projects development is starts with interfaces?
To develop loosely coupled based  applications runtime polymorphism applications.
Loosely coupled or runtime polymorphism based application means it allows to replace an object with another object  of same category without recompiling and it exhibits the same expected functionality.