Classes and Objects in
Java
By the end of this video you will be able to…
▪Motivate the use of classes and objects in programming
▪Write classes in Java
▪Create objects and call methods on them
▪Describe what member variables, methods and
constructors are
Computer Science is…
The science of using and processing
large amounts of information
to automate useful tasks
and learn about the world around us
(using a computer)
Map
Shape
Location
Color
Window
… and plenty more objects
A class is a type of data
An object is one such piece of data*
* With associated functionality
Concept of location
Latitude: 32.9
Longitude: -117.2
Defining a Class
public class SimpleLocation
{ Must be in file
public double latitude; [Link]
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
public double latitude; Member variables:
public double longitude; data the objects need to store
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
Methods:
public double latitude;
The things this class can do
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
Constructor:
public double latitude;
Method to create a new object
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{ In file
public double latitude; [Link]
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other)
{
// Body not shown
}
}
Creating and using objects
public class LocationTester In file
{ [Link]
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);
[Link]([Link](lima));
}
}
public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);
[Link]([Link](lima));
}
}
public class SimpleLocation
{
public double latitude;
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);
[Link]([Link](lima));
}
}
public class SimpleLocation
{
public double latitude;
public double longitude;
public SimpleLocation(double lat, double lon)
{
[Link] = lat;
[Link] = lon;
}
public double distance(SimpleLocation other) {
...
[Link](lima)
public double distance(SimpleLocation other)
{
return getDist([Link], [Link],
[Link], [Link]);
}
"this" is the calling object
public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);
[Link]([Link](lima));
}
}
% javac *.java
% java LocationTester
6567.659