What is the purpose of the "super" keyword in Java and other similar languages?
It refers to the superclass in inheritance
The question asks about the primary purpose of the "super" keyword in Java and other languages that support similar concepts. The correct answer relates to its role in inheritance.
In languages like Java that support object-oriented programming (OOP concepts), inheritance is a fundamental feature allowing a new class (subclass) to inherit properties and behaviors from an existing class (superclass). The super keyword is specifically designed to help subclasses interact with their immediate superclass.
The primary purpose of the super keyword is to refer to the members (fields and methods) or the constructor of the immediate parent class (superclass) from within the child class (subclass). This is especially useful when members are hidden or methods are overridden in the subclass, or when the superclass requires its constructor to be called.
The super keyword has two main uses in Java inheritance:
When you create an object of a subclass, the constructor of the superclass is also executed. By default, Java automatically calls the default no-argument constructor of the superclass (if it exists). However, if the superclass has parameterized constructors or you need to explicitly call a specific superclass constructor, you use super() with appropriate arguments.
It is important to remember that if you use super() in a subclass constructor, it must be the very first statement.
class Animal { Animal() { System.out.println("Animal constructor"); } Animal(String type) { System.out.println("Animal of type: " + type); } } class Dog extends Animal { Dog() { // Calls the no-arg Animal constructor super(); System.out.println("Dog constructor"); } Dog(String name) { // Calls the parameterized Animal constructor super("Mammal"); System.out.println("Dog named: " + name); } } In the example, the subclass Dog uses super() or super("Mammal") to explicitly call the relevant Java constructors from its superclass Animal.
If a subclass has a field or a method with the same name as a field or method in its superclass, the subclass's version hides or overrides the superclass's version. To access the superclass's version from within the subclass, you use super.fieldName or super.methodName().
This is particularly useful in the context of method overriding, where you might want to extend the behavior of the superclass method rather than completely replace it.
class Vehicle { int speed = 50; void displaySpeed() { System.out.println("Vehicle Speed: " + speed); } } class Car extends Vehicle { int speed = 100; // This hides the superclass speed void displaySpeed() { System.out.println("Car Speed: " + speed); // Refers to Car's speed System.out.println("Vehicle Speed: " + super.speed); // Refers to Vehicle's speed super.displaySpeed(); // Calls the Vehicle's displaySpeed method } } Here, the Car subclass uses super.speed and super.displaySpeed() to access the members defined in the Vehicle superclass, demonstrating how the super keyword helps manage members in inheritance scenarios, including cases of method overriding.
It creates a new object instance: This is incorrect. New object instances are created using the new keyword, followed by a constructor call (e.g., new Dog()).
It defines a new class: This is incorrect. New classes are defined using the class keyword, followed by the class name (e.g., class Dog { ... }).
It handles exceptions: This is incorrect. Exceptions are handled using try, catch, finally, and throw keywords, or declared using the throws keyword.
Therefore, the correct purpose of the super keyword in Java is to reference the immediate superclass, facilitating interaction between parent and child classes within the inheritance hierarchy.
When an applet begins, which of the following sequence methods is CORRECT?
1) start()
2) paint()
3) init()
What will be the output of the following Java program?
class test
{public static void main (String args [ ])
{
System.out.println(20 + 30 + "Java");
System.out.println("Java" + 20 + 30);
}
}
In Java, the 'PreparedStatement' interface is a sub interface which is used to execute __________ query.
Which of the following is NOT a java primitive type?
Which of the following is/are the rules to declare variables in Java?