
Classes and Objects
Classes & Objects
A class is the blueprint for an object. An object is a software bundle of related state and behavior. The software object is often used to model the real world objects that we see everyday such as desks, pets, and so on. Before an object is created, the programmer determine the fields and methods that are necessary, and then creates a class. Hence, a class is a blueprint of the objects that may be created from, so a class is not an object but it can be a description of an object. Each object created from a class is called an instance of the class. We have two types of classes, a private and public class. A private class, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class. A public class, the member can be accessed by code inside the class or outside. Now lets see a basic example on how class and object works.
Example
First, we create a class without the main method, then we create another class with the main method in it. The two class should be in the same directory.
Here is the first class:
/*
Basic class & object ask for a name
*
*/
public class SetName
{
private String name;
// Assign newname to
name
public void setName(String newname)
{
name = newname;
}
// Gets the newname and
return it
public String getName()
{
return name;
}
}
Here is the second class which contains the main method:
/*
The program demonstrate the SetName class
*
*/
import java.util.*;
public class SetNameDemo
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
String getname;
// Creates a SetName object
SetName sname = new SetName();
System.out.println("Enter a name: ");
getname = key.nextLine();
//
Pass getname into the class method setName
sname.setName(getname);
/*
Calls the
getName method in SetName class using the object sname
Then outputs
the name entered by the user
*
*/
System.out.println("The name you
enter is " + sname.getName());
}
}
ClickHere to download firstclass.java
ClickHere to download secondclass.java
The above program we created a class without the main, inside the class contains two methods. One method is to set the name and the other method will returns the name. Hence, in other class which contains the main method, we created a object " SetName sname = new SetName()". By creating the object, we can access the SetName class using the object name that we created which is "sname". To access the SetName class, simply we use the object name "sname" followed by a period and the methods we want to access. Now lets see a bit more complicated example.
Here is the first class:
/*
This is a Rectangle class
It sets the length and width of the rectangle
In addition it calculates the area
*
*/
public class Rectangle
{
private double width;
private double length;
// Method that sets the
length
public void setLength(double newlength)
{
length = newlength;
}
// Method that sets the
width
public void setWidth(double newwidth)
{
width = newwidth;
}
// Method that returns
a rectangle object's length
public double getLength()
{
return length;
}
// Method that returns
a rectangle object's width
public double getWidth()
{
return width;
}
// Method that returns
a rectangle object's area
public double getArea()
{
return length * width;
}
}
Here is the second class:
/*
This program contains the main method
It demonstrates the Rectangle class
*
*/
import java.util.*;
public class RectangleDemo
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
// Creates the Rectangle Object
Rectangle rec = new Rectangle();
//
Get the length from the user
System.out.print("Enter the length:
");
double len = key.nextDouble();
// Get the width from the user
System.out.print("Enter the width:
");
double wid = key.nextDouble();
/*
Calls the
setLength and setWidth methods in the Rectangle class
Using the
object name we created, "rec"
Pass the len
and wid into the methods
*
*/
rec.setLength(len);
rec.setWidth(wid);
/*
Outputs the
length and width entered by the user
In addition
to that the area will be output
*
*/
System.out.println("The length is " +
rec.getLength());
System.out.println("The width is " +
rec.getWidth());
System.out.println("The area is " +
rec.getArea());
}
}
ClickHere to download firstclass.java
ClickHere to download secondclass.java
More about Class
Often time two or more methods in a class may
have the same name as long as their parameter lists are different. This also
applies to constructors. We call this Overloading methods and constructors.
Method overloading is an important part of object-oriented programming. When a
method overloaded, it means that multiple methods in the same class have the
same name, but different types of parameters. Method overloading is important
because sometimes we need several different ways to perform the same operation.
Lets see an example on how it works. Recall, from our " Summary " chapter, we
have an Atm program. For this example we will use the same Atm program.
Here is the first class:
/*
This is an atm class, it demonstrates method overloading
*
*/
public class AtmOverload
{
private double balance;
// This constructor sets the
starting balance at 0.0;
public AtmOverload()
{
balance = 0.0;
}
/*
This constructor sets the starting
balance to the value
passed as an argument
*
*/
public AtmOverload(double startBalance)
{
balance = startBalance;
}
// This is the deposit
method
public void deposit(double amount)
{
balance += amount;
}
// This is the withdraw
method
public void withdraw(double amount)
{
balance -= amount;
}
// This is the set
balance method
public void setBalance(double newbalance)
{
balance = newbalance;
}
// This is the get
balance method
public double getBalance()
{
return balance;
}
}
Here is the second class:
/*
This demonstrate overloading of Atm class
*
*/
import java.util.*;
public class AtmOverloadDemo
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
double money;
// Gets the user account starting balance
System.out.println("Enter your
starting balance ");
money = key.nextDouble();
/*
Creates an
Atm object
Pass the
balance into AtmOverload
Then assign
money into balance
*
*/
AtmOverload atm = new
AtmOverload(money);
// Ask the user for deposit amount
System.out.println("How much do you
want to deposit");
double damount = key.nextDouble();
/*
Calls the
deposit method in AtmOverload class
Passes
damount into it
*
*/
atm.deposit(damount);
// Prints out the new amount after deposit
System.out.println("Your new amount
after deposit is " + atm.getBalance());
// Ask the user for withdraw amount
System.out.println("\nHow much do you
want to withdraw");
double wamount = key.nextDouble();
/*
Calls the
withdraw method in AtmOverload class
Passes
wamount into it
*
*/
atm.withdraw(wamount);
// Prints out the new amount after withdraw
System.out.println("Your new balance
is " + atm.getBalance());
}
}
ClickHere to download firstclass.java
ClickHere to download secondclass.java