aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance and Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance and Polymorphism" Watch "Inheritance and Polymorphism" New topic
Author

Inheritance and Polymorphism

Arun Kumar Gaddam
Ranch Hand

Joined: May 05, 2007
Posts: 58
In Inheritance we get qualities of the parent class and also in Polymorphism we get the same.Then what is the main difference.
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
Linking of method happen at compile time(inheritence) where as in polymorphism at run time.go through overloading and over-riding.

Thanks.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Inheritance let us to adapt superclass properties so we dont need to re write the same code again and again..
where as polymorphism let us to customize the inherited code as per our requirement..!!


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Manish Khurana
Greenhorn

Joined: Jan 26, 2008
Posts: 23
Overloading is compile time and Overriding is run time i think
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Originally posted by Manish Khurana:
Overloading is compile time and Overriding is run time i think

Overloading is a method with the same name but different arguments (and possibly a different return type). It can be in the same class, or a subclass.

Overriding is redefining a method in a subclass. The method must have the same name, arguments and return type. The access should be no more restrictive than the original.

Although, since Java 5, the return type can be more narrow when it returns an object reference. For instance, clone() doesn't need to return an Object but can return the class type itself. Arrays use this - if you clone an int[], the return is also an int[], and casts are no longer needed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
Inheritance is the capacity by which a class can use the properties and methods of another class while adding its own functionality. In other words it is the ability of a subclass to derive state and behavior from its superclass.

Inheritance gives the opportunity to add some functionality that does not exist in the original class. So the child class has the option of using the behavior as defined in the super class or modify the behavior/add new behavior. Inheritance provides a formal mechanism for code reuse.

The keyword used in inheritance is 'extends'. When a class B 'extends' another class A, we all the class 'B' a child or sub class of 'A'.

class A{
}

class B extends A{
}

Java doesn't support multiple inheritance. That means a class can 'extend' only one class. Each class is allowed to have just one direct superclass, and each superclass has the potential for an unlimited number of subclasses
--------------------

Polymorphism is a capability that helps to assign a different behavior or value in a subclass, to something that was declared in a parent class.

For example, a method can be declared in a parent class, but each subclass can have a different implementation of that method (name remains the same but the implementation differs). This allows each subclass to differ, without the parent class being explicitly aware that a difference exists. Polymorphism allows improved code organization and readability as well as the creation of extensible programs.

Let us take a simple example to illustrate how this works.

class Person{
public String getName(){
return "Person";
}
}

class JavaUser extends Person{
public String getName(){
return "JavaUser";
}
}

Now lets assume that we create two objects like
Person person = new Person();
Person javaUser = new JavaUser();

Although both the objects are of types 'Person' (Look at the left side of = sign),
a call
person.getName() returns -> "Person"
javaUser.getName() returns -> "JavaUser"
because the getName() has a different implementation in the sub class.

Polymorphism in Java is classified into two general types:
----------------------------------------------------------
1. compile-time polymorphism (method overloading type)

Overloading occurs when several methods have same names with different method signature. Overloading is determined at the compile time.Overloaded methods may all be defined in the same class, or may be defined in different classes as long as those classes have a superclass-subclass relationship.

Class Person{
String name;
String preferredName;

setName(String name){
}
setName(String name, String preferred){
}
}


2. run-time polymorphism (method overriding type)

When overriding methods, java determines the proper methods to call at the program´┐Żs run time, not at the compile time.Overriding occurs when a class method has the same name and signature as a method in parent class


An example of runtime polymorphism is below.

class Person{
public String getName(){
return "Person";
}
}

class JavaUser extends Person{
public String getName(){
return "JavaUser";
}
}


Hope you are clear on the concepts now!

Sincerely,
Your friends at www.javaadvice.com
www.javaadvice.com - Your partner in mastering Java.
-------------------
[ January 29, 2008: Message edited by: Lukasz Bajzel ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance and Polymorphism