On Apr 1, 10:26 pm, "[email protected]" <[email protected]> wrote:
> Hi All,
>
> I apologize if this question has been answered, but I've read all the
> lessons and supporting material (up to "inner class," at least) and I've
> spoken to a colleague who has taken a similar intro to Java class, but I
> still can't figure this out.
>
> My question is this:  I know that it's possible to do have a simple class
> hierarchy like this:
>
> Animal
> -----Dog
>
> And to create an object like this:
>
> Animal dog1 = new Dog();
>
> But I'm curious as to why you would do that when you can just write:
>
> Dog dog1 = new Dog();
>
> because it seems like either way the compiler would know that a Dog object
> is a subclass of Animal, right?  Is it partially for code readability?
>
> Can anyone give me an example of where you'd want to use one method over the
> other, and what the benefits (e.g. performance) might be when using one
> method over the other?
You may search the forum, this question has been answered recently in
great details.
However, here is a reason why you may want to use the superclass to
define an instance.
Say you have a set of person, that is a Person class. Now you have a
set of managers, that is a Manager class. Obviously Manager is a
subclass of Person. Now you have a set of employees, that is an
Employee class. Employee is also obviously a subclass of Person. And
seeing from the point of view of the Entreprise (a new class), a
manager -aka an instance of the Manager class) is also an employee (an
instance of the Employee class).
But you cannot say directly Brian is a Manager and an Employee; as
with Dog dog1 = new Dog(), on the contrary you may say: Person brian =
new Manager(), that's just a way to make brian viewed as a person who
is also a manager; and as he is a person, you may thereafter view him
as an employee.

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to