Objects in software is a bundle that contains state and behavior .We use objects in our daily lives Example of objects are- Dog Des...
Objects in software is a bundle that contains state and behavior.We use objects in our daily lives Example of objects are-
- Dog
- Desk
- Car etc.
All objects have to characteristics:
- State –Eg Cow- has(name,color,breed etc)
- Behaviour-Eg Car has behavior like (changing gear, four wheel drive, apply breaks etc.)
Importance of bundling software objects
- Modularity-it’s easy to write and maintain object code.These objects can be passed around the system easily.
- Information hiding-Because users interact with objects, it's easy to hide internal implementation of a software.
- Enables code reuse-it’s easy to reuse objects once created.
- Easy to debug-if a certain object does not work properly, its easy to remove that object.
How to create a Class Object
Creating a class object is just easy. You just need to use new keyword then call the class constructor.Example.
/** * * @author Eric * www.techoverload.net * How to create class object */ public class Employees { //class instance variables public int id; public String name; public String address; public int age; //class setter and getter methods public void setAge(int employeeAge) { age=employeeAge; } public void setName(String employeeName) { name=employeeName; } public void setAddress(String employeeAddress) { address=employeeAddress; } public void setId(int employeeId) { id=employeeId; } public int getId() { id=this.id; return id; } public int getAge() { age=this.age; return age; } public String getName() { name=this.name; return name; } public String getAddress() { address=this.address; return address; } //Static main method public static void main(String args[]) { //creating class object using class constructor Employees em=new Employees(); em.setAge(23); em.setAddress("PO BOX 11 Mombasa"); em.setId(234532345); em.setName("Mellisa"); System.out.println("ID " +em.id); System.out.println("Age " +em.age); System.out.println("Name " +em.name); System.out.println("Address " +em.address); } }