Object Oriented Programming.docx

No arg constructor constructor with no arguments is

This preview shows page 4 - 7 out of 11 pages.

no-arg constructor: Constructor with no arguments is known as no-arg constructor . The signature is same as default constructor, however body can have any code unlike default constructor where the body of the constructor is empty. Although you may see some people claim that that default and no-arg constructor is same but in fact they are not, even if you write public Demo() { } in your class Demo it cannot be called default constructor since you have written the code of it. Example: no-arg constructor class Demo { public Demo () { System . out .println( "This is a no argument constructor" ); } public static void main( String args[]) { new Demo (); } }
Image of page 4

Subscribe to view the full document.

Output: This is a no argument constructor Parameterized constructor Constructor with arguments (or you can say parameters) is known as Parameterized constructor. Example: parameterized constructor In this example we have a parameterized constructor with two parameters id and name . While creating the objects obj1 and obj2 I have passed two arguments so that this constructor gets invoked after creation of obj1 and obj2. public class Employee { int empId; String empName; //parameterized constructor with two parameters Employee ( int id, String name){ this .empId = id; this .empName = name; } void info(){ System . out .println( "Id: " +empId+ " Name: " +empName); } public static void main( String args[]){ Employee obj1 = new Employee ( 10245 , "Chaitanya" ); Employee obj2 = new Employee ( 92232 , "Negan" ); obj1.info(); obj2.info(); } } Output: Id : 10245 Name : Chaitanya Id : 92232 Name : Negan Example2: parameterized constructor In this example, we have two constructors, a default constructor and a parameterized constructor. When we do not pass any parameter while creating the object using new keyword then default constructor is invoked, however when you pass a parameter then parameterized constructor that matches with the passed parameters list gets invoked.
Image of page 5
class Example2 { private int var ; //default constructor public Example2 () { this . var = 10 ; } //parameterized constructor public Example2 ( int num) { this . var = num; } public int getValue() { return var ; } public static void main( String args[]) { Example2 obj = new Example2 (); Example2 obj2 = new Example2 ( 100 ); System . out .println( "var is: " +obj.getValue()); System . out .println( "var is: " +obj2.getValue()); } } Output: var is : 10 var is : 100 What if you implement only parameterized constructor in class class Example3 { private int var ; public Example3 ( int num) { var =num; } public int getValue() { return var ; } public static void main( String args[]) { Example3 myobj = new Example3 (); System . out .println(
Image of page 6

Subscribe to view the full document.

Image of page 7

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern