# Creating instances of inherited classes in Java

**URL:** <https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864>\
**Category:** Factual Questions\
**Created:** [July 10, 2006, 1:46pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864 "2006-07-10T13:46:01Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayo\_Speaks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mayo\_Speaks](https://boards.straightdope.com/u/Mayo_Speaks)\
**Post date:** [July 10, 2006, 1:46pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/1 "2006-07-10T13:46:01Z")

</div>

I have a class “Student” that extends an abstract class “Person”. If you were instantiating a new Student object, would you code:

```auto

Student newStudent = new Student( "John Doe" ); // Call this one 'A'

```

or

```auto

Person newStudent = new Student( "John Doe" ); // Call this one 'B'

```

Both forms appear to work the same, but my teacher and I keep arguing about which is preferable. I say A, she says B. I’ve looked it up in my two textbooks, and one uses A and one uses B.

So the questions are:

1. What’s the difference? (if any)
2. Which style is better? (if either)

And no, this is NOT a homework question.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [July 10, 2006, 1:53pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/2 "2006-07-10T13:53:00Z")

</div>

Why does she think that the second one is preferable?

---

<div class="post-metadata">

**Author:** ![Mayo\_Speaks](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mayo\_Speaks](https://boards.straightdope.com/u/Mayo_Speaks)\
**Post date:** [July 10, 2006, 2:21pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/3 "2006-07-10T14:21:59Z")

</div>

> [@Dominic Mulligan](#):
>
> Why does she think that the second one is preferable?

Because it’s what the textbook we’re using in class uses. ([Java Outside In](http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=052101087X): a thouroughly terrible book, IMHO) I actually think it’s a horrible idea, and it wouldn’t even compile if I were designing the language. It’s especially confusing when I type:

```auto

Person aStudent = new Student( "John Doe" );
aStudent.showCurrentGPA(); // showCurrentGPA() defined in class Student

```

The actual meaning of the code seems very muddled to me, since a generic Person object shouldn’t have a showCurrentGPA() method.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [July 10, 2006, 3:22pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/4 "2006-07-10T15:22:06Z")

</div>

Your teacher is a boob.

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [July 10, 2006, 3:23pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/5 "2006-07-10T15:23:40Z")

</div>

Which is preferable is completely context-dependent.

If you need to use the generic class (Person) for, say, passing to a method that specifies ‘Person’ as a parameter, then use method ‘b’.

On the other hand, if you’re planning on using the object as a Student, might as well refer to it as Student when you instantiate it.

Really, either way is fine. The only thing you’re risking is that might have to cast it later on, and the code will be easier to read without a bunch of casts.

If your teacher says that ‘a’ or ‘b’ is preferable in all situations, she’s wrong.

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [July 10, 2006, 3:25pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/6 "2006-07-10T15:25:11Z")

</div>

> [@friedo](#):
>
> Your teacher is a boob.

On second thought, yeah, what Friedo said.

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [July 10, 2006, 3:38pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/7 "2006-07-10T15:38:43Z")

</div>

> [@Athena](#):
>
> If you need to use the generic class (Person) for, say, passing to a method that specifies ‘Person’ as a parameter, then use method ‘b’.

I would have to double check this but I believe that method A will also work in this situation due to how Java implements polymorphism (dynamic binding).

The only time I would use A is if newStudent could somewhere else be assigned an object of class Person or some other subclass of Person, but the naming in your example makes this seem unlikely.

What level of school is this? What are the credentials of your teacher?

(Teachers are not always right. I had to correct my daughter’s fourth grade teacher when she said that “another name for the Prime Meridian is the International Dateline.”)

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [July 10, 2006, 3:40pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/8 "2006-07-10T15:40:37Z")

</div>

> [@Mayo Speaks!](#):
>
> It’s especially confusing when I type:
> 
> ```auto
> 
> Person aStudent = new Student( "John Doe" );
> aStudent.showCurrentGPA(); // showCurrentGPA() defined in class Student
> 
> ```
> 
> The actual meaning of the code seems very muddled to me, since a generic Person object shouldn’t have a showCurrentGPA() method.

It sounds like your book/teacher is trying to present dynamic dispatch and failing miserably. Shame, really, as that’s one of the most important ideas in object-oriented programming.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [July 10, 2006, 4:04pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/9 "2006-07-10T16:04:11Z")

</div>

If you need methods of the Student class, then obviously you have to use it. If you don’t need methods of the Student class(ie the only methods that you’ll be calling are declared in the Person class), then this really becomes a matter of style\*. When it comes to matters of style, you can’t say that one is “better” than another (well, unless the “style” in question is something ludicrous like using no newlines whatsoever).

However, when it comes to matters of style, you should go with the style your boss mandates. In this case I agree that it’s silly, but is it really worth the effort to fight it?

- Well, I suppose given a sufficiently clever JIT compiler you might see a slight performance enhancement from using A, but it’s also possible that the compiler could be smart enough to apply the same optimization using style B anyway.

---

<div class="post-metadata">

**Author:** ![CookingWithGas](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/cookingwithgas/32/485_2.png) [@CookingWithGas](https://boards.straightdope.com/u/CookingWithGas)\
**Post date:** [July 10, 2006, 8:32pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/10 "2006-07-10T20:32:40Z")

</div>

> [@Rysto](#):
>
> . . .is it really worth the effort to fight it?

Sometimes. I got my first job because I described an argument I had with a teaching assistant in college when I thought I was right about a structured design issue. The hiring manager agreed with me and liked that I would stand up for an idea I thought was right.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [July 10, 2006, 9:11pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/11 "2006-07-10T21:11:43Z")

</div>

I was talking about **style** issues, not **design** issues. Style is mostly a matter of personal preference.

---

<div class="post-metadata">

**Author:** ![ticker](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@ticker](https://boards.straightdope.com/u/ticker)\
**Post date:** [July 10, 2006, 9:52pm UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/12 "2006-07-10T21:52:57Z")

</div>

> [@Mayo Speaks!](#):
>
> I actually think it’s a horrible idea, and it wouldn’t even compile if I were designing the language.

Actually it is very important for an OO language that it should compile.

> [@Mayo Speaks!](#):
>
> It’s especially confusing when I type:
> 
> ```auto
> 
> Person aStudent = new Student( "John Doe" );
> aStudent.showCurrentGPA(); // showCurrentGPA() defined in class Student
> 
> ```
> 
> The actual meaning of the code seems very muddled to me, since a generic Person object shouldn’t have a showCurrentGPA() method.

and that should not.

Style ‘A’ would be appropriate where we are interested in the _Student-ness_ of newStudent. Style ‘B’ when we are only interested in newStudent’s _Person-ness_.

---

<div class="post-metadata">

**Author:** ![Absolute](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Absolute](https://boards.straightdope.com/u/Absolute)\
**Post date:** [July 11, 2006, 12:39am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/13 "2006-07-11T00:39:17Z")

</div>

> [@Mayo Speaks!](#):
>
> Because it’s what the textbook we’re using in class uses. ([Java Outside In](http://www.cambridge.org/us/catalogue/catalogue.asp?isbn=052101087X): a thouroughly terrible book, IMHO) I actually think it’s a horrible idea, and it wouldn’t even compile if I were designing the language. It’s especially confusing when I type:
> 
> ```auto
> 
> Person aStudent = new Student( "John Doe" );
> aStudent.showCurrentGPA(); // showCurrentGPA() defined in class Student
> 
> ```
> 
> The actual meaning of the code seems very muddled to me, since a generic Person object shouldn’t have a showCurrentGPA() method.

That won’t compile.

---

<div class="post-metadata">

**Author:** ![ratatoskK](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ratatoskk/32/2987_2.png) [@ratatoskK](https://boards.straightdope.com/u/ratatoskK)\
**Post date:** [July 11, 2006, 1:07am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/14 "2006-07-11T01:07:58Z")

</div>

Agreed with **Athena**.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [July 11, 2006, 2:34am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/15 "2006-07-11T02:34:54Z")

</div>

> [@Absolute](#):
>
> That won’t compile.

It will if showCurrentGPA() is defined in the Person class.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [July 11, 2006, 2:36am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/16 "2006-07-11T02:36:07Z")

</div>

> [@Rysto](#):
>
> It will if showCurrentGPA() is **defined** in the Person class.

:smack:  
Declared. I meant declared.

---

<div class="post-metadata">

**Author:** ![Jeff\_Lichtman](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/jeff_lichtman/32/1531_2.png) [@Jeff\_Lichtman](https://boards.straightdope.com/u/Jeff_Lichtman)\
**Post date:** [July 11, 2006, 6:02am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/17 "2006-07-11T06:02:20Z")

</div>

One reason you might want to assign the new Student to a Person variable is to take advantage of polymorphism. Consider the following:

Person  
personFactory(String name, int salary) {  
Person newPerson;

```
if (salary == 0) {
    newPerson = new Student(name);
} else {
    newPerson = new Teacher(name, salary);
}

 return newPerson;

```

}

Your teacher might have something like this in mind - that is, in the context where you’re creating the object, there might be a good reason to use a superclass variable.

---

<div class="post-metadata">

**Author:** ![Stealth\_Potato](https://avatars.discourse-cdn.com/v4/letter/s/d78d45/32.png) [@Stealth\_Potato](https://boards.straightdope.com/u/Stealth_Potato)\
**Post date:** [July 11, 2006, 6:27am UTC](https://boards.straightdope.com/t/creating-instances-of-inherited-classes-in-java/363864/18 "2006-07-11T06:27:42Z")

</div>

Polymorphism is the whole reason you would want to refer to objects of derived classes by their common base class - hoard together a bunch of objects irrespective of their specialized types, and take advantage of dynamic binding to allow each to exhibit its specialized behavior.

If all you’re going to do is have one Student object, and use it as a Student object, it would make more sense to give the reference to the object the Student type, so you can access Student’s methods without casting. The whole advantage of the Person-\>Student heirarchy is generality - a Student “is a” Person, an Engineer “is a” Person, so the behavior common to Persons can be kept in the common superclass. The thing to remember is that in your initial declaration (“Student newStudent = new Student(…);”), that first typename is only the type of the reference - a label, a handle to the actual object. It can be as specific to your type as you need it to be.

Polymorphism is very useful for OOP, so you should learn to love it. 🙂  
Oh, and **Rysto** , I think the OP mentioned that showCurrentGPA was declared in the Student class.
