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...
Java Apply inheritance to write a super class and subclass to compute the triangle area and...
Java Apply inheritance to write a super class and subclass to compute the triangle area and the surface area of triangular pyramid, respectively. Assume that each side has the same length in the triangle and triangular pyramid. You need also to override toString() methods in both of super class and subclass so they will return the data of an triangle object and the data of the pyramid object, respectively. Code a driver class to test your classes by creating at...
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)
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.
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.
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.
Java Questions involving OOP: (Include java coding examples if possible) (1) What is an exception type...
Java Questions involving OOP: (Include java coding examples if possible) (1) What is an exception type an instance of? (2) What are the conditions when a class implements an interface? (3) What is/are the conditions of creating an abstract class object from its derived concrete class?
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT