Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware question

 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following classes, what will be the output of compiling and running the class Truck?

My thought for this question
Truck: drive
Truck: drive
Truck: drive

Because the method drive is overriding and changes the methods body. But the answer is

Automobile: drive
Truck: drive
Truck: drive


Why Automobile: drive not Truck: drive for first output? Isnt is overriding?
And i have also problems with casting and polymorphism . Can you suggest some links which explain them. I read the OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide two times.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:Why Automobile: drive not Truck: drive for first output? Isnt is overriding?

The actual object is Automobile, so why would you expect to execute a method of the Truck class. You create a new Automobile not a Truck. It's the type of the actual object (in this case Automobile) which determines which method is executed. If you have an Automobile instance, it's completely impossible to execute a Truck instance method.

Nil. Hatamova wrote:And i have also problems with casting and polymorphism . Can you suggest some links which explain them. I read the OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide two times.

Do you only read this study guide? Or do you also write lots of code snippets (while reading)? You could buy another study guide to use as a complementary resource. You could have a look at the Oracle Java tutorials. You can look in this forum for threads about casting and polymorphism.

These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about casting:
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class


  • And using the search function you'll find a similar list for overriding too:
  • Overriding static methods
  • overriding Interface variables
  • Question about static or private method hidden/redefined in subclass
  • Confused about static and instance methods with same name (with an excellent explanation about which rules you have to follow when hiding a static method)
  • Exceptions, overriding and constructors
  • Doubt with overridden methods
  • Access Modifiers in base and derived class and changing output


  • Hope it helps!
    Kind regards,
    Roel
     
    Ramya Subraamanian
    Ranch Hand
    Posts: 178
    17
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    also check this, it has info about upcasting and downcasting.
    http://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/14%20Java%20Upcasting%20Downcasting.htm
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic