• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Kindly explain me the difference between inheritance and instantiation

 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
kindly see the code below.

public class Animal()
{
public static void execute()
{
System.out.println("This is an animal");
}
}

//Case 1:Accessing the methods after inherting
public class Lion extends Animal
{
public static void main(String[] args)
{
execute();
}
}

Case2:Accessing the methods by instantiating
public class Lion
{
public static void main(String[] args)
{
Animal a = new Animal();
a.execute();
}
}

Kindly explain me the difference between case1 and case2.In both cases,ia able to access the method in the Animal class.So what is the difference between instantiating a class and inheriting a class?Awaiting your replies.

Regards,
Sathya
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sathya,
In both cases you are instantiating. In case 1 you are instantiating Lion class which inherit Animal class so even Animal class is getting instantiated and by the law of inheritence execute method from Animal class is getting inherited and executed by main method.
In case 2 you are directly instantiating the Animal class and calling its execute function.

Thanks,
kundan
 
Dani Atrei
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdi,

When you inherit a class, you get the superclass' attributes and methods that you can use without instantiating a superclass object. You can only use those which are public or protected.
If you instantiate them, you make an object and upon that object you call methods defined in the object's class. Cool.

Now the interesting bits of what you can do with inheritance are tricky, and i hope i get the details right. Imagine you have an Animal superclass and a Lion subclass. You can write this piece of code:


Here you have a variable anAnimal, holding a reference of type Animal to a Lion object.

So now if you code this up:



It's actually going to call the Lion execute method. This is called Dynamic Binding.
The important thing about inheritance is that they share a supertype, but then the details of the code can differ, so in a method that accepts Animal as a parameter, you could give it a Lion, Elephant, Iberian Lynx etc... Yet, they can call methods from their own class definition.

Dan
[ December 29, 2005: Message edited by: Dani Atrei ]
 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kundan,
so is inheritance and instantiation the same???Kindly let me know.

Regards,
Sathya
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Sathya not at all, If you inherit a class then you can use its methods and variables in subclass( except private). But instantiation is a process where you are making object of class in runtime. You can only call methods of a class if you instantiate it( except static class). Now inheritance provides super class method to subclass thats why you can call method of Animal from Lion because execute method was inherited to Lion class by the definition of INHERITANCE. So in runtime you are calling inherited method of Lion class actually.
Hope its clear now.
Thanks,
kundan
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sathya,
Go through this url

http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html

and

http://java.sun.com/docs/books/tutorial/java/concepts/class.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic