2) Constructors and Interface
2) Constructors and Interface
It has same name as that of class name. It is used to initialize the objects. It is invoked
implicitly.
Ex:
class student{
student(){ // constructor
System.out.println(“constructor block”);
}
public static void main(String args[]){
System.out.println(“main method block”);
}
}
Syntax: class_name(){}
Syntax: class_name(arg1,arg2,…){}
INTERFACE:
Interface helps to achieve Multiple Inheritance.
Ex:
interface Animal { // this is an interface
public void bark();
}
@Override
public void bark() {
// TODO Auto-generated method stub
System.out.println("This is implemented method of interface
Animal");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("This is implemented method of interface
Mammals");
}
}
Output:
This is implemented method of interface Animal
This is implemented method of interface Mammals