• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I want to write the code about polymorphism.

sales
I want to create an array of Employee class, then put instance of salesman ,worker.... and use this loop:




But I dont know How to create an array of Employee class and put these instance to it.Please guide me

 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is that an array is an object, and like any other object,
you must declare it and then create it, like:This allocates space in myArray for 5 Integer objects, each set to null.
You can now loop through to initialize them: Jim ... ...
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create an array of employee class, Then put in this array instance of classes that inheritance from employee class, Then calculate salary.

The result is I want to show the polymorphism: an array that each of its members do exclusive thing,but same data type, data type is employee class.


Is it good example for polymorphism?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Is it good example for polymorphism?


You're using inheritance and overriding parent methods, so it sounds great to me!
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Is it good example for polymorphism?


If your example is just to practice polymorphism for the sake of polymorphism, it is a good example, because similar/same to those examples in Java book on drawing different shape (Rectangle, Triangle, etc.)

I am doubt that you are having some method overriding code inside constructor ??? is this correct ???

---------

However, when back to the basic from the view of design, the method raise() should not be in Employee class, and thus not in all its subclasses. No offense ...
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It has error, I used super in order to access salary, Why doesn't it work?Secretary class wants to have its own constructor.


 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each constructor that does not have an explicit call to "this(...)" or "super(...)" gets an implicit call to "super()" - the parent's constructor without parameters. This is called constructor chaining. Since class Employee does not have such a constructor you have to manually call the existing constructor providing the fields:
 
Henry Wong
author
Marshal
Pie
Posts: 21442
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
It has error, I used super in order to access salary, Why doesn't it work?Secretary class wants to have its own constructor.



Just taking a very quick look ... is the compiler complaining that the Secretary class is trying to use a constructor of the Employee class that doesn't exist? Remember, in a constructor, if you don't make an explicit call, either to another constructor, or the super constructor, then the compiler will call the super no-arg constructor for you.


[EDIT: beaten to the answer again... ]

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then when we want to use parent variable in child constructor, we must use super keywrod and use constructor of paret class, There is no other way to initialize parent variable. Right?
 
Henry Wong
author
Marshal
Pie
Posts: 21442
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Then when we want to use parent variable in child constructor, we must use super keywrod and use constructor of paret class, There is no other way to initialize parent variable. Right?



I think you are missing the point. The error has nothing to do with whether you can access a parent variable via the super keyword. The child class IS-A parent class, and hence, the super constructor must be called -- and in your example, you are trying to call a constructor that does not exist.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic