• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting to know keyword super  RSS feed

 
Ranch Hand
Posts: 318
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have always had issues with supper, and inheritance when I was taking my java classes. Going through my udemy lessons super shows up again, but this time in javascript. From what I can tell super is used to avoid duplication of code in the constructor function?

lets say I have a car with make model and year


and a motorcycle class with the same arguments


So correct me if im wrong here, but super is just calling below??


what if for some strange reason I wanted to add another argument to my motorcycle in addition to the constructor im getting from the car class?
 
Ranch Hand
Posts: 68
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

super allows you to access stuff in the superclass. If you have variables by the same name (make, model, year)
in the superclass (car) and in the subclass (motorcicle), then from an instance of motorcicle you can access the
motorcicle variables using this, and the car variables using super. Since constructors are not inherited, you
can access the superclass constructor with super and the subclass constructor with this
 
Sheriff
Posts: 12103
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mano Ag wrote:...the subclass constructor with this


That doesn't sound right. The this keyword refers to the current instance.  If it used as part of a method call as in, this.someMethod() and that method is a non-final instance method, then it will get resolved at runtime to the appropriate overridden version of whatever subclass this refers to.  Your design, however, should not make any assumptions as to which subclass this actually is. If it does, that's a big code smell.

If this is used in a constructor and is passed parameters or is used like this(), then it will refer to the current class' constructor with a matching argument list.

Maybe you meant something else? If you did, please clarify.
 
Marshal
Posts: 59453
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . That doesn't sound right. . . . .

I think, “subclass” means, “subclass of the superclass,” which, I hope, is the same class we are in at present. I agree, it isn't clear the way it was written.
 
Bartender
Posts: 617
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want another variable for Motorcycle, you could do something like this:
where xxx, yyy, and zzz are the type declarations for make, model, and year, etc.
 
Mano Ag
Ranch Hand
Posts: 68
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think, “subclass” means, “subclass of the superclass,” which, I hope, is the same class we are in at present



That's what I meant
 
Cody Biggs
Ranch Hand
Posts: 318
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, let me see if I got this. I call super in the motorcycle class getting the same arguments as the car class. In my motorcycle class I can use this.make and that will refer to the motorcycle? Same as I say this.make = make in the car class it refers to the car. This has always confused me a little.

Correct me here. This refers to the closest instance of an object right?
 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, lets start off by making a more practical example. Let's say we have a Car class like so.

Then, let's say we want to extend the Car class with a subclass Sedan (fyi: a sedan is a four doored car.)


As you can see, we used the super keyword twice in this example. Once for passing Sedan's constructor arguments to the superclass Car's constructor, and also to call the Car's implementation of the toString method. super can also be used to refer to an non-static instance variable in the superclass. These are essentially the three functions of the super keyword.
Note: If you call the super constructor from within a subclass' constructor, the call to super must be the first statement declared. For example...



 
Cody Biggs
Ranch Hand
Posts: 318
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. This.type is referring to the sedan type correct. The this keyword refers to the closest object?
 
Junilu Lacar
Sheriff
Posts: 12103
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Arnold wrote:Then, let's say we want to extend the Car class with a subclass Sedan (fyi: a sedan is a four doored car.)


This code won't compile (line 14 is trying to access a private member of the superclass)

Also, the return value of toString() can't be void, it has to be String
 
Junilu Lacar
Sheriff
Posts: 12103
197
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cody Biggs wrote:The this keyword refers to the closest object?


Not sure what you mean by "closest" object.  The this keyword here refers to the current object.
 
Eric Arnold
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
This code won't compile (line 14 is trying to access a private member of the superclass)

Also, the return value of toString() can't be void, it has to be String



Ahh right you are on both counts. Sorry for the confusion OP!
 
Cody Biggs
Ranch Hand
Posts: 318
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Cody Biggs wrote:The this keyword refers to the closest object?


Not sure what you mean by "closest" object.  The this keyword here refers to the current object.



Right. This in the car class will always point to the car class, and this in the subclass or any other class will point to that class.

I read somewhere that if there is no object instance or something that ‘this’ will point to the window object. Which I don’t think I’ve ever heard the window object mentioned before.
 
Author and ninkuma
Marshal
Posts: 66730
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cody Biggs wrote:I read somewhere that if there is no object instance or something that ‘this’ will point to the window object.


Which I don’t think I’ve ever heard the window object mentioned before.



The window object is the global context for a web page.

What the keyword this points to in a function is called the function context, and what it actually points to is much more complex in JavaScript than it is in Java. In JavaScript, the function context is determined by how the function is called, rather than how the function is declared.

The discussion on this would fill a chapter, so I'll self-servingly push my book Secrets of the JavaScript Ninja which does have four whole chapters exclusively devoted to functions in JavaScript.

Understanding how functions work in JavaScript is the key to writing anything other than mundane JS code.

To remark upon your question: when a function is called directly, rather than through the object within which it is defined, the function context defaults to the window object. It's the difference between treating a function as a method of an object or not.
 
I've never won anything before. Not even a tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!