Syntax:
Ex: Employee security = new Employee();
When you create an object, the source class is copied into memory and it referred by your object name. Therefore, you can make as much as objects using the same class. Computer will make copies of same class over and over again.
Sample Program:
Case: This program needs two classes; ‘Computer’ class which we will use to create an object named ‘myComputer’. ‘ComputerUser’ class which use the object to access ‘Computer’ class.
First create “Computer.java” which is the source of object.
public class Computer
{
public static void ComputerName()
{
System.out.println("Dual Core");
}
}
Then create “ComputerUser.java” which create the object and use it.
public class ComputerUser
{
public static void main(String[] args)
{
Computer myComputer = new Computer();
myComputer.ComputerName();
}
}
This is the output:
Tip: Objects have a state (also known as member variables, fields, data) and certain behavior (also known as operations, methods, procedures, functions).
Tip: A class represents a blueprint that defines the variables and the methods common to all objects of a certain kind.
Tip: Objects communicate by sending a message or a request to another object to do a particular task. It is usually a call to a method of another object.
No comments:
Post a Comment