aspose file tools*
The moose likes Beginning Java and the fly likes Kindly explain me the difference between inheritance and instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Kindly explain me the difference between inheritance and instantiation" Watch "Kindly explain me the difference between inheritance and instantiation" New topic
Author

Kindly explain me the difference between inheritance and instantiation

Sat Nar
Ranch Hand

Joined: Oct 22, 2004
Posts: 83
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

Joined: Mar 08, 2004
Posts: 322
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


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Dani Atrei
Ranch Hand

Joined: Feb 17, 2004
Posts: 73
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 ]

Si altas son las torres, el valor es alto - Alberti
Sat Nar
Ranch Hand

Joined: Oct 22, 2004
Posts: 83
Hi Kundan,
so is inheritance and instantiation the same???Kindly let me know.

Regards,
Sathya
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
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

Joined: Mar 08, 2004
Posts: 322
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Kindly explain me the difference between inheritance and instantiation
 
Similar Threads
OverLoading
Constant Specific class body [ENUM]
LinkedHashSet
Difference between redefinition and overridding
a strange question about polymorphism