Constructors - This - Inherit in Java PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Constructors in Java

In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling constructor, memory for the object is allocated in the
memory.

It is a special type of method which is used to initialize the object.

Every time an object is created using the new() keyword, at least one constructor is called.

It calls a default constructor if there is no constructor available in the class. In such case, Java
compiler provides a default constructor by default.

There are two types of constructors in Java: no-argumentconstructor(default constructor), and


parameterized constructor.

Note: It is called constructor because it constructs the values at the time of object creation. It
is not necessary to write a constructor for a class. It is because java compiler creates a default
constructor if your class doesn't have any.

Rules for creating Java constructor

There are two rules defined for the constructor.

1. Constructor name must be the same as its class name


2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Note: We can use access modifiers while declaring a constructor. It controls the object
creation. In other words, we can have private, protected, public or default constructor in
Java.

Types of Java constructors

There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

Java Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.


Syntax of default constructor:
1. <class_name>(){}

Example of default constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at
the time of object creation.

//Java Program to create and call a default constructor


class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output: Bike is created

Rule: If there is no constructor in a class, compiler automatically creates a default


constructor.

Q) What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null, etc.,
depending on the type.

Example of default constructor that displays the default values


//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}
Output: 0 null
0 null

Explanation: In the above class, you are not creating any constructor so compiler provides
you a default constructor. Here 0 and null values are provided by default constructor.

Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects. However,
you can provide the same values also.

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We
can have any number of parameters in the constructor.

//Java Program to demonstrate the use of the parameterized constructor.


class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output: 111 Karan
222 Aryan

Difference between constructor and method in Java

There are many differences between constructors and methods. They are given below.

Java Constructor Java Method

A constructor is used to initialize the state of A method is used to expose the behaviour of an
an object. object.

A constructor must not have a return type. A method must have a return type.

The constructor is invoked implicitly. The method is invoked explicitly.

The Java compiler provides a default The method is not provided by the compiler in
constructor if you don't have any constructor any case.
in a class.

The constructor name must be same as the The method name may or may not be same as
class name. the class name.
Java Copy Constructor

There is no copy constructor in Java. However, we can copy the values from one object to
another like copy constructor in C++.

There are many ways to copy the values of one object into another in Java. They are:

o By constructor
o By assigning the values of one object into another
o By clone() method of Object class

In this example, we are going to copy the values of one object into another
using Java constructor.

class Student6{
int id;
String name;
//constructor to initialize integer and string
Student6(int i,String n){
id = i;
name = n;
}
//constructor to initialize another object
Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}

public static void main(String args[]){


Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}

Output: 111 Karan

111 Karan

Copy the values of one object into another by assigning the values of one
object into another:
class copy

{ int x;

copy(int a){x=a;}}

public static void main(String a[]){

copy cp=new copy(10);

copy cp1=new copy(20);

cp=cp1;

System.out.println(cp.x);

System.out.println(cp1.x);}}
this keyword in Java
There can be a lot of usage of Java this keyword. In Java, this is a reference variable that
refers to the current object.

1) this: to refer current class instance variable

The this keyword can be used to refer current class instance variable. If there is ambiguity
between the instance variables and formal parameters, this keyword resolves the problem of
ambiguity.

Let's understand the problem if we don't use this keyword by the example given below:

class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis1{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}

Output:

0 null 0.0
0 null 0.0

In the above example, parameters (formal arguments) and instance variables are same. So, we
are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keyword


class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}

class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}

Output:

111 ankit 5000.0


112 sumit 6000.0
If local variables(formal arguments) and instance variables are different, there is no need to
use this keyword like in the following program:

class Student{
int rollno;
String name;
float fee;
Student(int r,String n,float f){
rollno=r;
name=n;
fee=f;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis3{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}

Output:

111 ankit 5000.0


112 sumit 6000.0
2) this: to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the
this keyword, compiler automatically adds this keyword while invoking the method. Let's see
the example.

class A{
void m(){System.out.println("hello m");}
void n(){
System.out.println("hello n");
//m();//same as this.m()
this.m();
}
}
class TestThis4{
public static void main(String args[]){
A a=new A();
a.n();
}}
OUTPUT: hello n
hello m
3) this() : to invoke current class constructor

The this() constructor call can be used to invoke the current class constructor. It is used to reuse
the constructor. In other words, it is used for constructor chaining.

Calling default constructor from parameterized constructor:

class A{
A(){System.out.println("hello a");}
A(int x){
this();
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){
A a=new A(10);
}}

Output:

hello a
10

Calling parameterized constructor from default constructor:

class A{
A(){
this(5);
System.out.println("hello a");
}
A(int x){
System.out.println(x);
}
}
class TestThis6{
public static void main(String args[]){
A a=new A();
}}

Output:

5
hello a
Real usage of this() constructor call

The this() constructor call should be used to reuse the constructor from the constructor. It
maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see
the example given below that displays the actual use of this keyword.
class Student{
int rollno;
String name,course;
float fee;
Student(int rollno,String name,String course){
this.rollno=rollno;
this.name=name;
this.course=course;
}
Student(int rollno,String name,String course,float fee){
this(rollno,name,course);//reusing constructor
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}
}
class TestThis7{
public static void main(String args[]){
Student s1=new Student(111,"ankit","java");
Student s2=new Student(112,"sumit","java",6000f);
s1.display();
s2.display();
}}

Output:

111 ankit java 0.0


112 sumit java 6000.0
Rule: Call to this() must be the first statement in constructor.

4) this: to pass as an argument in the method

The this keyword can also be passed as an argument in the method. It is mainly used in the
event handling. Let's see the example:

class Test
{
int a;
int b;

// Default constructor
Test()
{
a = 10;
b = 20;
}

// Method that receives 'this' keyword as parameter


void display(Test obj)
{
System.out.println("a = " +obj.a + " b = " + obj.b);
}

// Method that returns current class instance


void get()
{
display(this);
}

public static void main(String[] args)


{
Test object = new Test();
object.get();
}

Application of this that can be passed as an argument:

In event handling (or) in a situation where we have to provide reference of a class to another
one. It is used to reuse one object in many methods.

5) this: to pass as argument in the constructor call

We can pass the this keyword in the constructor also. It is useful if we have to use one object
in multiple classes. Let's see the example:

class B{
A obj;// Class with object of Class A as its data member
B(A obj){
this.obj=obj;
}
void display(){
System.out.println(obj.data);//using data member of A4 class
}
}
class A{
int data=10;
A(){
B b=new B(this);
b.display();
}
public static void main(String args[]){
A aa=new A();
}
}
OUTPUT: 10
Proving this keyword

Let's prove that this keyword refers to the current class instance variable. In this program, we
are printing the reference variable and this, output of both variables are same.

class A5{
void m(){
System.out.println(this);//prints same reference ID
}
public static void main(String args[]){
A5 obj=new A5();
System.out.println(obj);//prints the reference ID
obj.m();
}}
A5@22b3ea59
A5@22b3ea59

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviours of a parent object. It is an important part of OOPs (Object Oriented
programming system).

The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse methods
and fields of the parent class. Moreover, you can add new methods and fields in your
current class also.

Inheritance represents the IS-A relationship which is also known as a parent-


child relationship.
Why use inheritance in java
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits
the features. It is also called a base class or a parent class.
o Reusability: As the name specifies, reusability is a mechanism which facilitates
you to reuse the fields and methods of the existing class when you create a new
class. You can use the same fields and methods already defined in the previous
class.

The syntax of Java Inheritance


class Subclass-name extends Superclass-name
{
//methods and fields
}

The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.In the
terminology of Java, a class which is inherited is called a parent or superclass, and the
new class is called child or subclass.

Java Inheritance Example

As displayed in the above figure, Programmer is the subclass and Employee is the
superclass. The relationship between the two classes is Programmer IS-A Employee.
It means that Programmer is a type of Employee.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}}
OUTPUT:
Programmer salary is:40000
Bonus of Programmer is:1000

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java: single, multilevel
and hierarchical. In java programming, multiple and hybrid inheritance is supported
through interface only. We will learn about interfaces later.

Note: Multiple inheritance is not supported in Java through class.


Single Inheritance Example

When a class inherits another class, it is known as a single inheritance. In the example
given below, Dog class inherits the Animal class, so there is the single inheritance.

1. class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}

Output:

barking...
eating...

Multilevel Inheritance Example

When there is a chain of inheritance, it is known as multilevel inheritance. As you can


see in the example given below, BabyDog class inherits the Dog class which again
inherits the Animal class, so there is a multilevel inheritance.

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}

Output:

weeping...
barking...
eating...

Hierarchical Inheritance Example

When two or more classes inherits a single class, it is known as hierarchical inheritance.
In the example given below, Dog and Cat classes inherits the Animal class, so there is
hierarchical inheritance.

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}

Output:

meowing...
eating...

Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation represents
HAS-A relationship.

Why use Aggregation?


o For Code Reusability.

class Operation{
int square(int n){
return n*n;
}
}
class Circle{
Operation op;//aggregation
double pi=3.14;

double area(int radius){


op=new Operation();
int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).

return pi*rsquare;
}
public static void main(String args[]){
Circle c=new Circle();
double result=c.area(5);
System.out.println(result); }}
Polymorphism in Java
Polymorphism is considered one of the important features of Object-Oriented
Programming. Polymorphism allows us to perform a single action in different
ways. In other words, polymorphism allows you to define one interface and
have multiple implementations. The word “poly” means many and “morphs”
means forms, So it means many forms.

Real life example of polymorphism: A person at the same time can have
different characteristic. Like a man at the same time is a father, a husband, an
employee. So the same person possess different behaviour in different
situations. This is called polymorphism.

In Java polymorphism is mainly divided into two types:


• Compile time Polymorphism
• Runtime Polymorphism
1. Compile-time polymorphism: It is also known as static polymorphism.
This type of polymorphism is achieved by Method overloading or operator
overloading. But Java doesn’t support the Operator Overloading.
Method Overloading in Java

If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.

If we have to perform only one operation, having same name of the methods increases
the readability of the program.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

1. By changing number of arguments


2. By changing the data type

In Java, Method Overloading is not possible by changing the return type of the
method only. That means return type of the method doesn’t play any role.

import java.util.*;

class overload
{

Scanner sc=new Scanner(System.in);

void Add()

System.out.println("Eneter a number");

int x=sc.nextInt();

System.out.println("Eneter a number");

int y=sc.nextInt();

System.out.println("Result="+(x+y));

int Add(int x,inty,int z)

{return(x+y+z);}

float Add(float x,float y)

{return(x+y);}

class OverloadDemo

public static void main(String a[])

overload ob1=new overload();

ob1.Add();

//int res=ob1.Add(10,20,30);

//float res1=ob1.Add(3.5f,2.5f);

//System.out.println("Addition of three integers="+res);


//System.out.println("Addition of two floats="+res1);

System.out.println("Addition of three integers="+ob1.Add(10,20,30));

System.out.println("Addition of two floats="+ob1.Add(1.5f,2.5f));}}

Constructor Overloading in Java

In Java, a constructor is just like a method but without return type. It can also be overloaded
like Java methods.

Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a different
task. They are differentiated by the compiler by the number of parameters in the list and their
types.

Example of Constructor Overloading


//Java program to overload constructors
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){


Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Output: 111 Karan 0
222 Aryan 25
Can we overload java main() method?
Yes, by method overloading. You can have any number of main methods in a class by
method overloading. But JVM calls main() method which receives string array as
arguments only. Let's see the simple example:

class TestOverloading{
public static void main(String[] args){System.out.println("HELLO JAVA");}
public static void main(String args){System.out.println("HII");}
public static void main(){System.out.println("HELLO WORLD");}
}

Output: HELLO JAVA

Method Overloading and Type Promotion

One type is promoted to another implicitly if no matching datatype is found. Let's


understand the concept by the figure given below:

As displayed in the above diagram, byte can be promoted to short, int, long, float or
double. The short datatype can be promoted to int, long, float or double. The char
datatype can be promoted to int,long,float or double and so on.

Example of Method Overloading with TypePromotion


class Overloading1{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}

public static void main(String args[]){


Overloading1 obj=new Overloading1();
obj.sum(20,20);//now second int literal will be promoted to long
obj.sum(20,20,20);
}
}
OUTPUT: 40 60
Example of Method Overloading with Type Promotion if
matching found

If there are matching type arguments in the method, type promotion is not performed.

class Overloading2{
void sum(int a,int b){System.out.println("int arg method invoked");}
void sum(long a,long b){System.out.println("long arg method invoked");}

public static void main(String args[]){


Overloading2 obj=new Overloading2();
obj.sum(20,20);//now int arg sum() method gets invoked
} }
OUTPUT: int arg method invoked
Example of Method Overloading with Type Promotion in case of
ambiguity

If there are no matching type arguments in the method, and each method promotes
similar number of arguments, there will be ambiguity.

class Overloading3{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}

public static void main(String args[]){


Overloading3 obj=new Overloading3();
obj.sum(20,20);//now ambiguity
} }
OUTPUT: COMPILE TIME ERROR
One type is not de-promoted implicitly for example double cannot be depromoted
to any type implicitly.
Output:40
Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding


o Method overriding is used to provide the specific implementation of a method which
is already provided by its superclass.
o Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


The method must have the same name as in the parent class

The method must have the same parameter as in the parent class.

There must be an IS-A relationship (inheritance).

//Creating a parent class.


class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}

public static void main(String args[]){


Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}}

Output:
Bike is running safely
A real example of Java Method Overriding

Consider a scenario where Bank is a class that provides functionality to get the rate of
interest. However, the rate of interest varies according to banks. For example, SBI, ICICI
and AXIS banks could provide 8%, 7%, and 9% rate of interest.

Java method overriding is mostly used in Runtime Polymorphism which we will


learn in next pages.
//Creating a parent class.
class Bank{
int getRateOfInterest(){return 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
} }
OUTPUT:SBI Rate of Interest: 8
ICICI Rate of Interest:7
AXIS Rate of Interest: 9

Why can we not override static method?

It is because the static method is bound with class whereas instance method is bound
with an object. Static belongs to the class area, and an instance belongs to the heap
area.

Can we override java main method?

No, because the main is a static method.

Difference between method Overloading and Method Overriding


in java:
There are many differences between method overloading and method overriding in
java. A list of differences between method overloading and method overriding are
given below:

No. Method Overloading Method


Overriding

1) Method overloading is Method overriding is used to provide the specific


used to increase the implementation of the method that is already
readability of the program. provided by its super class.

2) Method overloading is Method overriding occurs in two classes that


performed within class. have IS-A (inheritance) relationship.

3) In case of method In case of method overriding, parameter must be


overloading, parameter must same.
be different.

4) Method overloading is the Method overriding is the example of run time


example of compile time polymorphism.
polymorphism.
5) In java, method overloading Return type must be same or covariant in method
can't be performed by overriding.
changing return type of the
method only. Return type can
be same or different in
method overloading. But you
must have to change the
parameter.

Super Keyword in Java


The super keyword in Java is a reference variable which is used to refer immediate
parent class object.

Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

) super is used to refer immediate parent class


instance variable.
We can use super keyword to access the data member or field of parent class. It is
used if parent class and child class have same fields.

class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}

OUTPUT: black white

2) super can be used to invoke parent class method


The super keyword can also be used to invoke parent class method. It should be used
if subclass contains the same method as parent class. In other words, it is used if
method is overridden.

class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark(); }}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
}}

Output:

eating...
barking...

3) super is used to invoke parent class constructor.


The super keyword can also be used to invoke the parent class constructor. Let's see a
simple example:

class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}

Output:

animal is created
dog is created
Note: super() is added in each class constructor automatically by compiler if there
is no super() or this().

As we know well that default constructor is provided by compiler automatically if there


is no constructor. But, it also adds super() as the first statement.

Another example of super keyword where super() is provided by the compiler


implicitly.

class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
System.out.println("dog is created");
}
}
class TestSuper4{
public static void main(String args[]){
Dog d=new Dog();
}}

Output:

animal is created
dog is created

super example: real use


Let's see the real use of super keyword. Here, Emp class inherits Person class so all the
properties of Person will be inherited to Emp by default. To initialize all the property,
we are using parent class constructor from child class. In such way, we are reusing the
parent class constructor.

class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}}
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary);}
}
class TestSuper5{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
e1.display(); }}

You might also like