Constructor:
Constructor is a special type of method that is used to initialize the object.
Properties of constructor:
Constructor is a special type of method that is used to initialize the object.
Properties of constructor:
- Has the same name that the class belong to.
- Has no return type.
- Called automatically.
- Default constructor (no parameter) , Parametrized constructor
- public class EmployeeC {
- int eid;
- String ename;
- String egender;
- String edept;
- //Parameterized Constructor implementation
- public EmployeeC(int id,String name,String gender,String dept) {
- eid=id;
- ename=name;
- egender=gender;
- edept=dept;
- }
- public void showData() {
- System.out.println("\nEmployee_id: "+eid+"\nEmployee_name: "+ename+"\nEmployee_gender: "+egender+"\nEmployee_department: "+edept);
- }
- }
- // Main class
- public class ConstractorDemo {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- EmployeeC emp1=new EmployeeC(101,"Sujan Hasan","Male","IT");
- emp1.showData();
- EmployeeC emp2=new EmployeeC(100,"Shimul Hossain","Male","Marketing");
- emp2.showData();
- EmployeeC emp3=new EmployeeC(102,"Shakil Sorker","Male","Networking");
- emp3.showData();
- }
- }
0 Comments:
Post a Comment