Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Superclasses

 
Kieran Webster
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Randall Twede
Ranch Hand
Posts: 4409
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeanne Boyarsky

is most definitifly not a constructor but a method with an unconventional name.
 
Kieran Webster
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic