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
Register | 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 Display Modes
  #1 (permalink)  
Old August 6th, 2007, 08:02 AM
Registered User
Join Date: Aug 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with Polymorphism

Hi friends ! This is my first post in P2P Wrox ! I know VB6 but very new to Java (and OOP).

In the following code, I've created a super class (MySuper) that has a subclass (MySub1).
objSuper is reference of MySuper
objSub1 is a object of MySub1

Both the classes has a method x(). Naturally the method is over ridden in MySub. MySub also has another method y().

Now if I comment the objSuper.y(); line, the code compiles perfectly. But with that line I'm gettin a compile error "cannot find symbol method y()".

I'm reading Just Java 2 (6th Ed) by Pere van der Linden. In the summary of Chapter 8 he wrote "superclass = subclass // always valid"
But then why this code is not working ? I'm really confused.
Please help me. Thanks in advance ! :)
public class Test
    public static void main(String [] args)
        MySuper objSuper;
        MySub1 objSub1 = new MySub1();
        objSuper = objSub1;
        // x() is overridden, y() is NOT overridden 
        objSuper.x(); // OK
        objSuper.y(); // Compile ERROR - "cannot find symbol method y()"
class MySuper
    public void x()
        System.out.println("In MySuper's x()");
class MySub1 extends MySuper
    public void x()
        System.out.println("In MySub1's x()");
    public void y()
        System.out.println("In MySub1's y()");
Reply With Quote
  #2 (permalink)  
Old August 8th, 2007, 05:24 AM
Authorized User
Join Date: Oct 2006
Location: Yerevan, , Armenia.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts

What do you actially need it's just type cast.

Use ((MySub1) objSuper).y(); for calling your method.

This problem occures because after objSuper = objSub1; type of objSub1 becomes MySuper for Java not MySub1 (as it is indeed).
There isn't such a method .y() for MySuper type. That's why Java complains.
Reply With Quote

Thread Tools
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 02:36 AM
What is Interface Polymorphism in C#? ramk_1978 C# 1 May 3rd, 2006 01:23 AM
Polymorphism->overloading? naveen J2EE 2 October 19th, 2005 02:34 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM
Polymorphism and Casts g_r BOOK: Beginning Java 2 1 July 13th, 2003 08:38 AM

All times are GMT -4. The time now is 10:34 AM.

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