OOP's Concept
_____________________________
_____________________________
Hiding of data, so that outside person can't access data directly, by using private modifier we can implement data hiding.
class Account {
private double bal = 1000;
}
the main advantage of data hiding is we can achieve security.
Hiding internal implementation details and highlight the necessary set of service to users is called Abstraction, by using interfaces and abstract classes we can achieve abstraction. the main advantage of abstraction are -
Wrapping up data and corresponding method (behavior) into a single unit is known as encapsulation. if any java class follows data hiding and abstraction such type of class is said to encapsulated class. (Encapsulation = Data Hiding + Abstraction)
class Account {
private double bal;
public double getBal() {
return bal;
}
public void setBal(double bal) {
this.bal = bal;
}}
Hiding data behind method is the central concept of encapsulation, the main advantage of using encapsulation are -
Note: If parent class is not tightly encapsulated then no child class is tightly encapsulated
A class is tightly encapsulated, when every data member declared as private, it improves the modularity of application.
It also know as inheritance, by using extends keyword we can implement inheritance concept, In inheritance child class acquires all the properties of super class, and this is the main advantage so that we can achieve code re-usability.
Note: Java don't provide multiple inheritance concept but through interface it is possible
It also known as composition or aggregation, there is no specific keyword to implement Has-A Relationship, mostly we use 'new' keyword, main advantage to use this concept is code re-usability but it increases dependency between the class and creates maintenance problem.
In the case of composition, whenever object container destroys then all objects gets destroy automatically. there is no chance of exists of object without existing object container, Container and contained object having strong association.
In the case of aggregation, whenever object container destroys, there is no guarantee of destruction of contained objects. without existing object container there may be chance to exists objects. object container just maintains reference of contained objects, this is called weak association which is nothing but aggregation.
If methods names are same but arguments are different then it is known as overloading. having overloading concept simplifies the programming
In overloading, method resolution always take care by compiler based on reference type. hence overloading is also considered as compile time polymorphism, static polymorphism or early binding reference type plays very important role and runtime object will become dummy overriding
In overriding, method names and arguments must be same, i.e method signature must be same until 1.4 version return type must be match but from 1.5 version onward co-variant return types are applicable, co-variant return type concept is applicable only for object type but not for primitive types.
It is also known as run time polymorphism, dynamic polymorphism or late binding, overriding method resolution is also known as dynamic method dispatch
We can't override final method, private method.
All rules of method hiding are same as overriding except these following difference -
Method Hiding
Overriding
Overloading
Overriding
Poly represents 'many' and morphs represents 'forms', we can use same name to represent multiple forms in polymorphism. There are two types of polymorphism
Compiler Time - Overloading, Method Hiding
Run Time - Overriding
Note: Three pillar of OOPs concept is Inheritance for code re-usability, Encapsulation for security, Polymorphism for flexibility
At the time of class loading if we want to perform any activity, we have to define that activity inside static block because static block will be executed at the time of class loading. within a class we can take any number of static block but all these static block will be executed from top to bottom
Object creation is not enough, we have to perform initialization then only object can provide response properly. whenever we are creating an object some piece of code execute automatically to perform initialization, this piece of code is nothing but constructor, hence the main objective of constructor is to perform initialization for the newly created object.
For any java class if we are allowed to create only one object such type of class is called singleton class. eg. Runtime, ActionServlelt (Structs), BusinessDeligate (EJB), ServiceLocation(EJB)
The main advantage of singleton class is instead of creating a separate object for every requirement we can create single object and reuse the same object for every requirement this approach improves memory initialization and performance of the system.
Runtime r1 = Runtime.getRuntime();
Runtime r2 = Runtime.getRuntime();
Runtime r100 = Runtime.getRuntime();
Runtime is a class and getRuntime is a method
we can create our own singleton class also for this we have to use private constructor and factory method.
By using class name if we call any method and return same class object, then that method consider as factory method.
The first line inside a constructor should be either super() or this(), with the constructor we can use any of them
this() - To call current class constructor
super() - To call parent class constructor
compiler provides super() as default
A class can contain more than one constructor with same name with different arguments, these constructor are consider as overloaded constructor, Inheritance and overriding concepts are not applicable for constructor, every class including abstract class can contain constructor but interface can't have constructor
Note: If the parent class contain some constructor then while writing child class we have to give special care to constructor, if parent class throws some checked exception then compulsory child class constructor should throw same checked exception or its parent otherwise the code will not be compile
Parent class reference can be used to hold child class object eg. Parent p = new child(); similarly, interface reference can be used to hold implemented class object eg. Runnable r = new thread();
syntax A b = (C) d; A and C is class or interface, b is reference variable and d is object reference or object
C and type of d must have some relationship (either parent to child or child to parent or any other) otherwise we will get compile time error 'inConvertable types found d type but required C TYPE', in type casting we are just converting only type of object not object itself.
The degree of dependency between the components is called coupling, it reduces maintainability and doesn't promote re-usability
For every component a clear well defined functionality is defined such type of component is called high cohesion, without effecting remaining components we can modify any component hence enhancement will become very easy, it improves maintainability of the application and promotes re-usability of the code
Note: Loose coupling and high cohesion is good for application.