Page 1 of 1

About Question enthuware.ocajp.i.v7.2.970

Posted: Fri Nov 08, 2013 1:34 pm
by I_user
Hi, could you give some detailed explanation or share a link regarding to this question. Because as for right answer look pretty odd ...
Thanks for the advise.

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Fri Nov 08, 2013 4:45 pm
by admin
This link explains the concept quite well: http://stackoverflow.com/questions/1059 ... a-confused

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Sat Nov 09, 2013 2:13 am
by I_user
Thank you for the quick answer, this clarifies it!

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Sun Jun 22, 2014 11:31 pm
by Shortrope
So why would you define an obj variable as a super class of the actual obj being created:

Code: Select all

Foo f = new Bar();
List myList = new ArrayList();
Inputstream fis = new FileInputStream(source)
I see this in examples all the time.
Why not just call a Bar a Bar and an ArrayList an ArrayList ...

Code: Select all

Bar b = new Bar();
ArrayList myList = new ArrayList();

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Mon Jun 23, 2014 1:01 am
by admin
That is a very good question. It is always advisable to declare the variables as generic (or less specific) as possible. This allows you the change actual object type without breaking the code. For example, if you code is not dependent on a Collection implementation being ArrayList, you should just declare your variable as Collection. So that if you want to later on use a different implementation of a Collection, there is no impact. The following code illustrates this point:

Code: Select all

void myMethod(Collection c){
  for(Object o :  c){
     System.out.println(o);
  }
}
Now, this code doesn't really depend on methods specific to a Set, or List, or ArrayList. But if you declare the method parameter as ArrayList, other people who want to use it cannot use it if all they have is a Set.

Declaring a variable as generic as possible also prevents use of methods that are specific to a class and tie your code to a specific implementation.

Of course, if you are using methods specific to ArrayList, then you have to declare the variable as ArrayList.


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Mon Jun 23, 2014 1:28 pm
by Shortrope
Ahhh, code re-use. I will definitely keep that in mind.

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Mon Jun 23, 2014 6:50 pm
by Shortrope
I get your point about declaring variables as generic as possible, but after play'n with the Car c = new SportsCar(); question, it seems that the SportsCar object lost its SportsCar attributes (field members) and has been castrated to a lowly Car .. gearRatio = 8. Although its behavior (methods) remain a SportsCar.

Doesn't this open the door for bugs. No compilation or runtime errors but faulty results!
I did find out I can use a cast to get the correct gearRatio for the SportsCar ... ((SportsCar) c).gearRatio

I hate to sound like a whiner but What a Pain... am I missing something?

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Mon Jun 23, 2014 8:37 pm
by admin
Shortrope wrote:I get your point about declaring variables as generic as possible, but after play'n with the Car c = new SportsCar(); question, it seems that the SportsCar object lost its SportsCar attributes (field members) and has been castrated to a lowly Car .. gearRatio = 8. Although its behavior (methods) remain a SportsCar.

Doesn't this open the door for bugs. No compilation or runtime errors but faulty results!
I did find out I can use a cast to get the correct gearRatio for the SportsCar ... ((SportsCar) c).gearRatio

I hate to sound like a whiner but What a Pain... am I missing something?
You have hit upon another good point. This happens because of lack of encapsulation. You should never have public fields in the first place. Member fields should always be private (or protected). Only methods should be publicly accessible.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.970

Posted: Mon Jun 23, 2014 11:27 pm
by Shortrope
You're right.
Thanks (SuperSportsCar)Paul.