X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Overloaded constructor :

Overloaded constructor is one in which constructor name is similar but its signature is different. Signature represents number of parameters, type of parameters and order of parameters (at least one thing must be differentiated).

Example :

Algo a1 = new Algo(10, 20); Algo a2 = new Algo(10, 20, 30); Algo a3 = new Algo(10.5, 20.5); Algo a4 = new Algo(10, 20.5); Algo a5 = new Algo(10.5, 20);

Whenever we define/create the objects with respect to both parameterized constructor and default constructor then it is mandatory for the Java programmer to define both the constructors.


Note :

When we define a class then that class can contain two categories of constructors, they are single default constructor and multiple parameterized constructors (overloaded constructors).

Example :

class Algo { int a, b; Algo() { System.out.println("Algo Default Constructor"); a = 1; b = 2; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } Algo(int c , int d) { System.out.println("Algo Double Parameterized Constructor"); a = c; b = d; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } Algo(int c) { System.out.println("Algo Single Parameterized Constructor"); a = c; b = c; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } Algo(Algo A) { System.out.println("Algo Object Parameterized Constructor"); a = A.a; b = A.b; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } } class Example { public static void main (String [ ] args) { Algo a1 = new Algo(); Algo a2 = new Algo(10 , 20); Algo a3 = new Algo(100); Algo a4 = new Algo(a1); } }

Output :

Algo Default Constructor Value of a = 1 Value of b = 2 Algo Double Parameterized Constructor Value of a = 10 Value of b = 20 Algo Single Parameterized Constructor Value of a = 100 Value of b = 100 Algo Object Parameterized Constructor Value of a = 1 Value of b = 2

Note :

By default the parameter passing mechanism is call by reference. 'this' is an internal or implicit object created by Java for following purpose :

  • 'this' object is internally pointing to current class object. Whenever the formal parameters and data members of the class are similar, to differentiate the data members of the class from formal parameters, the data members of class must be proceeded by 'this'.

this() :

this() is used for calling current class default constructor from current class parameterized constructors.


this(...) :

this(...) is used for calling current class parameterized constructor from other category constructors of the same class.

Example :

class Algo { int a, b; Algo() { this(10); //calling current class single parameterized constructor System.out.println("Algo Default Constructor"); a = 1; b = 2; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } Algo(int c) { this(100, 200); //calling current class double parameterized constructor System.out.println ("Algo Single Parameterized Constructor"); a = b = c; System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } Algo (int a , int b) { System.out.println("Algo Double Parameterized Constructor"); this.a = a; this.b = b; System.out.println("Value of a = " + this.a); System.out.println("Value of b = " + this.b); System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } } class Example { public static void main (String [ ] args) { Algo a = new Algo(); } }

Output :

Algo Double Parameterized Constructor Value of a = 100 Value of b = 200 Value of a = 100 Value of b = 200 Algo Single Parameterized Constructor Value of a = 10 Value of b = 10 Algo Default Constructor Value of a = 1 Value of b = 2

Rule for 'this' :


  • Whenever we use either this() or this(...) in the current class constructors then that statements must be used as first statement only.

  • The order of the output containing this() or this(...) will be in the reverse order of the input which we gave as input.

Note :

  • Whenever we refer the data members which are similar to formal parameters, the JVM gives first preference to formal parameters whereas whenever we write a keyword 'this' before the variable name of a class then the JVM refers to data members of the class.

  • this() is used for calling current class constructors.

  • If any method called by an object then that object is known as source object.

  • If we pass an object as a parameter to the method then that object is known as target object.

Example :

import java.util.Scanner; class Algo { int a , b; Algo() { a = b = 0; } Algo(int a , int b) { this.a = a; this.b = b; } Algo sum(Algo A) { Algo A1 = new Algo(); A1.a = this.a + A.a; A1.b = this.b + A.b; return(A1); } void display() { System.out.println("Value of a = " + a); System.out.println("Value of b = " + b); } } class Example { public static void main(String [ ] args) { Scanner input = new Scanner(System.in); System.out.println("Enter four numbers : "); int n1 = input.nextInt(); int n2 = input.nextInt(); int n3 = input.nextInt(); int n4 = input.nextInt(); Algo a1 = new Algo(n1,n2); Algo a2 = new Algo(n3,n4); Algo a3 = new Algo(); // a3 = a1 + a2; invalid statement a3 = a1.sum(a2); System.out.println("a1 values :"); a1.display (); System.out.println ("a2 values :"); a2.display (); System.out.println ("a3 values :"); a3.display (); } }

Output :

Enter four numbers : 1 2 3 4 a1 values : Value of a = 1 Value of b = 2 a2 values : Value of a = 3 Value of b = 4 a3 values : Value of a = 4 Value of b = 6

super keyword :

super keyword is used for differentiating the base class features with derived class features. super keyword is placing an important role in three places. They are :

  • at variable level




  • at method level




  • at constructor level




super at variable level :

Whenever we inherit the base class members into derived class then there is a possibility that base class members are similar to derived class members.

In order to distinguish the base class members with derived class members in the derived class, the base class members will be preceded by a keyword 'super'.

Syntax for super at variable level : super.base_class_member_name

Example :

import java.util.Scanner; class Base { int a; } class Derived extends Base { int a; void set(int c , int d) { super.a = c; a = d; // by default 'a' is preceded with 'this.' since 'this' represents current class } void sum() { System.out.println("Sum = " + (super.a + a)); } } class Example { public static void main (String [ ] args) { Scanner input = new Scanner(System.in); System.out.println("Enter numbers : "); int n1 = input.nextInt(); int n2 = input.nextInt(); Derived d = new Derived(); d.set(n1 , n2); d.sum(); } }

Output :

Enter numbers : 10 20 Sum = 30

super at method level :

Whenever we inherit the base class methods into the derived class, there is a possibility that base class methods are similar to derived methods.

To differentiate the base class methods with derived class methods in the derived class, the base class methods must be preceded by a keyword super.

Syntax for super at method level : super.base_class_method_name

Example :

class Base { void m1() { System.out.println("Base class m1()"); } } class Derived extends Base { void m1() { super.m1(); //refers to base class m1() method System.out.println("Derived class m1()"); } } class Example { public static void main (String [ ] args) { Derived d = new Derived(); d.m1(); } }

Output :

Base class m1() Derived class m1()

super at constructor level :

Whenever we develop any inheritance application, we use to create object of bottommost derived class. When we create an object of bottommost derived class, it in turns calls its immediate super class default constructor and it in turns calls its constructor. Therefore, in Java the execution starts from top to bottom.


super() :

super() is used for calling super class default constructor from default constructor or from parameterized constructor of derived class.


super(...) :

super(...) is used for calling super class parameterized constructor either from default constructor or from parameterized constructor of derived class. It is always mandatory.


Note :

Whenever we use either super() or super(...) in derived class constructors then they must be used as first statement.

Whenever we want to call default constructor of base class from derived class then using of super() in default constructor of derived class is optional.

Example :

class Base { Base() { System.out.println("Base class"); } } class Intermediate extends Base { Intermediate() { System.out.println("Intermediate class"); } } class Derived extends Intermediate { Derived() { super (); //optional System.out.println("Derived class"); } } class Example { public static void main (String [ ] args) { Derived d = new Derived(); } }

Output :

Base class Intermediate class Derived class

Whenever we want to call the super class parameterized class from parameterized class of the derived class then using of super(...) in parameterized class of derived class is mandatory.

Example :

class Base { int a; Base(int a) { System.out.println("Base class parameterized constructor"); this.a = a; System.out.println("Value of a = " + a); } } class Derived extends Base { int b; Derived(int a , int b) { super(a); // mandatory System.out.println("Derived class parameterized constructor"); this.b = b; System.out.println("Value of b = " + b); } } class Example { public static void main (String k []) { Derived d = new Derived(10 , 20); } }

Output :

Base class parameterized constructor Value of a = 10 Derived class parameterized constructor Value of b = 20

Whenever we want to call default constructor of base class from parameterized class of derived class then using of super() in parameterized class of derived class is optional.

Example :

class Base { int a; Base() { System.out.println("Base class parameterized constructor"); this.a=a; System.out.println("Value of a = " + a); } } class Derived extends Base { int b; Derived(int b) { super(); //optional System.out.println("Derived class parameterized constructor"); this.b = b; System.out.println("Value of b = " + b); } } class Example { public static void main (String [ ] args) { Derived d = new Derived(20); } }

Output :

Base class parameterized constructor Value of a = 0 Derived class parameterized constructor Value of b = 20

Whenever we want to call parameterized class of base class from default constructor of derived class using of super(...) in default constructor of derived class is mandatory.

Example :

class Base { int a; Base(int a) { System.out.println("Base class parameterized constructor"); this.a = a; System.out.println("Value of a = " + a); } } class Derived extends Base { int b; Derived() { super(10); System.out.println("Derived class default constructor"); this.b = 20; System.out.println("Value of b = " + b); } } class Example { public static void main (String [ ] args) { Derived d = new Derived(); } }

Output :

Base class parameterized constructor Value of a = 10 Derived class default constructor Value of b = 20


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.