• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A little help with my homework, explaining inheritance

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have homework due for my class and I got the swing design down but am really confused with the specifications of the homework. I'm still reading about inheritance so the assignment is a bit past my skill level. If someone could explain it in a simpler method that would be awesome!

Unfortunatly the case only meets once and week and the instructor is busy so it's hard to ask questions directly. Any comments are welcome.

Here's the program specifications.

*******************
*******************
*******************

Part I: Use inheritance to display a student record (continued)
Operation

� The user enters a college student�s identification number followed by the major and GPA. (this part I got)
� The application creates a college student object and displays the object to the screen. (this part done)

(Now the confusing part)
Specifications
� First, create a class that defines a student. This class should contain at least two instance variables for identification number and GPA. It should also contain two constructors�a default one and one that accepts two values for the instance variables. In addition to set and get methods, the class should override the toString method from the Object class.
� Next, create a class that defines a college student. To do this, the class should inherit the above class. In addition to the inherited instance variables, this class should contain at least one more instance variable that defines the college student�s major. This class should also contain a default constructor and one that accepts three values for the instance variables. The first statement in this constructor should call a constructor in the superclass. This class should also override the toString method.
� Code a driver class that carries out the operation of the project.
� Assume valid data is entered.

Again, any comments are more than welcome!

Thanks,
Rob
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that you're looking at the whole picture and worrying too much, it's much simpler when you look at all the rather specific little problems.
Can you perhaps go through all the steps mentioned and then ask when you get stuck on a specific problem? It will be much easier to assist you then without spoon feeding.
Even to deal with the inheritance only it would help to know what you don't understand, simply put though all students have an "identification number and GPA", so these are properties of the Student class. Next you have a college student, that is a student, so your class is defined as CollegeStudent extends Student. It inherits all the properties of the student, and in addition has a major, so add that property to CollegeStudent.
Lastly both of these classes override Object's toString method to give a String representation of the object, well Student does and CollegeStudent overrides Student's toString.
I'm not sure that was helpful at all but if you're more specific you'll get a more specific response.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

The student class contains the attributes of a generic student, things like an ID & GPA. The college student subclass contains these properties as well as some properties special to it , like the major.

Now, your student class must have two constructors, a default one, and another one accepting two default values for the ID & GPA.

In your college class, you'll have a constructor that accepts three values(two for the attributes it has inherited from student class, and one for its own attribute). This would call the superclass' constructor and pass the two values that are ID & GPA. The third one it would assign to its local attribute (Major).

I am not clear as to why do you have to override the toString method. If you could shed some light on the same..

Hope I was able to help...
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Freiberger:
� Next, create a class that defines a college student. To do this, the class should inherit the above class. In addition to the inherited instance variables, this class should contain at least one more instance variable that defines the college student�s major. This class should also contain a default constructor and one that accepts three values for the instance variables. The first statement in this constructor should call a constructor in the superclass. This class should also override the toString method.



Rob,
As a hint to this "Next" bullet, I designed an initial design for your college class for the constructors that you specified above.

public class CollegeStudent extends Student {
private String major;
public CollegeStudent() {
super();
}
public CollegeStudent(int id, float gpa, String major) {
super(id, gpa);
this.id = id;
this.gpa = gpa;
this.major = major;
}

public <type> getXxx(){...}
public void setXxx(<type> xxx){...}

public String toString() {
return "The String that we want to return";
}
}

Any enhancement on this inital class design is welcome to help Rob out. Rob, why don't you come up with your existing design so that ranchers can help you to improve according to your initial design.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ta Ri Ki Sun:
It seems to me that you're looking at the whole picture and worrying too much, it's much simpler when you look at all the rather specific little problems.



I agree.

Are you familiar with creating your own classes and intantiating them as object? You should have that down cold before you start worrying about inheritance.

So, if you haven't already, try just doing the Student class first. Get so that you can define your class, create a new object (or objects) based on that class, assign various ID and GPA values to it, print those values out, and so on.

Then, and only then, should you tackle the CollegeStudent class.

Which might lead you to wonder: Why do all this inheriting in the first place? Well, the idea here is that you need a special type of Student that has the ability to hold a Major. Now, in the dark old days before object-oriented programming, we would have to rewrite our Student class. But in Java, we simply need to create a new class (a subclass) that has the Student class as a "parent". We add just the new functionality to our subclass, and it magically gets all the functionality of the Student class "for free".

In theory, someone else might one day write a "ElementarySchoolStudent" class that also inherits from Student. It won't have a Major (that's just in your CollegeStudent subclass), but it might add the ability to store a SchoolBusRouteNumber, or it might have a method that sends a report card to the parents. Someone else might write an "AdultEducationStudent" that would have still other functionality... you get the idea.

And if it turns out that, for example, you one day need to store and track the GPA as a percentage instead of a 0.0-4.0 number, all you have to change is your parent class, Student. The subclasses will then automatically use this new way of dealing with the GPA--you won't have to change them at all.

- Jeff
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
This sample chapter from Head First Java, Second Edition might help you in understanding the way to subclass a class and the reason we need OO. I am even currently reading that chapter for my SCJA preparation.
[ April 19, 2005: Message edited by: Ko Ko Naing ]
 
I am Arthur, King of the Britons. And this is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic