Knowee
Questions
Features
Study Tools

You are provided with the sample code for the class Employee.Create a constructor for the class Employee, so that, when an object is created for the class Employee, it should print the message as "In Default constructor".public class Employee{           Answer             {                                    System.out.println("In Default constructor");             }      public static void main(String args[])      {               Employee empObj = new Employee();      }}

Question

You are provided with the sample code for the class Employee.Create a constructor for the class Employee, so that, when an object is created for the class Employee, it should print the message as "In Default constructor".public class Employee{           Answer             {                                    System.out.println("In Default constructor");             }      public static void main(String args[])      {               Employee empObj = new Employee();      }}

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is the corrected code in Java:

public class Employee {
    // This is the constructor
    public Employee() {
        System.out.println("In Default constructor");
    }

    public static void main(String args[]) {
        Employee empObj = new Employee();
    }
}

In this code, we have created a constructor for the class Employee. A constructor in Java is a block of code similar to a method that's called when an instance of an object is created. Here, the constructor is Employee().

When we create a new instance of Employee in the main method with new Employee(), the constructor is called and "In Default constructor" is printed to the console.

This problem has been solved

Solution 2

Here is the corrected code in Java:

public class Employee {
    // This is the constructor
    public Employee() {
        System.out.println("In Default constructor");
    }

    public static void main(String args[]) {
        Employee empObj = new Employee();
    }
}

In this code, we have defined a constructor for the class Employee. A constructor in Java is a block of code similar to a method that's called when an instance of an object is created. Here, the constructor is called when we create a new Employee object in the main method. When the Employee object is created, it will print "In Default constructor".

This problem has been solved

Similar Questions

public class Employee {this.id = id;this.name = name;this.salary = salary;}this(id,name,salary); this.pancardno = pancardno; }public Employee(int id, String name, float salary) {this();System.out.println("In default constructor");}public Employee(){}public Employee(int id, String name, float salary, String pancardno) {private int id;private String name;private float salary;private String pancardno;

import java.util.Optional;public class EmployeeDetail { private String alternateEmail; public Optional<String> getAlternateEmail() { return Optional.ofNullable(alternateEmail); } public EmployeeDetail(String alternateEmail) { this.alternateEmail=alternateEmail; }}public class TechSol { public static void main(String[] args) { EmployeeDetail employeeDetailOptional = new EmployeeDetail(new String("[email protected]")); System.out.println(employeeDetailOptional.getAlternateEmail().isPresent()); }}What can be expected when the above code gets compiled and executed? True will be consoled out as a result of executionFalse will be consoled out as a result of executionCompilation will fail because alternateEmail needs to get initialized when declaredExecution will fail because alternateEmail needs to get initialized when declared

Match the constructor Types based on Objects created.When constructor is provided by the developer for the class EmployeeEmployee e = new Employee();Answer 1When constructor not written in the class EmployeeEmployee e = new Employee(101,"Peter" );Answer 2When constructor not written in the class EmployeeEmployee e = new Employee();Answer 3When constructor is provided by the developer for the class EmployeeEmployee e = new Employee(101,"Peter" );

Analyze the following code carefully.public class Test {  int x;  public Test(String t) {     System.out.println("Test");  }  public static void main(String[] args) {    Test test = new Test();    System.out.println(test.x);  }}Group of answer choicesThe program has a compile error because you cannot create an object from the class that defines the object.The program has a compile error because Test does not have a default constructor.The program has a compile error because System.out.println method cannot be invoked from the constructor.The program has a compile error because x has not been initialized.

You have a blank class Student:package com.skillsoft.test;public class Student {}You instantiate this class:Student myStudent = new Student();System.out.println(myStudent.toString());What is the right structure of the output produced by this snippet of code?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.