INTRODUCTION
There is no special definition needed for "Object" since everything (movable or immovable) in this world is an object.
There are two important things which help to know more about the object. They are:
- State
- Behaviour
State
Denotes the object's characteristics or attributes which give the details about the object.
Behaviour
Denotes the action which can be performed by the object with or without taking the parameters.
There is no rule that all the object can perform some actions. Example Building which can't have any action to perform but it contains the state (attributes) like the size, color, location etc.,
EXAMPLE
Consider the below object Person.
package jbr.oops.object;
public class Person {
private String name = "Ranjith";
private String age = "30";
private String mobile = "989898989898";
public void run() {
System.out.println("I can run....");
}
public void walk() {
System.out.println("I can walk....");
}
public void speak() {
System.out.println("I can speak....");
}
public void view() {
System.out.println("I can see the world....");
}
}
|
Here the name, age, and mobile give the information about the Person which are nothing but the attributes.
And the methods/functions, run(), walk(), speak() and view() are the actions which can be performed by the Person object.
One can create an instance (nothing but the copy of an object) for the object and can retrieve the details of the Person object. Below code snippet will help.
PersonDemo.java
package jbr.oops.object;
public class PersonDemo {
public static void main(String[] args) {
// Create an instance for Person Object
Person person = new Person();
// Get the attribute values.
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Mobile No: " + person.getMobile());
// Test what Person object can do (actions)
person.run();
person.walk();
person.speak();
person.view();
}
}
|
OUTPUT
Name: Ranjith
Age: 30
Mobile No: 989898989898
I can run....
I can walk....
I can speak....
I can see the world....
|
No comments :
Post a Comment