Created
August 18, 2020 05:12
-
-
Save neer2808/46b3bdc39248715fce9566e0e9d8fc53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class emp | |
{ | |
int empid; // instance variables | |
String name; //instance variables | |
// non parameterised constructor | |
emp() | |
{ | |
empid = 10; | |
name = "abc"; | |
//System.out.println("constructor invoked "); | |
} | |
emp(int ta, String tb) | |
{ | |
empid = ta; | |
name = tb; | |
} | |
emp(int ta) | |
{ | |
empid = ta; | |
name = "aaa"; | |
} | |
public void show() // instance method | |
{ | |
System.out.println(empid + " "+ name); | |
} | |
} // end of class emp | |
public class ConstructorDemo { | |
public static void main(String[] args) { | |
emp obj = new emp(); | |
obj.show(); | |
emp obj2 = new emp(3,"hhh"); | |
obj2.show(); | |
emp obj3 = new emp(4); | |
obj3.show(); | |
// new keyword is responsible to create object | |
// purpose of the constructor to perform | |
// initialization of the object | |
} | |
} | |
// constructor | |
// overloading | |
//1) by providing different no of arguments | |
// emp(int) | |
// emp(int, int ) | |
// emp(int, int , int ) | |
//2) by providing different types of arguments | |
// emp(String) | |
// emp(int) | |
// emp(int , String) | |
// emp(String, int) | |
// | |
// | |
//emp() | |
//{ | |
// | |
//} | |
//emp(int) | |
//{ | |
// | |
//} | |
//emp(String) | |
//{ | |
// | |
//} | |
//emp(int, String ) | |
//{ | |
// | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment