dynamic polymorphism

dynamic polymorphism

give an example for dynamic polymorphism?

View Answers

June 11, 2012 at 1:14 PM

Dynamic polymorphism is where a class overrides a superclass method or implements an interface. For example, any class may override the Object.toString() method and provide its own implementation, and this is known at compile time. However, for a simple Java program that instantiates a series of objects and calls their toString() method, the compiler does not consider the object references differently. Any differences in the objects' toString() implementations are only seen at runtime, so they are considered dynamic.

Here is an example of Dynamic Polymorphism that will develop suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc.

class point{
void show(){
System.out.println("This is the Point Base class");
}
}
class shape extends point{
void display(){
System.out.println("Different shapes can be developed with different number of points");
}
}
class rectangle extends shape{
int l,b;
void getdata(int x,int y){
l=x;b=y;
}
void area(){
System.out.println("Length:"+l);
System.out.println("Breadth:"+b);
System.out.println("Area:"+(l*b));
}
}
class square extends shape{
int a;
void gdata(int x){
a=x;
}
void area(){
System.out.println("Side:"+a);
System.out.println("Area:"+(a*a));
}
}
class circle extends shape{
int r;
void get(int x){
r=x;
}
void area(){
System.out.println("Radius:"+r);
System.out.println("Area:"+(3.14*r*r));
}
}
class triangle extends shape{
int b,h;
void tdata(int x,int y){
b=x;h=y;
}
void area(){
System.out.println("Base:"+b);
System.out.println("Height:"+h);
System.out.println("Area:"+(0.5*b*h));
}
}
class ShapeTest{
public static void main(String args[]){
rectangle r = new rectangle();
square s = new square();
circle c = new circle();
triangle t = new triangle();
r.show();
s.display();
System.out.println("");
System.out.println("Rectangle:");
System.out.println();
r.getdata(12,6);
r.area();
System.out.println("");
System.out.println("Square:");
System.out.println();
s.gdata(7);
s.area();
System.out.println("");
System.out.println("Circle:");
System.out.println();
c.get(5);
c.area();
System.out.println("");
System.out.println("Triangle:");
System.out.println();
t.tdata(4,7);
t.area();
}
}

April 14, 2013 at 1:05 PM

This is the Point Base class Different shapes can be developed with different number of points

Rectangle:

Length:12 Breadth:6 Area:72

Square:

Side:7 Area:49

Circle:

Radius:5 Area:78.5

Triangle:

Base:4 Height:7 Area:14.0









