• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in SCJP textBook

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi frnds

I got a doubt while reading SCJP Text Book by Keithy Seirra.

please go through this

Class Car()
{
}

class Honda extends Car()
{
}

Car c[];Honda h[];
its said that
c=h;It can be assigned directly but h=c, needs type casting.

I got a doubt say, h Honda instance has some extra features that in car,
if we assign that to car, those features will be lost.Then what is the use of doing like that.

We usually do List l1= new ArrayList() rather than
ArrayList l1= new ArrayList(); Why?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if we assign that to car, those features will be lost.Then what is the use of doing like that.



Well, those "features" are not lost. For overriden methods, it is the Honda version that will be called. For new methods, then you could always cast it back to Honda before calling it.

Now why would we want to do this? Generally, you write code using Car, instead of Honda, because you don't need those "features". Think of it this way. Your methods that take a Car, and only work with the Car, can not only work with Honda, but can also work with Toyota, Ford, GM, BMW, Mercedes, KIA, Lexus, etc.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic