Question

In: Computer Science

What does the super keyword represents and where can it be used? Give an example of...

What does the super keyword represents and where can it be used? Give an example of a superclass and subclass. Be sure to make all the instances variables of the super class private. Include at least one constructor in each class and ensure that the constructor of the subclass calls the constructor of the superclass. Also include a toString method in both classes that returns the values of the instance variables with appropriate labels. Ensure that the toString method of subclass calls the toString method of the superclass so that the string returned contains the values of all the inherited instance variables.

Solutions

Expert Solution

//Shape.java
public class Shape {
    // instance variable
    private String shapeName;

    // Super class constructor
    public Shape(String shapeName) {
        this.shapeName = shapeName;
    }

    public String toString() {
        return "Shape{" +
                "shapeName='" + shapeName + '\'' +
                '}';
    }
}

///////////////////////////////////////////////////////////

//Circle.java
public class Circle extends Shape {
    private double radius;

    //Subclass constructor
    public Circle(double radius) {
        // constructor of the subclass calls the constructor of the superclass
        super("Circle");
        this.radius = radius;
    }

    // toString method of subclass calls the toString method of the superclass
    public String toString() {
        return super.toString()+"\n"+"Circle{" +
                "radius=" + radius +
                '}';
    }
}

///////////////////////////////////////////////////////////

//TestCircle.java
public class TestCircle {
    public static void main(String[] args) {
        Circle circle = new Circle(5);

        System.out.println(circle);
    }
}

Shape{shapeName='Circle'}
Circle{radius=5.0}


Related Solutions

In general Describe when, where, and how is super keyword used in a constructor.
In general Describe when, where, and how is super keyword used in a constructor.
What is CreERT2? Give an example of how it can be used
What is CreERT2? Give an example of how it can be used
Please, give an example of at least 1 case where rational functions can be used in...
Please, give an example of at least 1 case where rational functions can be used in each of the next careers: -Photography -Economics -Medicine -Music Which one uses them the most?
What does the void keyword mean?
What does the void keyword mean?
What does the static keyword mean?
What does the static keyword mean?
What are futures contracts? Give an example of how a futures contract can be used as...
What are futures contracts? Give an example of how a futures contract can be used as protection against commodity price changes. Why did the price of the May WTI futures contract fall to about -$40. Is there a surplus of oil? What does this have to do with the state of the economy? Explain. Note: Each WTI contract is for 1000 barrels of oil, and each barrel contains 42 gallons. Please write at least 10 sentences.
What is Correlation Analysis, give an example of how it can be used in marketing. Cite...
What is Correlation Analysis, give an example of how it can be used in marketing. Cite and reference any sources.
What is Correlation Analysis, give an example of how it can be used in marketing. Cite...
What is Correlation Analysis, give an example of how it can be used in marketing. Cite and reference any sources.
Give an example where the concept of "rate of change" is used in real life.
Give an example where the concept of "rate of change" is used in real life.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT