File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism, overriding and overloading

 
rajan sellapan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Larry Olson
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ulrich Vormbrock
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • 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...)
 
Alejandro Herrera
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • 0
  • Mark post as helpful
  • send pies
  • 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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic