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

Below we have implemented program to demonstrate use of "super" and "this" keywords in java and how they play a role in inheritance of classes.

Some important points about "super" and "this" :

  • super is used to access methods, variables of the parent class, and this keyword is used to access methods, variables of the current class.
  • super and this are first statements inside the constructor, we can use super or this as first statemtent inside constructor and not both.

Using Super in constructor:

Code:

public class Parent {

    // Parent class constructor
    public Parent() {
        System.out.println("Parent class constructor");
    }
}
class Child extends Parent{

    public Child(){

        // Calling parent class constructor
        super();
        System.out.println("From parent to child constructor");
    }

    // Main()
    public static void main(String[] args) {
        // Creating child class object
        Child c = new Child();
    }
}

Output:


Using super to access parent class variables:

Code:

public class Parent {

    // Parent class variables
    String var = "Hell0";
    int num = 25;

    // Parent class constructor
    public Parent() {
        System.out.println("Parent class constructor");
    }

}
class Child extends Parent{

    public Child(){

        // Calling parent class constructor
        super();
        System.out.println("From parent to child constructor");
    }

    public void printItems(){
        // Printing parent class variables using super
        System.out.println(super.var + " " + super.num);
    }

    // Main()
    public static void main(String[] args) {
        // Creating child class object
        Child c = new Child();
        c.printItems();
    }
}

Output:

Using this keyword to call constructors of the same class, and using this to assign values of the current class:

Code:

public class Parent {

    // Parent class variables
    private String var;
    private int num;

    // Parent class constructor
    public Parent() {

        // Using this key word to call parameterised constructor of this class
        this("Parent", 25);
        System.out.println("Parent class constructor");
    }

    public Parent(String var, int num) {

        // Using this key word to assign values to the variables of this class
        this.var = var;
        this.num = num;
    }

    public void print(){
        System.out.println(var + " " + num);
    }
}

class Child extends Parent {
    private String var;
    private int num;

    public Child() {
        // Calling Parent default constructor
        super();
    }

    public Child(String var, int num) {

        // Calling parent class constructor and passing attributes
        super(var, num);
        System.out.println("From parent to child constructor");
    }

    @Override
    public void print() {
        // Calling Parent class method using super
        super.print();
    }

    // Main()
    public static void main(String[] args) {
        // Creating child class object
        Child c = new Child("Child", 10);
        c.print();
        Child c2 = new Child();
        c2.print();
    }
}

Output:

#Please ask for any doubts. Thanks.


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