<p>I don't understand this
Student a = new Undergrad();</p>
<p>Why isn't Student used before and after? Like
Student a= new Student();</p>
<p>or</p>
<p>Undergrad a = new Undergrad();</p>
<p>When using Student a = new Undergrad();, is "a" a student or an undergrad? Which methods does it get? And what does this do?</p>
<p>Inheritance.
Undergrad is a Student and it is a subclass of Student(superclass is Student, subclass is Undergrad), so Undergrad gets all of Student’s methods. That’s why the notation above is correct. </p>
<p>Student a = new Undergrad(); will work
a will be an Undergrad</p>
<p>But the advantages are you can use it as a Student.</p>
<p>Like if a class contained an ArrayList of Student objects it would be able to contain Undergrad, and graduate students.</p>
<p>I just want to make sure I’m getting that correct. If a method has a parameter of (Student s), then you could use the object created by:</p>
<p>Student a = new Undergrad();</p>
<p>correct?</p>
<p>Yes, you are right. :P</p>
<p>There’s a distinction here that a lot of people miss.</p>
<p>When you declare Student s, that means that at compile time s is of type Student (it’s called the static type of s), regardless of what you initialize it to. So, for instance, if Undergrad has a method getMajor(), you cannot call s.getMajor(), because Student does not have any such method. </p>
<p>Now, when you do s = new Undergrad(), at runtime s is of type Undergrad (this is called its dynamic type). So if you call s.getName(), then the code will use whatever getName() method is defined in Undergrad, even if it overrides the getName() method for Student.</p>
<p>Ok…
but then why not do everything as
Object a = new Undergrad();</p>
<p>So it would have everything from undergrad, but you could array it with anything you wanted?</p>
<p>Because often you want the subclass to inherit methods from its superclass. Say student had getID(). You don’t have to really change that from undergrad to grad to high school. So you can use getID() for any type of student. The object class doesn’t have any/very few defined methods (I think just compareTo and equals).</p>
<p>I thought it would get everything from undergrad?</p>
<p>You can only access the methods in Undergrad which it inherits or overrides from the superclass (if that is object, declaring Undergrad with object would leave it very mildly functional). In the exam you will only be tested on the toString() and equals() methods of the object class, (compareTo() is part of the Comparable interface).</p>
<p>To clarify my first point - say these are the methods in Student and Undergraduate</p>
<h2>Student</h2>
<p>getName()
getID()
study()
numClasses()</p>
<h2>Undergrad</h2>
<p>study() //overrides it
getIntendedMajor() </p>
<p>When you say,
Student s = new Undergrad();</p>
<p>you cannot call
s.getIntendedMajor();</p>
<p>however you can call the rest and if you call study(), polymorphism ensures that the study() method in Undergrad and not the one in Student is called. </p>
<p>I hope this helps (explaining it actually helped me get a better grasp on it =])</p>
<p>^Hahaha, yea, you’re right. I meant to put toString(). How’s everybody feeling about the test tomorrow?</p>
<p>I think I’m just gonna make silly mistakes on the MC. I seem to grasp the FRQ pretty well though. Gridworld’s just a pain to remember for the MC though. Praying for a 4, haha.</p>
<p>Okok, I see, thanks :)</p>
<p>@tmanneopen, you do know they provide an appendix with most of the Gridworld code for both sections of the exam, so it’ll be there for the multiple choice.</p>
<p>overall, I feel like I’m prepared (more so for the FRQ) and am just going to hope that I don’t make stupid mistakes on the MC</p>
<p>By the way guys is the quick reference guide updated to include java.util.List for the test???</p>
<p>Uh, not sure about the update. The code I know is used for the FRQ, not sure about MC.</p>
<p>Also, in the above, why can’t you do s.getIntendedMajor();? Is it because even though you declared it an Undergrad, s is still a Student?</p>
<p>If the student class does not have the method you are calling then a pointer of type Student will not be able to call that method.</p>
<p>If the student class DID have the method and the Undergrad class overrode it, then depending on whether s is pointing to an undergrad or not it will either use the Undergrad version of the method or the Student version of the method.</p>
<p>Yes, you can’t get it because it is still Student in nature - the Undergrad only tells it to choose overriden methods from the undergrad class polymorphically.</p>
<p>You can find the appendix here, this is what it is this year (I believe):</p>
<p><a href=“College Board - SAT, AP, College Search and Admission Tools”>College Board - SAT, AP, College Search and Admission Tools;
<p>Yep, makes sense. Thanks guys!</p>