Super keyword and Casting. In English

                                   Super keyword and Casting

Super keyword............


In Java, parent class objects can be referred to using the super keyword, which is a reference variable. To grasp the Java super keyword, one must have a basic understanding of inheritance and polymorphism. When inheritance was introduced, the term "super" entered the picture.

To refer to the parent class of a subclass in Java, use the super keyword. Its features include the following: -

  • The constructor of a subclass must call the constructor of its parent class when it is built; this is done using super. The parent class constructor is called via the super () keyword to do this.
  • A subclass can use the super keyword to invoke a method defined in its parent class. When a subclass wishes to call the parent class's implementation of the method in addition to its own, this is helpful.
  • Super is the keyword used to access a superclass field: A subclass can use the super keyword to access a field defined in its parent class. When a subclass wishes to use a field that is part of the parent class, this is helpful.
  • If a subclass constructor is called, the super () statement needs to appear first in the constructor before executing the constructor of the superclass.
  • The super keyword is not permitted to be used in static contexts, such as static variable initializers or static methods.

Using Java's super keyword

1.Use of super keyword with variables

When the parent class and a child class share the same data members, the super keyword can be used here to implement the data member of the parent class.

Ex: -

                         class Bicycle {

                         int speed = 45;

                         }

                        class Motorcycle extends Bicycle {

                        int speed = 100;

 

                         void display ()

                        {

                        System.out.println("Minimum Speed: " +super.speed);

                         }

                        }

                       class Test {

                        public static void main(String [] args)

                         {

                      Motorcycle m = new Motorcycle ();

                       m.display();

                       }

                       }

output - Minimum speed: 45 

2.Use of super keyword with methods

This is used to call the parent class method. So use super keyword to call parent class method when parent and child class have same named methods.

Ex: -
              class Tescher {
              void message()
              {
                 System.out.println("This is Teacher class\n");
              }
              }
             class Student extends Teacher {
             void message()
             {
                 System.out.println("This is student class");
             }
             void display()
             {
             message();
             super. message();
             }
             }
             class Test {
                public static void main(String args[])
             {
             Student s = new Student();
             s.display();
             }
             }

output - This is Teacher class
             This is student class

3.Use of super keyword with constructors

The super keyword can also be used to access the constructor of the parent class. 'super' keyword can call both parametric and non-parametric constructors depending on the situation.

Ex: -
              class Teacher {
              Teacher()
              {
                  System.out.println("Teacher class Constructor");
              }
             }
 
             class Student extends Teacher {
             Student()
             {
              super();
               System.out.println("Student class Constructor");
              }
             }
            class Test {
              public static void main(String [] args)
            {
              Student s = new Student();
             }
            }

output - Teacher class constructor
             Student class constructor

Advantages of Using Java super keyword

  • Subclasses can inherit functionality from their parent classes by using the super keyword, which encourages code reuse and minimizes duplication.
  • Super keyword allows subclasses to access attributes from their parent classes and override methods, making polymorphism feasible. This makes programming more expandable and versatile.
  • With the super keyword, subclasses can leverage pre-existing behavior without having to reimplement it by accessing and using methods and fields provided in their parent classes.
  • Subclasses can extend and modify the behavior of their parent classes by overriding methods and calling the parent implementation via super keyword.
  • Super keyword allows subclasses to concentrate on their own activity while depending on the parent class to handle lower-level aspects, hence promoting encapsulation and abstraction.
Casting..............


Casting is the process of assessment a primitive data type's value into another type.
 In java, there are two types of casting namely upcasting and downcasting .

1.Upcasting
Casting a subclass variable upward to the inheritance of a superclass variable is known as upcasting. A superclass reference variable refers to a subclass object; this is an automatic process for which no effort is required. It is comparable to dynamic polymorphism.
Cast syntax is not used in implicit casting, which is the compiler's method of class typecasting.
Cast syntax is used by programmers to perform class typecasting, often known as explicit casting.

                           


Ex: -
 
                     class Animal { 
                     void display ()
                     {
                        System.out.println("Animal class display method is called");
                      }
                     }  
                    class Dog extends Animal {
                    void display () 
                    {
                       System.out.println("Dog class display method is called");
                     }
                    }
                    class Test {
                      public static void main(String [] args)
                    {
                       Animal a = new Dog ();
                       a.show();
                     }
                    }
output - Dog class display method is called

2.Downcasting
Downcasting is the Java term for the process of changing an object of a parent class to an object of a child class. The cast operator is used expressly by the programmer to do this. There can be no implicit downcasting.


Ex: -
                 class Animal {
                  }
                 class Cat extends Animal {
                 static void run (Animal v)
                  {
                 if (a instanceof Cat) {
                 Cat c = (Cat)v;

                    System.out.println("Downcasting performed");
                   }
                 }
                    public static void main(String [] args)
                 {
                 Animal v = new Cat ();
                 Cat.run(v);
                 }
               }

Difference between Upcasting and Downcasting...................





Comments

Popular posts from this blog

Lambda expression in Java. In English

Loops in Java. In English

Conditions in java. In English