About Question enthuware.ocajp.i.v7.2.970

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
I_user
Posts: 3
Joined: Thu Oct 31, 2013 3:27 am
Contact:

About Question enthuware.ocajp.i.v7.2.970

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

This link explains the concept quite well: http://stackoverflow.com/questions/1059 ... a-confused

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

I_user
Posts: 3
Joined: Thu Oct 31, 2013 3:27 am
Contact:

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

Post by I_user »

Thank you for the quick answer, this clarifies it!

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

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

Post 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();

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

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

Post by Shortrope »

Ahhh, code re-use. I will definitely keep that in mind.

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Shortrope
Posts: 15
Joined: Sun Jun 01, 2014 8:27 pm
Contact:

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

Post by Shortrope »

You're right.
Thanks (SuperSportsCar)Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests