• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

polymorphism, overriding and overloading

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain me with good code example, I was not able to understand in K&B book the below definition?

Polymorphism [Objective 5.2]

The reference variables type ( not the object's type) determines which methods can be called

Overriding and Overloading [Objective 1.5 and 5.4]

Object type ( not the reference variables type) determines which overridden method is used at runtime.

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajan,

I'm in hurry, so please study my following code which I've prepared for you in order to explain issues such as polymorphism, overrriding and overloading:



The output is the following:

LaserPrinter prints 3 pages
Printer does tilt
Printer does second tilt
LaserPrinter does klimbim within 10 seconds



Ok, maybe it's a stupid example, but in any case, you can study the differences between reference type (left-side) and instance type (right-side).
Please take into account, that the instance type must pass the IS A test according to the reference type:
as you can see, LaserPrinter IS A Printer (the contrary is not true, a Printer IS NOT a LaserPrinter, an Animal IS NOT a cat, etc.)

If something remains unclear, I'll come back to you tomorrow!
(My guests are already waiting for dinner...)
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rajan sellapan wrote:Can anybody explain me with good code example, I was not able to understand in K&B book the below definition?

Polymorphism [Objective 5.2]

The reference variables type ( not the object's type) determines which methods can be called

Overriding and Overloading [Objective 1.5 and 5.4]

Object type ( not the reference variables type) determines which overridden method is used at runtime.




Remember:

class A{}
class B extends A{}

if you have:

A variable = new B();

methods class B are called
attributes classA are called

good luck
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a way to think about what happens at compile time and then later at run time.
The declared variable type determines what methods are available to be called - the
type's list of method behaviors. The compiler checks your code for valid calls to the
variable type's methods and allows calls on any type that has at least those behaviors.
It doesn't care that it may have more methods, or overrides of some of the variable
type's methods. Later at run time, the JVM gives first shot to any overriding method
the assigned object may have.

Jim ... ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic