Show
Home » JAVA Programming » Methods » Question MethodsEasy Questions JAVA ProgrammingJava Basic Basic Syntax Object & Classes Basic Datatypes Variable Types Modifier Types Basic Operators Loop Control Decision Making Numbers Characters Strings Arrays Date & Time Regular Expressions Methods Files and I/O Exceptions Inheritance Polymorphism Abstraction Encapsulation Interfaces Packages Data Structures Collections Generics Serialization Networking Multithreading Applet
Correct Option: Dsuper
Your comments will be displayed only after manual approval. Sometimes you want the subclass to do more than what a superclass’ method is doing. You want to still execute the superclass method, but you also want to override the method to do something else. But, since you
have overridden the parent method how can you still call it? You can use We’ve used super() before to call the superclass’ constructor. There are two uses of the keyword super:
The keyword super is very useful in allowing us to first execute the superclass method and then add on to it in the subclass. In the example below, the Student class overrides the getFood() method of the Person() class, and it uses super.getFood() to call the Person getFood() method before adding on to it. Here, a Person is associated with the food “Hamburger” and a Student is associated with “Hamburger” and “Taco”. Add another subclass called Vegan that inherits from the Student class. Add a Vegan contructor that takes a name as an argument and passes it to the super constructor. Override the getFood() method in Vegan to call the superclass getFood() but add a “No ” in front of it and then say “but ” and add a vegan food. Change Javier to a Vegan object in main() and try it out! How does this work? Remember that an object always keeps a reference to the class that created it and always looks for a method during execution starting in the class that created it. If it finds the method in the class that created it, it will execute that method. If it doesn’t find it in the class that created it, it will look at the parent of that class. It will keep looking up the ancestor chain until it finds the method, all the way up to the Object class. The method has to be there, or else the code would not have compiled. When the student 9-4-2: Given the following class declarations, and assuming that the following declaration
appears in a client program: public class Base { public void methodOne() { System.out.print("A"); methodTwo(); } public void methodTwo() { System.out.print("B"); } } public class Derived extends Base { public void methodOne() { super.methodOne(); System.out.print("C"); } public void methodTwo() { super.methodTwo(); System.out.print("D"); } } You can step through this example using the Java Visualizer by clicking on the following link: Super Example. The toString() method is a common method that is overridden. A subclass can override the superclass toString() method and call the super.toString() before adding on its own instance variables. // overridden toString() in subclass public String toString() { return super.toString() + "\n" + subclassInstanceVariables; } 9.4.1. Programming Challenge : Customer Info¶The Customer class below keeps track of the names and addresses of customers. It has a toString() method that prints out the name and address of the object.
Complete the OnlineCustomer class below to inherit from Customer and add an email address, a constructor, and override the toString() method. 9.4.2. Summary¶
You have attempted of activities on this page Which of this keyword can be used in subclass to call the constructor of super class?The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.
Which of this keyword can be used in a subclass to call the constructor of super class 1 point super this extent extends?1. Which of this keyword can be used in a subclass to call the constructor of superclass? Explanation: None.
Which of this keyword can be used in a subclass to call the constructor of superclass * super this super () extends?super keyword
super is used to refer super-class's instance as well as static members. super is also used to invoke super-class's method or constructor.
Which of this keyword can be used in a subclass to call the constructor of superclass your third question *?We can call the parameterized constructor of the parent class from the subclass using the super keyword.
|