CH 5 Oops
CH 5 Oops
==========
*ABSTRACTION :-
--------------
>Hiding internal impl and just highlight the set of services, is called
abstraction.
>by using using abstract classes and interfaces we can impl abstraction.
Ex: ATM GUI -> bank people are highlighting only the set of services .
ADVANTAGE OF ABSTRACTION:-
--------------------------
> we achive security.
> provide flexibility to end user, improve modularity, and maintainablilty.
Note:-Every data member (vars) declared as private and for every var or mem
maintain getter and setter methods.
*ADVANTAGE OF ENCAPSULATION:-
-----------------------------
>achiving security.
Note:- if we extends any class in herarchy , and if any parent class var not
declare private var that extended by our class
then our class is also not tight encapsulated class (mean all class should
have private var.)
IMP NOTE:-
1-whatever parent has by default availble to child
but child data not availble to parent.
and using child ref we can call both class data child and parent.
but using parent ref can call only to parent methods but can not call to child
method.
2-parent class ref can be used to hold child class object but by using that ref we
can call only parent class mem but
we can not call child specific methods.
3-child class ref can not be used to hold parent class object.
Note:- but a interface can extends any no of interfaces at a time , so its provide
support for multiple inheritance through interface.
Note:-if our class does not extends any class then our class is direct child class
of object class.
if our class extends any class then its indirect child class of object , and
which forms multilevel inheritance.
OR
** METHOD SIGNATURE:
--------------------
>consist of method name with argument types.
ex:
public void mehtodAdd(int i, float f)
{
}
Note:- in same class , we can not declare 2 method with same signature else get
compile time error.
**POLYMORPHISM :-
-----------------
>a method with same name with diff forms is concept of polymorphism.
ex: abs() method for int, long , float.
OOPS : PILLARS-
---------------
1- inheritance is talks to reusability.
2- polymorphism talks to flexibility.
3- encapsulation talks to security.
byte - > short -> int -> long -> float - double
|
char
Note:- in resolving overloaded, exact match will always get first priority,
in resolving overloaded, child class will get more prority than parent
class.
CASE 3: in general, VAR-ARGS- method will get less priority that is no other method
matched then only var-arg method get chance.
CASE 4: in overloading , runtime object will not play any role in overloading.
** OVERRIDING :
---------------
> Overriding occurs when a subclass provides its own implementation of a method
that is already defined in its superclass.
The method in the subclass must have the same name, return type, and parameter
list as the method in the superclass.
> Whatever parent has by default avaible to child through inheritance,
but if child is not satisfy then child allowed to re_define that parent class
method in child in its own way , this process is called overriding.
Note:- Co-variant return type concept is applicable only for object type not for
primitive.
Note:- private methods are not visible in child classes hence overriding concept is
not applicable for private method.
base on our requirment we can declare same parent private method in child it
is allowed , but it is not overriding.
Note:- parent class final method can not be override in child class.
Note:- parent class final method we can override as final in child class. we can
also override native methods in child.
Note:- we can override a non abstract method as abstract.
Note:- syncronized, strictfp will not has any restriction on overriding.
Note:- not allowed: final -->non final but non-final -> final is allowed.
native <====> non native, abstract <====> non abstract,
syncronized <====> non syncronized,
strictfp <====> non strictfp.
>the exception which are not checked by compiler are called un-checked exceptions.
ex: RuntimeException and its child, + Error and its child are unchecked
exeception.
except / remaining are checked exception.
*OVERRIDING RULE:- while overriding if the child class method throws any checked
exception compulsory the parent class method should throw same checked exception.
else we get C.E
but
There is no restrictions on un-checked exceptions.
Note:- if we overriding static to static then its not overriding , its mehtod
hiding.
**METHOD HIDING:-re defining same parent class static method in child class.
-----------------
overriding hiding
-----------------------------------------------------------------------------------
------------
1.both non static 1.both static
2.take care by jvm 2.take care by compiler
3.runtime/dynamic/latebinding
3.compiletime/static/early binding.
-----------------------------------------------------------------------------------
------------
OVERLOADING OVERRIDING
-----------------------------------------------------------------------------------
----------------------------------
1. method name must be same 1. Must be same
2. argument type diff 2. must be same.
3. method signature must be diff 3. must be same.
4. return type no restriction 4. same or co-variant.
5. private, static, final can not be overloaded. 5. also can not be
overriden.
6. no restriction on Access Modifiers 6. weak / reducing is not
allowed.
7. no restriction on throws clause 7. parent method should
throw same checked exception as child,
not restiction for un-
checked.
8.also called compiletime/static/early binding 8.also called
runtime/dynamic/late binding.
-----------------------------------------------------------------------------------
-------------------------
imp note:
1 : in overloading , we have check only same method name , and args diff ,
remaining things like return type extra not req.
but
2 : in overriding , we check everything like mehtod names, args, return type,
throws , modifiers.
**IMP:
Q-IN HOW MANY WAYS WE CAN CREATE OBJECT OR WAYS TO GET OBJECT ?
------------------------------------------------------------------
ANS:-
1- by using new operator:
ex: Test t = new Test();
3- by using Clone()
ex: Test t1 = new Test();
Test t2 = (Test)t1.clone();
5- by using Deserialization:
ex: FileInputStream fis = new FileInputStream("abc.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Test t = (Test)ois.readObject();
**CONSTRUCTOR:
==============
>When we create an object some piece of code will be executed automatically to
perform initialization of an object
this piece of code is called "constructor".
>main objective of constructor is to perform initialization of object.
IMP NOTE:- whenever we r writing any arg const , its highly recommended to write no
arg const also.
ex:
class Contructor
{
int rollNo;
String name;
public String toString()
{
return "rollNo : "+rollNo +"\n" +"Name : "+name;
}
Contructor(int rollNo, String name)
{
this.rollNo=rollNo;
this.name=name;
}
public static void main(String[] args)
{
Contructor cn = new Contructor(1010,"BL");
Contructor cn2 = new Contructor(1020,"BL Gocher");
System.out.println(cn +"\n"+cn2);
//System.out.println(cn);
}
}
Default Constructor:- if we r not writing at least one const then cmpiler will
generate default const.
if we r writing at least one const, then compiler not
generate default const.
Note: class contains either compiler generated const or programmer written const
but not both.
** SUPER() VS THIS():
======================
super() is used to call a parent class constructor from a subclass constructor.
this() is used to call another constructor within the same class.
>first line inside every const is super() or this() , if we r not writing anything
compiler will place super();
case1: super() or this() in first line.
case2: we can use super() or this() at a time, but not both.
case3: we can use only inside constructor , if out then error.
--------------------------------------------------------------------------
**OVERLOADING CONSTRUCTOR:
--------------------------
> a class can contain more than one constructor and all these const having same
name but diff args , called construcor overloading.
Note: overriding concept not used for constructor but constructor can be
overloaded.
Note: abstarct class contains const for child class object creation to perform
initialization of child object only.
Note: whenever we are creating child class obj then parent class const will be
executed.
Nested call:- calling a method inside another method is called nested call.
Recursive call:- calling a method within same method is called recursive call.
IMP NOte:- whenever we r writing any arg const , its highly recommended to write no
arg const also
Note:- if parent class const throws some checked exception then compulsory child
class const should throw same checked excetion or its parent.
** SINGLETON CLASSES:-
=====================
> For any java class if we allow to create only one object , class is called
sigleton class.
ex: Runtime class
ActionServlet
ServletLocator
BusinessDelegate
ex :
Runtime r1=Runtime.getRuntime();
Runtime r2=Runtime.getRuntime();
Runtime r3=Runtime.getRuntime();
if(r1==r2) // true
if(r1==r3) // true
class Test
{
private static Test t=null; // 1 static var
private Test() // 2 private constructor
{}
public static Test getTestObject()//3 factory method returning Test class
object if null.
{
if(t==null)
{
t=new Test();
}
return t;
}
}
class Singleton
{
public static void main(String[] args)
{
//Test t = new Test();
//Test t2= new Test();
System.out.println(Test.getTestObject().hashCode()); // 1579210
System.out.println(Test.getTestObject().hashCode()); // 1579210
}
}
Note:- we can also create xxxTon class by increasing static var and adding if
condition.
Q- if class is not final, how we can restrict or not allow to create child class
object ?
ans: by using private const.
class Parent{
private Parent(){
}
}
Note:- when creating child class object automatically parent class const will be
executed but parent object will not be created.
--------------------------------------------
** FACTORY METHOD:
------------------
>by using class name if we are calling a method and that method return same class
object , is called factory method.
ex: Test t=Test.getTest();
RunTime r = RunTime.getRuntime();
DateForamt df = DateFormat.getInstance();
-------------------------------------------------------
or
Note:- whenever we r loading child class automatically parent class will be loaded
but
whenever we r loading parent class, child class do not loaded automatically.
-----------------------------
** STATIC BLOCK :-
------------------
> to perform any activity at the time of class loading , SB executed at the time of
class loading, from top to bottom.
ex class google{
static
{
sop("hi its printint without main method.");
System.exit(0);
}
-------------------------
IMP NOTE:'
> static control flow is one time activity at the time of class loading.
but
> instance control flow is not one time, for every object creation it will be
executed.
------------------------------------------------------------
** TYPE CASTING :
------------------
>parent class ref can be used to hold child class object but by using that ref we
can not call child specific methods.
Note:- similarly we can use interface ref to hold impl class object.
ex: Runnable r = new Thread();
but invalid
String s = new String("delhi");
StringBuffer sb = (StringBuffer)s;
Rule 2:
-------
> "C" must be either same or derived type of "A" , else we get CE.
** RUNTIME CHECKING:
--------------------
>underlying(containing )object type of "D" must be either same or derived type "C".
else we get runtime exception saying:- ClassCastException.
** COUPLING :
-------------
the degree of dependency between component is called coupling.
if it is high , then its called tight coupling and
if it is low , then its called loose coupling.
** COHESION:
------------
>For every component we have to maintain a clear well defined functionality , said
to be high cohesion, its good programming.