Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 8th, 2006, 12:46 PM
Registered User
Join Date: May 2006
Location: Athens, , Greece.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jimblanc
Default Questions about Inheritance & Polymorphism

Sorry but I have a problem understanding Inheritance and Polymorphism (so chapter 6 in "beginning Java2"
1. why must you define a method in superclass and use it in subclass?
2.i don't understand Object casting...
why should you cast an Object of a subclass to an Object of a superclass? i am asking cause you can use all the methods of the subclass
3.on page 301 we learn about "instanceOf()" and the example is:
Duck aDuck;

if (pet instanceof Duck) {
 aDuck = (Duck) pet;
 pet is a variable of type Animal and you want to cast it to type Duck
 and Book says "if pet does not refer to Duck object, an attempt to cast the object referenced by pet to Duck would cause an exception to be thrown.The code fragment will execute the cast and lay an egg (the method) only if pet does point to a Duck object."

MY QUESTION>>> if pet point to a Duck object why you need to cast it to Duck object...??? I mean that pet is of type Animal and points to Duck... i don't understand it!!!

can anybody help?
Reply With Quote
  #2 (permalink)  
Old December 31st, 2006, 03:54 PM
Authorized User
Join Date: Oct 2003
Location: Jönköping, Jönköping, Sweden.
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mrafaqi Send a message via Yahoo to mrafaqi

The Sense of Inheritence and Polymorphism is quite simple, first forget everything from you mind and consider the following.

class Employee
   /*common attributes in the super class, which will be inherited in the child class
   int socialSecurityNumber = 0;
   Srring firstName, lastName;

   //this method is implemented, note that it has the definition
   //we have also the code in method body, because, we've know
   //already that every Employee can speak the same
   public void speak()
      System.out.println("I'm the Employee, Hahahaha, What is problem");

   //this method is abstract and we have not the parenthesis because,
   //we don't have information about its implementation, so, every
   //class that is extended from it should have to implement that
   public abstract void work();

class Clerk extends Employee
   public void work()

class Manager extends Manager
   public void work()
1. A Super Class is a class that has the most general form of information, like consider you are creating an application of Employees which has different designation you know that in general they will be employees so create a class 'Employee' which is a super class of other designation. it may contains the fields (attributes / data members) like firstName, lastName, socialSecurityNumber etc ... . then other classes like Clerk, Manager will extend this information because we don't want to re-write the all code and don't want to define these data members again in the system using this.

2. Keep one thing in mind, that, it is not necessary to define methods in super class only. but it depends on the situation for example, if you know already in advance that sub classes will contain these specific methods that are common in all then you define it in super class and suppose if the implementation is also the same for example a method of speak() which is off course will be same because, every Employee will be able to speak surely, here you are in position to also to implement the method (coding in the class).

3. if you realize that each child has a different implementation of some method, means, they do a bit different, you then only define in the super class and making this super class abstract and then, in each sub class you override this method. Example is a work() method, which will be different for each Employee since every Employee is supposed to work differently.

4. Casting is a machenisms to support the flexibility in Object Oriented. As you know that if different subclasses could be referrred using the Name of the Super class. For example, you can store the reference of Clerk and Manager object in Employee. see the following code snippet

   public static void main(String[] args)
      Employee emp1 = new Clerk();
      Employee emp2 = new Manager();

the benifits is as we can manipulate every type of sub class object in same way as other. when we need the specific functionality then we cast it as its nature. consider the two following ways

   emp1.speak (); //valid because it is common
   emp1.work (); invalid because actually employees work is not defined, so the correct one is


5. instanceof is used to determine that either the object belongs to specific to that class. it returns true if, false otherwise

    if(emp1 instanceof Clerk())
      return true;
      return false;

it will return true because see the previous code than it.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM
Behavior of "this" & "base" in inheritance guntank C# 7 September 27th, 2006 09:00 PM
I Need Interview Questions & Answers ... keyur_rhtdm .NET Framework 2.0 2 August 5th, 2006 10:55 AM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM

All times are GMT -4. The time now is 05:40 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.