Difference between creating object of class and extending class?

UPDATED: 15 October 2010
Java
Lets understand the difference between creating object of class and extending class. It'll be much better if I explain this logic with real codes.

Creating object of class
class superclass{
 private void display(){
  System.out.println("Super Class");
 }
}
public class subclass{
 public static void main(String args[]){
  superclass sup = new superclass();
  sup.display();
 }
}
Creating object of any class holds definition of its inner methods. Whether its public, private or any other identifier.

When you try to compile above code using command line. It'll show error as follow. If you are working in some IDE like netbeans or eclipse. IDE it self shows the error before compile.
subclass.java:9: display() has private access in superclass
  sup.display();
     ^
1 error

Extending class
class superclass{
  private void display(){
    System.out.println("Super Class");
  }
}
public class subclass1 extends superclass{
  public static void main(String args[]){
    display();
  }
}

When you extend any class it can't find definition of its inner methods. It'll show error as follow.
subclass1.java:8: cannot find symbol
symbol  : method display()
location: class subclass1
    display();
    ^
1 error

0 comments :