answersLogoWhite

0


Best Answer

This can be a confusing topic because one can argue that sharing method attributes between a super or abstract class with a derived class could be described by either Polymorphism or Inheritance. One significant differerence could be illustrated as to when or under what circumstances you would you use one or the other. An ideal instance of using Inheritance would be when you want to create an entirely new class, but wish to borrow a group of existing attributes or methods resident in an existing Abstract or super Class, instead of re-inventing the wheel. If you had an Abstract Class Carnivore and wanted to create a subclass Cat, you could instantly inherit all the methods that were common in Carnivore that applied to Cat without writing new code. Polymorphism could be best applied when you had an existing SubClass that you wanted to modify or add a feature that could borrow a method that existed in a higher class. Take the Cat subclass, which inherited attributes from Carnivore, and you wanted to add the method Stalk, which would generically describe how a carnivore approaches its prey. However its implementation in Cat would detail the stealth it uses that are unique to a Cat.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

This can be a confusing topic because one can argue that sharing method attributes between a super or abstract class with a derived class could be described by either Polymorphism or Inheritance. One significant differerence could be illustrated as to when or under what circumstances you would you use one or the other. An ideal instance of using Inheritance would be when you want to create an entirely new class, but wish to borrow a group of existing attributes or methods resident in an existing Abstract or super Class, instead of re-inventing the wheel. If you had an Abstract Class Carnivore and wanted to create a subclass Cat, you could instantly inherit all the methods that were common in Carnivore that applied to Cat without writing new code. Polymorphism could be best applied when you had an existing SubClass that you wanted to modify or add a feature that could borrow a method that existed in a higher class. Take the Cat subclass, which inherited attributes from Carnivore, and you wanted to add the method Stalk, which would generically describe how a carnivore approaches its prey. However its implementation in Cat would detail the stealth it uses that are unique to a Cat.

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

Short answer:

They are the same.

Long Answer, (and yet less revealing):

Polymorphism is simply the ability to have many different methods (Or functions, for those who are used to C-Type programs) to have the same name, but act differently depending on the type of parameters that were passed to the function.

So for example, we may have a method called punch, which accepts no parameters at all, and returns an integer:

public int punch()

{

return 3;

}

We could also have a method named punch that accepts a String and returns a boolean.

public boolean punch(String poorGuyGettingPunched)

{

if(poorGuyGettingPunched.equals("joe"))

{

System.out.println("sorry Joe");

return true;

}

else

return false;

}

That is called polymorphism... And strangely enough, it is also called overloading.

Do not confuse this with overriding, which replaces a function or method with a new one, or rather, hides the old method and replaces it with a new one.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Polymorphism

Polymorphism means "many forms". Programmers use polymorphism in two ways: statically and dynamically. Static polymorphism occurs at compile time while dynamic polymorphism occurs at runtime. This is achieved though overloads and overrides (respectively) but all it really means is that the compiler generates code that allows your program to behave in a more generic manner rather than a specific manner. Generic programming makes your code more flexible, easier to maintain and (with good design) more efficient.

Overloaded Functions

Overloaded functions are functions that have the same name within the same namespace, but that have different signatures (they each differ in the number and/or type of its arguments). The signatures are used to differentiate these functions, thus eliminating any ambiguity about which function is to be invoked.

Function templates allow the compiler to generate function overloads that have the exact same implementation, only differing by type. This is useful in that the programmer need only provide one implementation that caters for all types. Function templates can also be specialised to provide more specific implementations for more specific types. Since the compiler generates the actual code on behalf of the programmer, this is a type of polymorphism known as static polymorphism. That is, the specific code generated by the compiler changes automatically according to the types actually used by the programmer; there's no need to manually write overloads that may never be used.

Overridden Functions

Overridden functions are functions that have the same name and signature but that reside in separate namespaces. In order to remove any ambiguity, the programmer must explicitly state the namespace of the function they wish to invoke. However, classes are also namespaces and classes use overrides to enable dynamic polymorphic behaviour.

Classes that are expected to be used as base classes will always have a virtual destructor and will typically expose one or more virtual or pure-virtual functions. Virtual functions are functions that are expected to be overridden by derived classes. Pure-virtual functions are functions that must be overridden by derived classes.

When a virtual function is invoked, the most-derived override of that function is always invoked first. This is why base classes must have a virtual destructor because destruction must always start with the most-derived class, regardless of which object within the hierarchy is destroyed. Destruction is the opposite of construction which always starts with the least-derived class (hence you cannot have a virtual constructor).

Typically, a base class will have no knowledge regarding any of its derivatives. Thus when a base class implicitly invokes one of its own virtual methods, it cannot know which override to invoke. This in turn means the compiler cannot know which specific override to invoke. In order to determine the specific override, the base class defines a virtual table (a v-table) containing a list of function pointers for each of its virtual methods. Derived classes inherit this v-table and replace any pointers they override with their own. Thus the most-derived class in any hierarchy always points to the most-derived method of each override, regardless of which class provides that method.

