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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 4 of 4

Thread: Accessing fields of a subclass

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Accessing fields of a subclass

    If you want a method to intake an object of a superclass type, but you want it to do things differently depending on what subclass it is, is it possible to take input as the superclass, and then refer to fields of the subclass within that method?

    So if i wanted to take an object of type shape, but depending on what shape it might be I want to do things differently... like a circle has a radius,but a rectangle doesnt.

    ie.. (This is a very dodgy pseudo code type explanation of what I want to do..)

    public void method(Superclass object) {

    if (object type Subclass)
    do this and refer to fields in subclass

    else if (object type Subclass 2)
    do this and refer to fields in subclass...

    etc...
    }

    I really want to stick with the input being of the superclass type. Is this possible?


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Accessing fields of a subclass

    Nevermind. I ended up writing the method for each subclass, and defining it as being an abstract method in the super class. This way I could refer to the method using an object from the superclass as input, and still have it do different things depending on what subclass it was.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Accessing fields of a subclass

    In regards to your "very dodgy pseudo code" you would need to use the instanceof operator and then cast the object to the subclass.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Accessing fields of a subclass

    The code which you have provided doesn't work at all coz you want to access subclass related thing with Superclass reference. It will directly give you compile time error if it is not in super class. You need to have the same method in in subclass with super class.

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] how to extend a subclass?!
    By migongotar in forum Object Oriented Programming
    Replies: 4
    Last Post: August 2nd, 2011, 03:22 PM
  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. subclass that implements
    By speedycerv in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2011, 07:35 AM
  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