I'm a nooby at Java Programming. I have two questions 1. What is the purpose of a subclass? 2. What does a constructor do? I have the examples listed in pastebin http://pastebin.com/yxzKTePE
Lets say you have objects that represent "students," "teachers," and "staff" that you need to make. You could make them completely from the ground up or you could use inheritence. Inheritence is cleaner because there is less duplicated work. You might make a parent class "people" that has a name, address, and ID number. Then a subclass of people would be student that would add to this GPA. The subclass staff adds pay to this. Then teachrs could be a subclass of staff, so it has pay that way, but adds in class number. Because all of these are one way or another children of "people" they all get a name, address, and ID number without you needing to retype all that code. Now, lets say you create one of these objects. When you do so, you want to set some of the infomation, like the name and ID. The ID might be assigned based off of what is the next available ID. If it is an employee, you may als want set the pay. But what if you have some of this info, but you heard that the address you got was wrong. You still need to add them, but you don't have all the info. A constructor handles all this. It constructs, or builds, the object with what information is passed to it. Generally there are multiple constructors so that if not all information is given, some default information will be used.
sub class/classes is used for inheritance. A constructor is a method/subroutine called to create an object of that class.
Join our real-time social learning platform and learn together with your friends!