Thus when an overridden function is invoked, the compiler generates code to perform a v-table lookup that will be performed at runtime. Thus functions are bound to their callers dynamically, at runtime, and every object behaves according to its runtime type, even when that type is unknown at compile time. Knowing a virtual interface exists is all the compiler needs to know.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Operator overloading simply means to provide a new implementation for an existing operator.

Polymorphism is a function of inheritance whereby it is not necessary to know the runtime type of an object so long as you know the generic type. The virtual table ensures you gain specific behaviour by calling the generic type's virtual methods.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Encapsulation, inheritance, polymorphism and abstraction form the four main pillars of object oriented programming.

Encapsulation includes information hiding (private and protected data and methods). All the functionality of an object should be fully encapsulated by the object itself. A clearly-defined interface should allow consumers of the object to manipulate that object's data members in a highly-controlled manner, thus ensuring data integrity at all times. It is not necessary for a consumer to know the underlying implementation of an object; consumers need only know what an object does, not how it does it. Providing the interface is not altered in any way, this allows the implementation of an object to be altered by the class developer without affecting the consumer in any way. Objects may contain other objects, however every object is responsible only for itself, not for the objects they contain.

Inheritance allows new objects to derive from existing objects (base classes), such that all public and protected members are inherited by the derived object. This allows the most-derived object to provide a more specialised implementation of the underlying base class, whether to augment those implementations or to replace them completely. This allows existing code to be re-used without having to duplicate that code, thus reducing code maintenance.

Polymorphism is related to inheritance in that derived objects are "a kind of" base class, and can therefore be treated as such. Virtual methods in the base class permit a generic interface to be overridden by the derived class such that the most-derived implementation is called even when referring to the base class. This ensures base classes "do the right thing", whether the base class is derived or not. Virtual methods need not be overridden if the base class implementation is sufficient. However, if no virtual methods are overridden by the derived class then there is no need to derive from the base class at all, the base class itself can be used instead.

Abstraction is related to polymorphism in that pure-virtual methods in the base class must be overridden by the derived class in order to instantiate the derived class. Any class that declares a pure-virtual method is automatically an abstract base class and cannot be instantiated. It is intended as a conceptual class that provides a common generic interface to all of its derivatives and is expected to be derived from by two or more derivatives. Derivatives of an abstract class that do not provide a complete implementation become abstract themselves, however any implementations they do provide can be inherited by derivatives of the derivative. Only a fully-implemented derivative (a concrete class) can be instantiated. The base class that declared the pure-virtual methods may optionally provide an implementation for those methods, but those implementations are not inherited by the derived classes. However, the derived class can explicitly call the base class implementation to augment it or leave it as is, or it can replace it outright with a completely new implementation.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Inheritance is the object oriented feature using which child classes can get the features of the parent class. The public variables and methods of the parent class would be available to the child class

Polymorphism is the feature by which the same entity of a class exists in different forms. For example you can have multiple methods in the same class with same names provided they have a different method signature.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Inheritance is the object oriented feature by which the functionality and features of one class are made available to another class without having to copy paste the code.

Polymorphism is the object oriented feature by which multiple methods of a class can coexist with minor differences. Ex: method overloading.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Inheritance and Polymorphism are two totally different things. The only relation between the two is that they are both Object Oriented concepts used in Java.

Please check the Related Links to read in detail about Inheritance and Polymorphism and once you do, you will realize that they are both totally different.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between inheretance and polymorphism in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Does java supports polymorphism?

Yes.


Is Interfaces in Java a kind of polymorphism?

No. Interfaces in Java are a construct to get polymorphism ( subtype polymorphism ) working in Java, but they are not a "kind" of polymorphism. In polymorphism happens when two objects respond to the same message ( method call ) in different way ( hence poly -> many, morphism -> way or shape : polymorphism -> many ways). In Java to be able to send the same message to two different objects you have to either inherit the same parent, or implement the same interface.


What is difference between java 2 and java 5?

They are different versions. Java 5 is newer than Java 2. Think of it like the difference between the Playstation 1 and the Playstation 3.


What is the main difference between inheritance and polymorphism?

The main difference is that: neither of them are related to one another. They have nothing similar between them and are totally different. The only point here is that they are both used in Java which can be stated as a similarity. Inheritance is the feature wherein functionality from one class is available for another class. Polymorphism refers to the feature wherein the same entity exists as multiple items. Ex: method overriding, method overloading etc.


Major difference between c and java?

Java is object oriented, C is not...


What is the difference between connectivity in java?

kamina


What is difference between connectivity in java?

kamina


What is the main difference between UNIX and JAVA?

Unix is an operating system, Java is a language.


Does java support oops concept?

Yes. Java is an Object Oriented Programming Language and it supports the OOPS concepts like Inheritance, Polymorphism etc


Difference between recordset and resultset in java?

Rowset


What is the difference between JAD and JAR?

JAD-Java Application Description JAR-Java archive


What is compile time polymorphism in java?

Compile Time Polymorphism in Java is when you have the several methods with same name and different parameters and compiler has to decide how to select which method has to run based on the arguments hence the name Compile time polymorphism or method overloading.