Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: a superclass variable can reference a subclass but here why cant cv access ???

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Default a superclass variable can reference a subclass but here why cant cv access ???

    class A {
    int i;

    // Create a subclass by extending class A.
    class B extends A {
    int i; // this i hides the i in A

    B(int a, int b) {
    super.i = a; // i in A
    i = b; // i in B

    void show() {
    System.out.println("i in superclass: " + super.i);
    System.out.println("i in subclass: " + i);

    class UseSuper {
    public static void main(String args[]) {
    B subOb = new B(1, 2);
    A cv;

    Last edited by prajwalfc; June 20th, 2012 at 05:49 AM. Reason: nothing

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: a superclass variable can reference a subclass but here why cant cv access ???

    When posting code, please use the highlight tags. And please post the specific error you're talking about. I had to copy and paste this code just to know what you were asking. Make it easy for people to help you!

    The reason that cv can't access the show() method is because show() is only defined for instances of B. Although cv happens to be an instance of B, there's no guarantee that that's the case when you try to call the show() method. You could also have defined cv as an instance of A, or some other class that extends A but doesn't have the show() method.

    This example demonstrates the same concept:

    class A {
    	public static void main(String args[]) {
    		String strString = "test";
    		Object objString = strString;
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Problem with subclass and superclass methods
    By Farmer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2011, 08:43 PM
  2. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  3. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM
  4. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM