File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes polymorphism, overriding and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism, overriding and overloading " Watch "polymorphism, overriding and overloading " New topic
Author

polymorphism, overriding and overloading

rajan sellapan
Greenhorn

Joined: Mar 24, 2010
Posts: 9
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

Joined: Feb 03, 2009
Posts: 142
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
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...)


SCJP 6 (88%), SCWCD (89%)
Alejandro Herrera
Greenhorn

Joined: May 12, 2010
Posts: 13
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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism, overriding and overloading
 
Similar Threads
Mock exam question
Method Overloading and overriding
Overloaded question
Polymorphism : unexpected result ! why
question