1 of 34

Classes &

Objects

Week 6

1

Helping Hand for Relief & Development

2 of 34

Goal of this lesson

  • In this lesson, we will learn:
    • How to create classes
    • How to use classes
    • What access modifiers are
    • How to write a constructor

2

3 of 34

Key Terms

  • Class:
  • Object:
  • Instance:
  • Constructor:
  • Field:
  • Method (review):
  • Access Modifier:

3

We will learn about these terms in the next slides

4 of 34

What are Classes?

4

5 of 34

What are Classes?

5

6 of 34

What are Classes?

  • Blueprint” or “Template”
  • Group Common Things Together
  • Create your own objects with unique methods and fields
  • Do we know any examples of classes?

6

7 of 34

Object

  • In real-life objects are around us
    • They have characteristics/attributes
    • Examples of attributes: Color, size, weight
  • In Java objects have attributes as well
    • Making a class lets us define them

7

8 of 34

Examples

  • String is a commonly used class
  • Attributes/Characteristics
    • Capitalized first letter: String
    • Uses the dot operator
    • Has built in functions for you

8

9 of 34

Why are Classes important?

  • Java is primarily used as an “Object Oriented Programming” (OOP) Language
  • Classes are more powerful than primitive data types
    • (int, float, boolean, ...) do not come with functions
    • (String, Integer, ...) do (String.charAt, String.length())

9

10 of 34

How do we create one?

  • Let’s do example with coordinate points
  • What is a point?

10

11 of 34

How do we create one?

  • Point has two main features: X Coordinate & Y Coordinate
  • Store with two integers
  • But what does the code look like?

11

12 of 34

How do we create one?

  • File -> New -> Java Class
  • Name it with a capital letter (Point)
  • Add variables for what you want to store (Definition):

12

13 of 34

How do we create one?

  • How do we use it (Creation)?
  • myPoint is an instance of the point class

13

14 of 34

Instance Variables

  • Variables that are stored by the class are called fields or instance variables
  • Unique per instance of the class
    • You can create multiple instances of Point
    • They can all have unique X & Y Coordinate values

14

15 of 34

Constructors

  • What if I had 50 instance variables?
  • Is there an easier way to make and set instances of our classes?

15

16 of 34

Constructors

  • Constructors:
    • “Special function”
    • Used to make creating instances easier
    • “This” keyword refers to a field

16

17 of 34

Constructors

  • Constructors use the same name as the class
  • Use input parameters to set the value of instance variables

17

18 of 34

Constructors

  • Result: much cleaner code when creating them

18

19 of 34

Your Turn! (Exercise 1)

  • Let us create a class together!
  • Create a book class:
    • What are the attributes of a book? Include at least 3
    • Make a constructor!
    • Make at least 2 instances of your class

19

20 of 34

Solution

20

21 of 34

Adding Methods

  • What do classes do right now?
    • Store information
  • Can classes do more than store things?
  • What about modifying/using the information stored?
  • How do we do this?

21

22 of 34

Adding Methods

  • Our classes can have their own functions as well:

22

23 of 34

Getters and Setters

  • Special functions
  • Purpose:
    • Used to get/retrieve variables
    • Used to set/modify variables
    • Exist when private is used

23

24 of 34

Access Modifiers (Brief)

  • Package-private (default) - nothing outside of the package can access
  • Public - all classes present can access the data in a class
  • Private - nothing outside of the class where the variables are defined can access

24

25 of 34

Getters and Setters

25

26 of 34

Exercise 2

  • Go to your book class
    • Make all the variable private
    • Add an age parameter for your book
    • Add a method to increase the age of the book
    • Add Getters and Setters
    • Get the name for one of your instances of books

26

27 of 34

Solution

27

28 of 34

Key Terms

  • Class:
  • Object:
  • Instance:
  • Constructor:
  • Field:
  • Method (review):
  • Access Modifier:

28

Define these terms in your own words, we will go over them in a minute

29 of 34

Key Terms

  • Class: A template used to create multiple objects/instances with similar characteristics
  • Object: Class with attributes in java
  • Instance: One specific version of an object of a class
  • Constructor: A special function used to make instances of classes simpler

29

30 of 34

Key Terms

  • Field: A variable belonging to a class
  • Method (review): A function often belonging to a class
  • Access Modifier: Special keywords that modify what classes can access data from other classes

30

31 of 34

What We Learned

  • Today, we learned:
    • How to create classes
    • How to use classes
    • What access modifiers are
    • How to write a constructor

31

32 of 34

Homework

  • Here is the link to the homework.
  • Please email your java file by Thursday, March 4th to hhrd.compsci@gmail.com

32

33 of 34

Extra Reading

33

34 of 34

Questions?

34

Feel free to ask!