13 Replies - 1596 Views - Last Post: 05 February 2009 - 02:42 PM Rate Topic: -----

#1 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Subclasses, Inheritance and Overriding

Posted 04 February 2009 - 09:58 AM

Hi,

Iím new to Java (and this is my first post). Iím having a little difficulty with subclasses, inheritance and overriding.

Hereís my problem/dilemma.

I have two classes A and B:

public class A
public class B extends A

now say A includes this:

   public void setPad (int aPad)
   {
	  pad = aPad;
	  this.update("pad");
   }



Now with B I want to override the inherited method from A but with a condition such as if (x < y).

So basically. I want to run the method from class A and then in B check the value and create a condition from that.

So in class B I would put something like:

public void setPad(int aPad)
   {   
/*Do I need to put something like super(); here?*\
	 if (this.getPad() > 0)
	 {
		this.setPad(aPad)
	  }
   }



Also what if the method I described in class A was actually in another class but inherited by class A would that make a difference? So:-

public class x
public class A extends x
public class B extends A

Iím sorry if this doesnít makes sense, itís hard to put down what you are thinking into words.

Thanks
Andy.

Is This A Good Question/Topic? 0
  • +

Replies To: Subclasses, Inheritance and Overriding

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 04 February 2009 - 10:05 AM

nope, super() not required in the subclass, you only use super when you want to call the constructor of the superclass, if you wanna override a method in the subclass you just change the method body as you like
public void setPad(int aPad)
{  
	 if (this.getPad() > 0)
	 {
		this.setPad(aPad)
	  }
}


you were correct ;)..as for the other part of the question, if A inherits from x then all the instance fields and methods of x will be inherited to A, when B inherits from A, the methods and fields of A and x will be inherited to B..in other words, a subclass inherits the fields and methods of all the superclasses..in case if you dont get it
http://java.sun.com/...I/override.html

This post has been edited by mostyfriedman: 04 February 2009 - 10:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 04 February 2009 - 04:56 PM

Thanks for your response. I will have a read of the link you posted tomorrow.

But again, thanks for confirming my question so quickly!

Andy.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 04 February 2009 - 05:10 PM

no problem ;)
Was This Post Helpful? 0
  • +
  • -

#5 c0mrade  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Subclasses, Inheritance and Overriding

Posted 04 February 2009 - 05:28 PM

To access a method that has been overridden by the subclass, you use 'super' instead of 'this'. So your code should probably be:

super.setPad(aPad);


And yes, if the method you reference in super was defined in the superclass of the superclass, you access it in the same way (unless it was private of course).
Was This Post Helpful? 0
  • +
  • -

#6 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 01:27 PM

Thanks for the responses, most helpful. I've had another though what if I want to use a return from a method in a superclass in a condition in the subclass?

So...


public class A extends B

   public String setCondition(int aV)
   {
	  if ((this.getFromB) <= (aV))
	  {
			/** An Action */
	  }
   }



Would something like this work?

Thanks in advance.

Andy.
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 01:37 PM

you will have to call the method of the super class in the subclass method use the super keyword

use super instead of this
Was This Post Helpful? 0
  • +
  • -

#8 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 01:44 PM

I have tried that idea but I get an error whilst compling. It says 'cannot find symbol - variable getFromB.

What does this actually mean?

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 01:49 PM

means that getFromB doesnt exist, make sure that you have that method written and make sure that you spell it correctly..for more info about calling methods and stuff
http://www.faqs.org/...avap/c5/s5.html
Was This Post Helpful? 0
  • +
  • -

#10 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 01:58 PM

Just to try and make it clear to what I'm trying to acheive this is my idea what the code is like.


/** These methods are in class B*/

public void setAnswer(int aAnswer)
	 {
		 this.andwer = aAnswer;
		 this.update("answer");
	 }

 public void getAnswer()
	 {
		 return this.answer;
	 }


/** Then I have this*/

public class A extends B

   public String setCondition(int aV)
   {
	  if ((super.getAnswer) <= (aV))  /** I want to call the return and compare it */ Is this possible?
	  {
			/** An Action */
	  }
   }




Ah bloody hell! Just realised what I was doing wrong (I think). I was forgetting to put the () after super.getAnswer so I wasn't calling the method. I think that's what it is anyway!

Thanks for the help once again. I'm sure i'll be back!
Was This Post Helpful? 0
  • +
  • -

#11 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 02:16 PM

Is it possible to compare a condition to a boolean? Such as

public class A extends B

   public String setCondition()
   {
	  if ((super.getAnswer()) = (??))  /** What do I put where ?? is.*/ I
	  {
			super.aState = true;
	  }
   }



In class B there is a method which gets the boolean. The boolean is called aState and if it equalsfalse then I want to change it to true

How do I go about comparing the boolean with it's state?

Thanks
Andy.

This post has been edited by Andy C: 05 February 2009 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 02:20 PM

put false in your if statement

This post has been edited by mostyfriedman: 05 February 2009 - 02:22 PM

Was This Post Helpful? 0
  • +
  • -

#13 Andy C  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-January 09

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 02:22 PM

Okay, ignore the above post again. I've manged to work it out. I've now realised it needs to be

public class A extends B

public String setCondition()
{
	 if (super.getAnswer() == false)
	 {
		  super.setAnswer(true);
	 }
}



Was This Post Helpful? 0
  • +
  • -

#14 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Subclasses, Inheritance and Overriding

Posted 05 February 2009 - 02:42 PM

alrighty then well done, you pick up things quicker than a lot of the new programmers around this forum and you actually "THINK" as opposed to a lot of people :P...you keep up that good work ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1