OOPS
OOPS
OOPS
Abstraction (ATM)
Encapsulation
Encapsulation: the process of binding data members and method into a Single unit is
encapsulation.it is a combination of data hiding and Abstraction.
Note: a class is said to be tightly encapsulated if and only if each And every variable declared as
private.Whether class contains corresponding getter () or setter () Methods or not and whether
these methods are declared as public or not These things we are not required to check.
Note2: if the parent class is not tightly encapsulated than no child Class will be tightly
encapsulated.
E.g.: university consist of several departments without existing university there is no chance of
existing departments. Hence university and department are strongly associated and this strong
association is nothing but composition
Aggregation: without existing container object if there maybe chance of existing contain objects
than container and contained objects are weekly associated and this weak association is nothing
but aggregation
E.g.: professor and departments
IS-A Versus Has-A relationship
If we want total functionality of a class automatically. Than we should go for IS-A relationship
Eg : student IS- A Person
If we want partial functionality of a class. Than we should go for has-A relationship
Eg: student HAS-A name, id etc
Method Signature
In java method signature consist of method names followed by argument types only.
Note: Compiler will use method signature to resolve method calls
A class cannot have two methods with same method signature in same class
Method Overloading:
(early binding: static binding: compile type polymorphism)
In C language method overloading concept is not available hence we can’t declare multiple
methods with same name but different args type. If there is change in args type compulsory we
need to go for new method which increases complexity of programing
Note: in overloading compiler is responsible to perform method resolution based on reference
type. Runtype object doesn’t play any role in method overloading
Byte—short—int—long—float—double
Char--int--long—float—double
This process is called automatic promotion in overloading
Case 2:
While resolving overloaded methods compiler will always keep the precedence for child type
argument when compared with parent type argument methods
Case 3
It will result in compile time error: reference to m1 is ambigious since the String arg method and
StringBuffer args method both are at same level
public class OverloadingAutoPromotion {
public static void main(String[] args) {
// TODO Auto-generated method stub
OverloadingAutoPromotion o = new OverloadingAutoPromotion();
char c = 10;
o.m1(null);
}
o.m1(10,10): The method m1(int, float) is ambiguous for the type OverloadingAutoPromotion
public class OverloadingAutoPromotion {
public static void main(String[] args) {
// TODO Auto-generated method stub
OverloadingAutoPromotion o = new OverloadingAutoPromotion();
char c = 10;
o.m1(10,10);
o.m1(10f,10f);
}
}
class Monkey extends Animal{
}
class Test12345{
}
Overriding: ONLY APPLICABLE FOR METHODS NOT TO VARIABLES
Note: Method resolution always taken care by JVM based on run time object and hence
overriding is also considered as run time polymorphism or dynamic polymorphism or late binding.
JVM will check if the runtime object is parent object or child object
8. Exceptions: if child class method throws any CHECKED EXCEPTION compulsory Parent
class method should throw the same CHECKED EXCEPTION or its Parent. Otherwise we
will get compile time error. But there are no restrictions for unchecked exception
9. Static:
a. We can’t override a static method as non-static otherwise we will get compile time
error.
b. Similarly we cannot override a non-static method as static
c. Method hiding. : If both parent and child class method are static than we won’t
get any compile time error. It seems overriding concept applicable for static
methods but it is not overriding but it is method hiding.
10. Method hiding: All rules of method hiding are exactly same as overriding except the
following differences
S.No. Method hiding Overriding
1 Both parent and child Both parent and child
method should be static. method should be non-static.
We can override vararg method with another vararg method only. If we are trying to override
with normal methods than it will become OVERLOADING but not OVERRIDDING.
IF we replace child method with vararg method than it will become OVERIDDING.
Variable Resolution:
Always takes care by compiler based on reference type. Irrespective of whether the variable is
static or non-static ( overriding concept applicable only for methods but not for variables)
Note :
In overloading we have to check only method names (must be same) and argument types(must
be different). We are not required to check remaning like return types access modifier etc.
But in overriding every thing we have to check like method names, arg types, return types, access
modifiers, throws class, etc
Note: static and abstract combination not allow in methods.
Polymorphism:
Parent class ref can be used to hold child object. But by using that reference we can call only the
methods available in parent class and we can’t call child specific methods.
BUT by using child reference we can call both parent and child class methods.
The above components is said to be tightly coupled with each other because dependency
between the components is more.
Tightly coupling is not a good programing practice. Because it has several serious disadvantages.
Note: for every component a clear well defined functionality is defined than that component is
said to be follow high cohesion.
It is always a good programing practice.