Related Tutorials/Questions & Answers:
dynamic polymorphism
dynamic polymorphism  Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism
dynamic polymorphism
dynamic polymorphism  give an example for dynamic polymorphism?   Dynamic polymorphism is where a class overrides a superclass method... of Dynamic Polymorphism that will develop suitable hierarchy, classes for Point, Shape
Advertisements
Dynamic polymorphism - Java Beginners
Dynamic polymorphism  Develop with suitable hierarchy, classes for point, shape, rectangele, square, circle,ellipse, triangle, polygon, etc. Design a simple test application to demonstrate dynamic polymorphism.. Thanks
Dynamic Polymorphism - Java Beginners
Dynamic Polymorphism  Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle, Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic polymorphism.? Thanks
polymorphism
polymorphism  can we achieve without overriding polymorphism
Run time polymorphism
Run time polymorphism  What is run time polymorphism or dynamic method dispatch
Java run time polymorphism
Java run time polymorphism  What is run-time polymorphism or dynamic method dispatch
Polymorphism - Java Beginners
Or we can also say that a polymorphism is possible in both state Static and Dynamic. Coz polymorphism means in many forms. So do not get confused of it just...Polymorphism  type of polymorphism  In Java polymorphism
Polymorphism in actionscript
Polymorphism in actionscript  Hi....... Please implement How polymorphism works on actionscript? Thanks  Ans: class Worker extends Employee { override public function receivePayment():Number { return 0
Polymorphism
Polymorphism - Java Beginners
Polymorphism  what is dynamic polymorphism? with example  Hi friend, Dynamic polymorphism is run time polymorphism. Thanks ... static. Dynamic polymorphism is where a class overrides a superclass method
Polymorphism in Hibenate - Hibernate
Polymorphism in Hibenate  Hi, How polymorphism inheritance and association achieved in hibernate. Thank u in advance
Does hibernate support polymorphism?
Does hibernate support polymorphism?  Hi, Does hibernate support polymorphism? thanks
ModuleNotFoundError: No module named 'polymorphism'
ModuleNotFoundError: No module named 'polymorphism'  Hi, My Python... 'polymorphism' How to remove the ModuleNotFoundError: No module named 'polymorphism' error? Thanks   Hi, In your python
Polymorphism in Java 7
This tutorial describe concept of Polymorphism. It is one of OOPs concept
polymorphism - Java Beginners
polymorphism  what is polymorphism? what is the advantage of function overloading? explain the concept of operator overloading?  Hi Friend, Please visit the following links: http://www.roseindia.net/help/java/m
Polymorphism - Java Beginners
Q: Isn't it possible to detect the method's associatation, in case of method... is run (run-time polymorphism) ? Isn't java compiler having such an ability... Polymorphism means the ability to take more than one form. Polymorphism
runtime polymorphism - Development process
runtime polymorphism  how run time polymorphism is achieved  Hi Friend, The run-time polymorphism is basically the Method Overriding.Here super class reference is referring to subclass object and compiler does
Polymorphism : Method Overriding
In this tutorial you will learn another concept of polymorphism that is method overriding
DYNAMIC BINDING
DYNAMIC BINDING  WHAT IS DYNAMIC BINDING
PHP Polymorphism Function
PHP Polymorphism Function: The PHP Polymorphism Method is one of the feature of OOP language. Generally we get polymorphism in two ways: Compile time Run time Compile time polymorphism PHP is like function overloading, operator
Polymorphism : Method Overloading
In this tutorial you will learn one of polymorphism concept that is method overloading with example in Java 7
dynamic report
dynamic report  i need complete code for generating dynamic report in jsp
What is Polymorphism in Java?
Polymorphism in Java means a subclass, though having its own unique behavior... IS-A relationship is polymorphic. In Polymorphism, common interface is used while writing code. Single polymorphism is achieved by method overriding while multiple
dynamic method dispatch - Java Beginners
dynamic method dispatch  can you give a good example for dynamic method dispatch (run time polymorphism)   Hi Friend, In dynamic method dispatch,super class refers to subclass object and implements method overriding
Dynamic keyword
Dynamic keyword  hi....... What is the dynamic keyword used for in flex? give me the answer ASAP Thanks  Ans: Dynamic keyword-> Specifies that instances of a class may possess dynamic properties added
dynamic jquery statement
dynamic jquery statement  dynamic jquery statement
( Inheritance,need help in Polymorphism, Overriding) - Java Beginners
( Inheritance,need help in Polymorphism, Overriding)  can any one please help me create this application,thank you Advanced Concepts with Classes( Inheritance, Polymorphism, Overriding) Employees in a company are divided
Java question to implement polymorphism - Java Beginners
Java question to implement polymorphism  I have a question that i need to answer:- Using the principle of polymorphism provide implementation for the calculateFees() method for each Online Fax Subscription type
PHP Dynamic CheckBox
PHP Dynamic CheckBox  Help with Dynamic Checkbox in PHP
php dynamic array checking
php dynamic array checking  php dynamic array checking
php dynamic array checking
php dynamic array checking  php dynamic array checking
create dynamic array in javascript
create dynamic array in javascript  How to create dynamic array in javascript
dynamic textfields
dynamic textfields  Hi, I am a fresher and joined recently in one company. they gave me a desktop application project using swings.here is my detailed spec Screen 1 - The user enters the ingredients from a food product, saves
Dynamic form
Dynamic form  I need to make a dynamic form using jsp for example, i will need a list of items which when we select one option another list is modified ( like 2 lists of countries and citys ), and also a radio button wich once
Polymorphism
; OOPs revolve around the four concepts: 1. Encapsulation 2. Polymorphism..., programmatically. Here we will discuss Polymorphism briefly: -- Polymorphism... behavior to method calls of the same name. -- Polymorphism does not allow
dynamic form
dynamic form  I need to make a dynamic form using php, for example, i will need a list of items which when we select one option another list is modified ( like 2 lists of countries and citys from a dtabase ), and also a radio
dynamic calender
dynamic calender  hi i need the code to "insert date using GUI"   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.awt.*; import java.awt.event.*; import javax.swing.*; class DatePicker{ int month
dynamic query
dynamic query  DECLARE QUERY VARCHAR2(32767); FIN_QUERY VARCHAR2(32767); CURSOR C IS (select distinct PORTFOLIO from MPE_TEST1); CURSOR C2 IS SELECT DISTINCT PORTFOLIO_LEVEL, PORTFOLIO FROM MPE_TEST1 ORDER
how to add dynamic data
how to add dynamic data  how to add dynamic data to an existing web application
dynamic web pages in html
dynamic web pages in html  How to create Dynamic web pages in HTML
static page and dynamic pages?
static page and dynamic pages?  what is the static page and dynamic pages
static page and dynamic pages?
static page and dynamic pages?  what is the diff between static page and dynamic pages
static page and dynamic pages?
static page and dynamic pages?  what is the static page and dynamic pages adv and disadv
Dynamic include jsp
Dynamic include jsp  I need dynamic include jsp page with an example
ModuleNotFoundError: No module named 'dynamic'
ModuleNotFoundError: No module named 'dynamic'  Hi, My Python... 'dynamic' How to remove the ModuleNotFoundError: No module named 'dynamic... to install padas library. You can install dynamic python with following command
ModuleNotFoundError: No module named 'dynamic'
ModuleNotFoundError: No module named 'dynamic'  Hi, My Python... 'dynamic' How to remove the ModuleNotFoundError: No module named 'dynamic... to install padas library. You can install dynamic python with following command
dynamic image change javascript
dynamic image change javascript  How to display images in JavaScript dynamically
Dynamic Array iPhone
Dynamic Array iPhone  Dynamic Array iPhone How can i add NSMutable array dynamically into my iPhone App
( Inheritance, Polymorphism, Overriding)i need help - Java Beginners
( Inheritance, Polymorphism, Overriding)i need help  Advanced Concepts with Classes( Inheritance, Polymorphism, Overriding) i need help to create this application,than you Employees in a company are divided into the classes

Ads