Asked by
Jollimay Dayanan
on Oct 27, 2024Verified
Which of the following class definitions is correct in Java?
(i)
Public class Employee
{
Private String name;
Private double salary;
Private int id; public Employee()
{
Name = "";
Salary = 0.0;
Id = 0;
} public Employee(String n, double s, int i)
{
Name = n;
Salary = s;
Id = i;
} public void print()
{
System.out.println(name + " " + id + " " + salary) ;
}
}
(ii)
Public class Employee
{
Private String name;
Private double salary;
Private int id; public void Employee()
{
Name = "";
Salary = 0.0;
Id = 0;
} public void Employee(String n, double s, int i)
{
Name = n;
Salary = s;
Id = i;
}
Public void print()
{
System.out.println(name + " " + id + " " + salary) ;
}
}
A) Only (i)
B) Only (ii)
C) Both (i) and (ii)
D) Neither is correct
Constructors
Special methods in object-oriented programming that are called to initialize new objects with specific initial values or states.
Employee
Generally refers to an individual who works part-time or full-time under a contract of employment, typically earning a salary or wage in exchange for their labor.
- Absorb the basics of object-oriented programming such as classes, methods (public and private), constructors, and objects.
- Learn to recognize and employ access modifiers, specifically private and public, when defining classes.
Verified Answer
BC
Learning Objectives
- Absorb the basics of object-oriented programming such as classes, methods (public and private), constructors, and objects.
- Learn to recognize and employ access modifiers, specifically private and public, when defining classes.