Video 42(ch9)

Yash
0

 



Program:-


class MyMainEmployee{
    private int id;
    private String name;

    public MyMainEmployee(){
        id = 4;
        name ="Harry";
    }
    //we can also do the constructor overloading as follows.
    public MyMainEmployee(String Name){
        id = 4;
        name = Name;
    }
    public MyMainEmployee(String n, int m){
        id = m;
        name = n;
    }

    public void setName(String a){
        name = a;
    }
    public String getName(){
        return name;
    }
    public void setId(int b){
        id = b;
    }
    public int getId(){
        return id;
    }
}

public class ConstructorV42 {
    public static void main(String[] args) {
        MyMainEmployee harry = new MyMainEmployee();
        MyMainEmployee harry1 = new MyMainEmployee("Harry");
        MyMainEmployee harry2 = new MyMainEmployee("Harry",4);
        /*
        harry.setName("Harry");
        harry.setId(4);
        System.out.println(harry.getName());
        System.out.println(harry.getId());
        here we use setter and getter but we can also set the id and name by creating a constructor.
        */

        System.out.println(harry.getName());
        System.out.println(harry.getId());

        System.out.println(harry1.getName());
        System.out.println(harry1.getId());

        System.out.println(harry2.getName());
        System.out.println(harry2.getId());
    }
}

Post a Comment

0 Comments
Post a Comment (0)
To Top