About Question enthuware.ocajp.i.v7.2.970
Moderator: admin
-
- Posts: 3
- Joined: Thu Oct 31, 2013 3:27 am
- Contact:
About Question enthuware.ocajp.i.v7.2.970
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.
Thanks for the advise.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
This link explains the concept quite well: http://stackoverflow.com/questions/1059 ... a-confused
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Thu Oct 31, 2013 3:27 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
Thank you for the quick answer, this clarifies it!
-
- Posts: 15
- Joined: Sun Jun 01, 2014 8:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
So why would you define an obj variable as a super class of the actual obj being created:
I see this in examples all the time.
Why not just call a Bar a Bar and an ArrayList an ArrayList ...
Code: Select all
Foo f = new Bar();
List myList = new ArrayList();
Inputstream fis = new FileInputStream(source)
Why not just call a Bar a Bar and an ArrayList an ArrayList ...
Code: Select all
Bar b = new Bar();
ArrayList myList = new ArrayList();
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
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:
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.
Code: Select all
void myMethod(Collection c){
for(Object o : c){
System.out.println(o);
}
}
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.
-
- Posts: 15
- Joined: Sun Jun 01, 2014 8:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
Ahhh, code re-use. I will definitely keep that in mind.
-
- Posts: 15
- Joined: Sun Jun 01, 2014 8:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
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?
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?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
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.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?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Sun Jun 01, 2014 8:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.970
You're right.
Thanks (SuperSportsCar)Paul.
Thanks (SuperSportsCar)Paul.
Who is online
Users browsing this forum: No registered users and 57 guests