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

Polymorphism

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


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

Joined: May 03, 2008
Posts: 4542
    
    5

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html


luck, db
There are no new questions, but there may be new answers.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jan 23, 2008
Posts: 213
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 ...

SCJP 6, SCWCD 5, SCBCD 5
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18828
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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
Sheriff

Joined: Sep 28, 2004
Posts: 18828
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism