Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 6th, 2007, 08:02 AM
Registered User
Join Date: Aug 2007
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()");
Old August 8th, 2007, 05:24 AM
Authorized User
Join Date: Oct 2006
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.

Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03: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

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