Guest User

Untitled

a guest
Oct 21st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. class Parent{
  2. int x=100;
  3. void method(){
  4. System.out.println("Parent Method");
  5. }
  6. }
  7. class Child extends Parent{
  8. int x=200;
  9. void method(){
  10. System.out.println("Child Method");
  11. }
  12. }
  13.  
  14. public class practice {
  15.  
  16. public static void main(String[] args) throws InterruptedException{
  17. Parent a = new Child();
  18. Child b=null;
  19.  
  20. b=(Child)a; //down casting 형 변환해야함
  21.  
  22. Parent c=null;
  23. Child d=new Child();
  24. c=d; //up casting 형변환 생략가능
  25.  
  26. //-----------------------------------------//
  27.  
  28. Parent p = new Child();
  29. Child q =new Child();
  30. System.out.println("p.x = "+p.x);
  31. p.method();
  32. System.out.println("c.x = "+q.x);
  33. c.method();
  34. }
  35. }
Add Comment
Please, Sign In to add comment