| Author |
Superclasses
|
Kieran Webster
Greenhorn
Joined: Jan 13, 2013
Posts: 21
|
|
I'm fairly new to Java and I'm trying to learn things bit by bit. I can't get my head around superclasses. I have a brief understanding of what they are, what they do etc but I can't seem to get one working in action.
I started with something basic like this, how would I implement this within the subclass using "super."
I'm not even sure I'm going the right way about it, any help would be appreciated. Thanks in advance.
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Welcome to CodeRanch!
First, your class doesn't compile. The name of the class (Superclass) and constructor (Numbers) needs to match.
Here's a sample subclass. It calls the constructor of the superclass. It also overrides getValue() to do something different - adding 1 to the superclass' value.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
there is no quick answer to this. try going to google.com and typing in "java super keyword"
you will find many tutorials to help you
|
SCJP
|
 |
Manuel Petermann
Ranch Hand
Joined: Jul 19, 2011
Posts: 175
|
|
@Jeanne Boyarsky
is most definitifly not a constructor but a method with an unconventional name.
|
Please correct my English.
|
 |
Kieran Webster
Greenhorn
Joined: Jan 13, 2013
Posts: 21
|
|
Jeanne Boyarsky wrote:Welcome to CodeRanch!
First, your class doesn't compile. The name of the class (Superclass) and constructor (Numbers) needs to match.
Here's a sample subclass. It calls the constructor of the superclass. It also overrides getValue() to do something different - adding 1 to the superclass' value.
What would I call in main in order to make this run properly? Also what's the difference between actually calling the super class, and just using the method within the subclass? Does it provide any benefits?
Randall Twede wrote:there is no quick answer to this. try going to google.com and typing in "java super keyword"
you will find many tutorials to help you
I've read up on them, I sort of get how they work, but I thought I'd learn better with people who are experienced so that's why I came here.
Manuel Petermann wrote:@Jeanne Boyarsky
is most definitifly not a constructor but a method with an unconventional name.
I understand that, but is it not possible to call a method using "super.", I've seen examples online where they have called a method from the superclass so that's where I got this idea from. Sorry if it's a disgrace
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Manuel Petermann wrote:@Jeanne Boyarsky
is most definitifly not a constructor but a method with an unconventional name.
Ah. Didn't catch that. I edited my post to make sense.
Kieran: This is why you want to follow standard Java naming conventions and begin methods with lowercase names. Experienced programmers will assume you did.
|
 |
Kieran Webster
Greenhorn
Joined: Jan 13, 2013
Posts: 21
|
|
Jeanne Boyarsky wrote:
Manuel Petermann wrote:@Jeanne Boyarsky
is most definitifly not a constructor but a method with an unconventional name.
Ah. Didn't catch that. I edited my post to make sense.
Kieran: This is why you want to follow standard Java naming conventions and begin methods with lowercase names. Experienced programmers will assume you did.
Alright, I've changed the method to
How would I call this in the main method of Subclass to get a value?
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
And you are correct that there is no point in having a subclass if it doesn't change something about the superclass.
|
 |
Kieran Webster
Greenhorn
Joined: Jan 13, 2013
Posts: 21
|
|
Jeanne Boyarsky wrote:
And you are correct that there is no point in having a subclass if it doesn't change something about the superclass.
Thanks, I still don't really get it, but I'll mess around and read up on them more and hopefully I'll get the hang of it.
|
 |
 |
|
|
subject: Superclasses
|
|
|