Anatomy of a Class
Creating a Class: 5.1.1
- A class defines an abstract data class
- An
object
is aninstance
of a class- A variable
object reference
- A variable
- A class is a blueprint which creates
objects
Writing classes
- start a class declaration with
public class nameOfClass
- The body of a class is defined in curly braces
- You can create objects of a type using
ClassName objectName = new ClassName()
public class House {
// Define attributes and methods here
}
House myHouse = new House();
House friendsHouse = new House();
- Objects have attributes and behaviors
- Correspond with instance variables and methods
instance variables
hold datamethods
hold behaviors
constructors
initialize the instance variables- a
main method
can run code
Instance variables: 5.1.2
instance variables
hold the data for an object- also known as
attributes
,fields
, orproperties
- also known as
- In general these should be declared
private
- only code in the class can directly access the variable
- They are usually declared right after the class declaration
import java.awt.Color;
public class Person {
private String name;
private String email;
private String phoneNumber;
}
- Once we have declared a class, we can create many instances of it with different attribute values
Object-oriented programming
stressesdata encapsulation
- Data (instance variables) and code acting on it (methods) are wrapped together in a single unit
- Implementation details are hidden
- This protects the data from harm
-
Anything outside can only interact with the object through public methods
- When designing a class, make conscious decisions about what data to make accessible from the outside
- The
private
access modifier protects data from external access
- The
Methods: 5.1.3
Methods
define what an object can do- Most are
public
- Accessible outside the class
- Some are
private
if they’re only meant to be used internally
- Most are
- Methods are defined as
public returnType methodName(type param0)
- Methods can use an object’s instance variables
void
return type means the method returns nothing
public class House {
private Color paintColor = Color.red;
public boolean isRed() {
return paintColor.equals(Color.red);
}
public void printDescription() {
System.out.println("This house is " + paintColor.toString());
}
}
Object-Oriented Design: 5.1.4
- in
Object Oriented Design
programmers decide what classes are needed to solve a problem- Also what data are needed in each class
- What methods each class needs
Summary: 5.1.6
- programmers use code to represent physical objects or nonphysical concepts using classes
- Based on attributes or behaviors of the object/concept
- In robotics, we use Java to model our robot into “subsystems” which each have their own class!
Instance variables
define attributes (data) of objectsmethods
define behaviors and functionsdata encapsulation
is a technique which hides the implementation details from the user- data is private; accessible through public methods which act on the data
- keywords
public
andprivate
affect the access of classes, data, constructors, and methodsprivate
restricts access outside of the class- instance variables are usually private
public
allows access from outside the class- most methods should be public, unless they aren’t meant to be externally accessed
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.