A class is a prototype, a blue print, or a template for creating different objects. Objects that has properties and behaviors. A class contains different methods to perform a specific behavior, or perform a code logic. Remember that method names are case sensitive.
I will discuss in the quickest possible way to learn!
To create a class in NetBeans:
- create a new project
- name it as you want
- right click your package in the projects tab
name the class as Employee
click Finish |
Please code this:
Class constructors has the same name as the Class name. Constructors never return a value that is why it has no return type nor a return statement. Constructors can have parameters or not. When a constructor is not created in a class, Java creates a default constructor that has no parameters/arguments and performs no actions/no codes.
Constructors are used when initializing the Object in your main program. E.g. Employee emp = new Employee(arguments here....if there are);
Please code the following in your Employee class:
public class Employee {
String FirstName;
String MiddleName;
String LastName;
int age;
String gender;
String emailaddress;
String contactno;
//This is a clas constructor
Employee(String fName,String mName,String lName){
FirstName=fName;
MiddleName=mName;
LastName=lName;
}
public String getFullname(){
return FirstName+" "+MiddleName+" "+LastName;
}
public String getEmailAddr(){
return emailaddress;
}
public void setAge(int Age){
age=Age;
}
public void setContactNo(String ContactNo){
contactno=ContactNo;
}
//Try to add other methods.....
}
To initialize your class in your main program:
Here is the complete project:
Comments and Questions are well accepted! Thanks!
Next: Error Handling
No comments:
Post a Comment