7 Replies - 173 Views - Last Post: 28 June 2012 - 12:43 AM Rate Topic: -----

#1 Omigosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-June 12

Inheriting classes problem

Posted 27 June 2012 - 05:26 AM

I'm quite a novice to Java and I'm having difficulty getting my superclass (AppletC) able to use a variable declared in my subclass (EventsC)
Here is the relevant code in EventC:

	int mouseClickedX, mouseClickedY;
        
        	public class EventMouseC extends EventsC implements MouseListener, MouseMotionListener{


                        public void mouseClicked(MouseEvent arg0) {
			      mouseClickedX = arg0.getX();
			      mouseClickedY = arg0.getY();
			
		        }
                
                

And here is the relevant code in AppletC:
  EventsC eO = new EventsC();
                eO.mouseCLickedX = 8; //Test  


Is This A Good Question/Topic? 0
  • +

Replies To: Inheriting classes problem

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,250
  • Joined: 05-April 11

Re: Inheriting classes problem

Posted 27 June 2012 - 05:47 AM

Well in the code you gave us, the two variables are not declared inside of EventMouseC. Secondly you dont create a new instance of EventMouseC, but instead you make an instance of EventsC.
Was This Post Helpful? 0
  • +
  • -

#3 Omigosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-June 12

Re: Inheriting classes problem

Posted 27 June 2012 - 02:15 PM

I didn't make this clear:
EventMouseC is inside EventsC which, in turn, is inside AppletC.
I've created EventMouseC in the same file as EventsC.
EventMouseC was supposed to change the value of mouseClickedX, inside EventsC,
which in turn, was to be accessed by the top superclass AppletC.
This would be easier to explain with a whiteboard. :lol:
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5603
  • View blog
  • Posts: 9,045
  • Joined: 19-March 11

Re: Inheriting classes problem

Posted 27 June 2012 - 02:37 PM

View PostOmigosh, on 27 June 2012 - 04:15 PM, said:

I didn't make this clear:
EventMouseC is inside EventsC which, in turn, is inside AppletC.
I've created EventMouseC in the same file as EventsC.
EventMouseC was supposed to change the value of mouseClickedX, inside EventsC,
which in turn, was to be accessed by the top superclass AppletC.
This would be easier to explain with a whiteboard. :lol:


View PostOmigosh, on 27 June 2012 - 07:26 AM, said:

  EventsC eO = new EventsC();
                eO.mouseCLickedX = 8; //Test  


Given this code, it doesn't matter what EventMouseC does, since this is an EventsC you're dealing with.

It doesn't matter what EventMouseC does because inheritance only goes one way - EventsC doesn't inherit from EventMouseC.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Inheriting classes problem

Posted 27 June 2012 - 06:17 PM

View PostOmigosh, on 27 June 2012 - 05:15 PM, said:

EventMouseC was supposed to change the value of mouseClickedX, inside EventsC,

by which magic ?
Was This Post Helpful? 0
  • +
  • -

#6 Omigosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-June 12

Re: Inheriting classes problem

Posted 27 June 2012 - 09:50 PM

So subclasses can't change values in their superclass?
I know how to make my idea work now.

Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5603
  • View blog
  • Posts: 9,045
  • Joined: 19-March 11

Re: Inheriting classes problem

Posted 27 June 2012 - 10:10 PM

Quote

So subclasses can't change values in their superclass?


Well, the question is not exactly right. A subclass has the fields and methods declared in its superclass, so it can change those values. Suppose you have a class Vertebrate which has a few fields:
public static final int NUMBER_OF_LIMBS = 4;
protected static int NUMBER_OF_VERTEBRATES = 0;
protected int numberOfArms = 0;
protected int numberOfWings = 0;


public int getNumberOfLegs()
{
  return NUMBER_OF_LIMBS - (numberOfArms + numberOfWings);
}


And then you have a few subclasses:

public class Toucan extends Vertebrate
{
  public Toucan()
  {   NUMBER_OF_VERTEBRATES ++;  //this is a terrible way to count instances, don't do this!
     this.numberOfWings = 2;
  }
}



public class Gorilla extends Vertebrate
{
  public Gorilla()
  {   NUMBER_OF_VERTEBRATES ++; 
     this.numberOfArms = 2;
  }
}


public class Ox extends Vertebrate
{
  public Ox()
  {   NUMBER_OF_VERTEBRATES ++; 
  }
}




So there it's fine for subclasses to modify fields that they inherit from their parent classes. You'll notice that you can modify a static variable, and it'll always be the same for all instances of Vertebrate. You can modify an instance variable, and that value will belong only to that instance.


What's going on in your example is different. You're not instantiating the subclass. The superclass doesn't know anything about the subclasses, and it doesn't care. (you can break that rule, there are ways, but you shouldn't - it's a bad practice) So your EventsMouseC class is never actually instantiated in the code you've shown us, and nothing you do in it can have anything to do with the program that runs.
Was This Post Helpful? 0
  • +
  • -

#8 Omigosh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-June 12

Re: Inheriting classes problem

Posted 28 June 2012 - 12:43 AM

What about two tier inheritance?

Superclass
|
Subclass
|
SubSubclass

I'm trying to get Superclass to be able to use information generated in SubSubclass.
I've changed it to
EventMouseC Mouse = new EventMouseC();

in EventsC
and
Mouse.mouseClickedX = arg0.getX();

in EventMouseC. I'd rather keep EventMouseC as a seperate class instead of implementing MouseListener in EventsC.
I can't get AppletC to see mouseClicked, though I still have
EventsC eO = new EventsC();
eO.mouseClickedX = 8;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1