This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Polymorphism

 
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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
 
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?
 
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!
 
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.


 
Sheriff
Posts: 21774
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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:
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!