How to dynamically create object properties from an array in Javascript?

Object-oriented java (not javascript) problem?

  • Ok now im having a hard time with object oriented programming in java. i am using blueJ to develop it as it is recommended. Now, the main problem lies in the following scenario: i have to create an application that declares an array with 3 different types of objects in it. I have decided to make this easier i would use 3 seperate arrays. My classes are as follows: Superclass - Person. Subclasses of Person - CollegeEmployee & Student. Subclass of CollegeEmployee is Faculty. Now, when the person object is created it holds information such as names and stuff. However i want to carry these details to the subclass and extend on them. However, to access them i need to add them into the subclass's constructor, however this makes me re-enter the data again into the object of that subclass, Student for example. What i would like to know is if there is a way to carry the variables from the superclass, person, to the subclasses, Student and CollegeEmployee... If you need the source code for the 3 classes i can post it up or something as a .zip file Thank you for reading and i hope someone knows how to help.. Pastebin links: Person Class: http://pastebin.com/FdXq1Ewe CollegeEmployee Class: http://pastebin.com/WyDRNsWF Student Class (currently working on this one): http://pastebin.com/nTfSDiB2 Faculty Class: http://pastebin.com/PaySbjBt Hope this helps...

  • Answer:

    Oh dear. I think back to the drawing board is required here. You seem to have missed by a very long way. You will have only one array and it will be of type Object or Person. Type Object because all classes in Java inherit from the Object class. Type Person because this is the class that all your other classes inherit from. Your Person class will have all the details and the methods that are unique to a person and nothing else. There will be NO references in the Person class to any of the sub class variables or methods. Now the sub classes when defined should only contain the extra information that is unique to that subclass. Overwritting anything that is not unique to the parent class like an equals method or a toString Method. Now create the array in the main method and instantiate the elements as you require. Test the array by printing various information of each element. Additional: When you have done it right this code will work. public static void main(String[] args) { Person myDetails[] = new Person[10]; myDetails[0] = new Person("Pete", "Johnson", "The Street, The town", 1234, 1234567); myDetails[1] = new Student("Stud", "Johnson", "The Street, The town", 1234, 1234567, 75, "Mathematics"); myDetails[2] = new CollegeEmployee("collemp", "Johnson", "The Street, The town", 1234, 1234567, 30000, 807, "Mathematics"); for (int i = 0; i < myDetails.length; i++) { myDetails[i].showPerson(); if (myDetails[i] instanceof Person) { myDetails[i].showPerson(); } if (myDetails[i] instanceof Student) { myDetails[i].showPerson(); //You have to have this method in every class. } if (myDetails[i] instanceof CollegeEmployee) { myDetails[i].showPerson(); } if (myDetails[i] instanceof Faculty) { myDetails[i].showPerson(); } } } If you wanted to you could actually place the array and processing into a new class so that way you could use your search methods. Have fun.

Mike Oxlong at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Decide whether you want Person super class to be abstract or not. I mean do you intend to instantiate Person type of objects in your application or not.

James Bond

i'm not sure what the question is, do you mean super(); ??

Baatus

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.