Question

In: Computer Science

Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of...

Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables.

Solutions

Expert Solution

The example below shows the use of "super" and "this" keyword" as a variable.

The code produces the following output :

Notice that both child class and parent class contained the variable - name of type String. So the variable name of parent class can be accessed only by using keyword - "super". Similarly, "this" keyword is used to access the variable of the same class int the above example.

"super" and "this" as constructor -

Calling one constructor from another constructor is termed as Constructor chaining.

"super" is used to invoke constructor of parent class and "this" is used for invoking some other constructor of same class.

Note that constructor calls should be the first line in constructor to prevent compile time error.

output :

Also, the java compiler automatically invokes the non-paramterized constructor of parent class, whether you insert "super()" as the first line in constructor or not.

"super" keyword used as constructor :

output :


Related Solutions

Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of using "super" and "this" both as constructors and as variables.**
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
Give 2 examples showing how competing equilibrium reactions affect one another. ( Le Chatlier's Principle)
Give 2 examples showing how competing equilibrium reactions affect one another. ( Le Chatlier's Principle)
Discuss Communication and give examples of how you personally demonstrate this SLO. Include how you utilize...
Discuss Communication and give examples of how you personally demonstrate this SLO. Include how you utilize scholarly writing, collaboration, and information management to provide and promote cultural competence and population health.
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples...
What are wrapper classes and why are they useful for ArrayLists? In your answer, include examples of autoboxing and unboxing. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
Inflammation and tumorigenesis are linked. Give 3 examples showing the linkage and why for each of...
Inflammation and tumorigenesis are linked. Give 3 examples showing the linkage and why for each of those examples ?
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
How to identify when to use super() in constructor for java language in a easy way?
How to identify when to use super() in constructor for java language in a easy way?
Give chemical examples (draw) for each term and construct a diagram showing the relationship between the...
Give chemical examples (draw) for each term and construct a diagram showing the relationship between the following terms: constitutional isomer, geometric isomer, stereoisomer, and conformer. (Please make explanation as detailed as possible, thank you!)
explain the principle of the inheritance give an example( CS related) of class inheritance
explain the principle of the inheritance give an example( CS related) of class inheritance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT