Constructor Overloading in Java
Constructor Overloading in Java
Constructor Overloading in Java
class Student{
int id;
String name;
int age;
Student(int i,String n){
id = i;
name = n;
}
Student(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}
Output:
1 ISOEH 0
2 ISOAH 20
Java Copy Constructor
There are many ways to copy the values of one object into another
in java. They are:
By constructor
class Student{
int id;
String name;
Student(Student s){
id = s.id;
name =s.name; }
1 ISOEH
1 ISOEH
class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
}
Student(){}
void display(){System.out.println(id+" "+name);}
Output:
1 ISOEH
1 ISOEH
Inheritance in Java
Inheritance in java is a mechanism in which one object acquires all
the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes
that are built upon existing classes. Inheritance represents the IS-A
relationship, also known as parent-child relationship.
The extends keyword indicates that you are making a new class that
derives from an existing class.
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);
}
}
Programmer salary is:40000.0
Bonus of programmer is:10000
In the above example, Programmer object can access the field of own
class as well as of Employee class i.e. code reusability.
class Trainer{
void run(){System.out.println("Trainer is guiding");}
}
class Student extends Trainer {
void run(){System.out.println("Student is learning");}