0% found this document useful (0 votes)
1 views8 pages

java programming notes3

The super keyword in Java is used to refer to the parent class, allowing subclasses to call superclass constructors, methods, and fields. It must be the first statement in a subclass constructor and cannot be used in a static context. Java does not support multiple inheritance directly but allows it through interfaces, which can provide a way to implement multiple behaviors without ambiguity.

Uploaded by

diwakarkhushi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

java programming notes3

The super keyword in Java is used to refer to the parent class, allowing subclasses to call superclass constructors, methods, and fields. It must be the first statement in a subclass constructor and cannot be used in a static context. Java does not support multiple inheritance directly but allows it through interfaces, which can provide a way to implement multiple behaviors without ambiguity.

Uploaded by

diwakarkhushi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Super Keyword in Java

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

1. Use of super with Variables


This scenario occurs when a derived class and base class have the same data members. In that case, there is a
possibility of ambiguity for the JVM.
class Vehicle {
int maxSpeed = 120;
}

// sub class Car extending vehicle


class Car extends Vehicle {
int maxSpeed = 180;
void display()
{
// print maxSpeed of base class (vehicle)
System.out.println("Maximum Speed: "
+ super.maxSpeed);
}
}

// 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.

2. Use of super with Methods


This is used when we want to call the parent class method. So whenever a parent and child class have the
same-named methods then to resolve ambiguity we use the super keyword.
class Person {
void message()
{
System.out.println("This is person class\n");
}
}
// Subclass Student
class Student extends Person {
void message()
{
System.out.println("This is student class");
}
// Note that display() is
// only in Student class
void display()
{
// will invoke or call current
// class message() method
message();

// will invoke or call parent


// class message() method
super.message();
}
}
// Driver Program
class Test {
public static void main(String args[])
{
Student s = new Student();

// calling display() of Student


s.display();
}
}

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.

3. Use of super with constructors


The super keyword can also be used to access the parent class constructor. One more important thing is that
‘super’ can call both parametric as well as non-parametric constructors depending on the situation.
class Person {
Person()
{
System.out.println("Person class Constructor");
}
}

// subclass Student extending the Person class


class Student extends Person {
Student()
{
// invoke or call parent class constructor
super();

System.out.println("Student class Constructor");


}
}

// 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.

Creating a Multilevel Inheritance Hierarchy

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

Java Multiple Inheritance


Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more
than one parent class. The problem occurs when there exist methods with the same signature in both the
superclasses and subclass. On calling the method, the compiler cannot determine which class method to be
called and even on calling which class method gets the priority.
Note: Java doesn’t support Multiple Inheritance

// Java Doesn't support Multiple Inheritance


import java.io.*;

// First Parent Class


class Parent1 {
void fun() { System.out.println("Parent1"); }
}

// Second Parent Class


class Parent2 {
void fun() { System.out.println("Parent2"); }
}

// 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();
}
}

Output: Compilation error is thrown

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
}

// Class implementing both interfaces A and B


class C implements A, B {
// Providing implementation for show() from interface A
public void show() {
System.out.println("Show method from Interface A");
}

// Providing implementation for display() from interface B


public void display() {
System.out.println("Display method from Interface B");
}
}

public class Main {


public static void main(String[] args) {
C obj = new C();
obj.show();
obj.display();
}
}
Output:
Show method from Interface A
Display method from Interface B

Explanation:

1. Interface A has one method: show().


2. Interface B has one method: display().
3. Class C implements both interfaces A and B using the implements keyword.
4. The class C provides the definition (body) for both methods.
5. There is no ambiguity because Java enforces that the class must define its own method body

From Java 8, you can:


1. Use default methods in an interface (with body).
2. Use static methods in an interface.

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();

// Static method called using interface name


Vehicle.stop();
}
}
Output:
Car is running at 100km/hr
Vehicle started
Vehicle stopped

You might also like