java programming notes3
java programming notes3
The super keyword in Java is a reference variable that is used to refer to parent class when we’re working
with objects.
Characteristics of Super Keyword in Java
In Java, super keyword is used to refer to the parent class of a subclass. Here are some of its key
characteristics:
super is used to call a superclass constructor: When a subclass is created, its constructor must call
the constructor of its parent class. This is done using the super() keyword, which calls the constructor
of the parent class.
super is used to call a superclass method: A subclass can call a method defined in its parent class
using the super keyword. This is useful when the subclass wants to invoke the parent class’s
implementation of the method in addition to its own.
super is used to access a superclass field: A subclass can access a field defined in its parent class
using the super keyword. This is useful when the subclass wants to reference the parent class’s
version of a field.
super must be the first statement in a constructor: When calling a superclass constructor, the
super() statement must be the first statement in the constructor of the subclass.
super cannot be used in a static context: The super keyword cannot be used in a static context,
such as in a static method or a static variable initializer.
super is not required to call a superclass method: While it is possible to use the super keyword to
call a method in the parent class, it is not required. If a method is not overridden in the subclass, then
calling it without the super keyword will invoke the parent class’s implementation.
Overall, the super keyword is a powerful tool for subclassing in Java, allowing subclasses to inherit and
build upon the functionality of their parent classes.
Use of super keyword in Java
It is majorly used in the following contexts as mentioned below:
Use of super with Variables
Use of super with Methods
Use of super with Constructors
// Driver Program
class Test {
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
Output
Maximum Speed: 120
In the above example, both the base class and subclass have a member maxSpeed. We could access the
maxSpeed of the base class in subclass using super keyword.
Output
This is student class
This is person class
In the above example, we have seen that if we only call method message() then, the current class
message() is invoked but with the use of the super keyword, message() of the superclass could also be
invoked.
// Driver Program
class Test {
public static void main(String[] args)
{
Student s = new Student();
}
}
Output
Person class Constructor
Student class Constructor
In the above example, we have called the superclass constructor using the keyword ‘super’ via subclass
constructor.
class A {
void funcA() {
System.out.println("This is class A");
}
}
class B extends A {
void funcB() {
System.out.println("This is class B");
}
}
class C extends B {
void funcC() {
System.out.println("This is class C");
}
}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
obj.funcC();
}
}
Output:
This is class A
This is class B
This is class C
// Inheriting Properties of
// Parent1 and Parent2
class Test extends Parent1, Parent2
{
// main method
public static void main(String args[])
{
// Creating instance of Test
Test t = new Test();
t.fun();
}
}
Conclusion: As depicted from code above, on calling the method fun() using Test object will cause
complications such as whether to call Parent1’s fun() or Parent2’s fun() method.
Multiple inheritances can be achieved through the use of interfaces. Interfaces are similar to classes in that
they define a set of methods that can be implemented by classes. Here’s how to implement multiple
inheritance using interfaces in Java.
An interface is a collection of abstract methods (without body).
A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
This solves the diamond problem by enforcing method implementation in the derived class.
Interfaces cannot be used to create objects.
Interface methods do not have a body - the body is provided by the "implement" class
On implementation of an interface, you must override all of its methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to create objects)
In Java, an interface is a reference type that is similar to a class, but it is a blueprint of a class that
contains only abstract methods (before Java 8). However, from Java 8 onwards, it can also have
default methods and static methods with a body.
By default, all methods in an interface are public and abstract.
All the variables declared in an interface are public, static, and final (constant).
An interface cannot have a constructor because it cannot be instantiated.
It is used to achieve multiple inheritance.
The class that implements the interface must override all the methods of the interface.
It Provides Loose Coupling between classes. Loose coupling in Java refers to reducing the
dependency between classes, meaning one class is not heavily dependent on another class for its
functioning. In loose coupling, if you change one class, it will have minimal or no effect on another
class.
Example:
interface A {
void show(); // Abstract method
}
interface B {
void display(); // Abstract method
}
Explanation:
interface Vehicle {
void speed();
// Default method
default void start() {
System.out.println("Vehicle started");
}
// Static method
static void stop() {
System.out.println("Vehicle stopped");
}
}
class Car implements Vehicle {
public void speed() {
System.out.println("Car is running at 100km/hr");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.speed();
c.start();