Scanner class Methods in Java

How base class constructor can be accessed?

 In the previous posts,we learnt about Constructors. In this post, we will learn how to call base class constructor from a child class.

The super Keyword

A subclass inherits the accessible data fields and methods from its superclass, but the constructors of super(base) class are not inherited in the derived(child) class.The only way use constructor of base class from subclass is to invoke the base class's constructor.

The super keyword is used to call the constructor of super (base) class.The super keyword refers to the base class and is beneficial when we want to initialize the constructor of super class.

Syntax: 

super([arguments])

We can call super class constructor using super keyword and passing arguments is optional.If you wan to pass argument then you can pass multiple arguments by separating them with comma.

How to call Super Class constructor from Subclass?

Now, we are familiar with super Keyword.But you must know how to use super? 

In  the body of subclass constructor the super call must the firsts statement.It means, while writing you sub class's constructor, you must write first call to super class constructor using super keyword as shown below;

super(arguments);

If you do not specify super class constructor in the subclass constructor then compiler itself specify super(); in default constructor of subclass.A super class constructor is always called before subclass constructor.

Example: 

In this example, we will create three classes. One Super class "Shape" and its subclass "Sphere" and at last "SuperExample" which is Main class and will print values on screen.We will create a program in which we will calculate the volume of sphere by extending Shape class. This shape class can be extend by any other shape too.We can also find area, volume of other shapes like rectangle, triangle etc. 

Write a program to show the use of super keyword to call the constructor of base (super) class from derived (sub) class.

  • Shape class : 
Write a program to show the use of super keyword to call the constructor of base (super) class from derived (sub) class.
Shape.java

 In above code, we have created a "Shape" class,

  1. We declared three variable x,y and z.
  2. Then we specify the constructor of Shape class, which can take three arguments while invoke call. In the body of constructor , we have initialized the class variable with variables passed to this Shape constructor.
  3. We have generated three Getters .The Getter is  just method which returns the value of class variable.We have created three getter for each variable x ,y and z.
  • Sphere class: 
Write a program to show the use of super keyword to call the constructor of base (super) class from derived (sub) class.
Sphere.java

In this class,

  1. We have declared one variable radius.
  2. In defining the Sphere constructor of this class, we are passing four variable.We will initialize the x1 ,y1 and z1 to super class  using super constructor.
  3. In the body of Sphere constructor, the first statement is super(x1,y1,z1).This statement is a call to the constructor of super class.
  4. Whenever we create the object of Sphere class, it will automatically call super constructor an will pass the values to Shape class.
  5. This class has two methods.The getRadius() is a getter to get the value of radius passed by user.And volume() will return the volume of sphere.
  • SuperExample class:
Write a program to show the use of super keyword to call the constructor of base (super) class from derived (sub) class.
SuperExample.java


In this class,

  1. We created the object sphere of Sphere class.We are passing values of x1,y1,z1 and r in the constructor.The values 2,3,4 and 5 are assigned to variables after this statement.
  2. In the next five print statements,we are printing the values by calling the methods using object sphere.
Output: 

output of super example in java

Analyse output by yourself.Happy Coding!





Comments

Post a Comment

If you have any doubt, ask here