Thursday 25 September 2014

Session 13 :- Upcasting OR Parent Reference to Child Object (prco):-

What is the Up -casting ??

  • In java any parent class can refer its child object in the entire inheritance hierarchy. 
  •  In case of “PROC” method definition will taken from parent and implementation taken from child work in case of overriding. 
  • In case of “PROC” we can’t access child specific member (Instance variable ,instance method).

Example :- Parent Reference to Child object Or Up-casting.



public class Parent {
public void walking(){
System.out.println(" parent is walking ");
}
}
public class Child extends Parent {
@Override
public void walking() {
System.out.println("child is walking ");
}
public void play(){
System.out.println (" playing ");
}
}
public class Run {
public static void main(String[] args) {
// PRCO or Upcasting
Parent p= new Child();
p.walking();
//p.play(); We can’t access child specific properties
}



OUTPUT


child is walking

}



Advantage of PRCO or UP-Casting

  • loose coupling.
  • If you have N- Number of child object  you just give that parent class reference.so you remove the dependency. 
  • Standard way to method implementation.
Dis Advantage of PRCO or Up-Casting.
  • By using Up-casting You can't use the child specific properties Directly


No comments:

Post a Comment