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?
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
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.
Correct me here. This refers to the closest instance of an object right?
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...
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 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.
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.
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.