Download presentation
Presentation is loading. Please wait.
Published byCatherine Booker Modified over 8 years ago
1
Defining Classes
2
Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class
3
Why did they design it that way? Java has some awkward compositions: To read from the keyboard we need: System.in object InputStreamReader object A BufferedReader object A String object To read from a disk file we need: A FileReader object A BufferedReader object A String object Complicated! But powerful
4
Solving the problem We can create our own classes that provide the required behaviour I want to describe a class that Java doesn’t contain It will do something I need
5
Woof I want to keep track of dogs
6
Steps to create your own class 1.Decide on the behaviour that the class will provide 2.Determine the way the class will be used by others (its interface) Determine the prototypes of the methods 3.Write a sample program using the class Test the design 4.Write a skeleton of the class definition Class boilerplate with prototypes and empty method bodies
7
1. Determining the behavior Actions What can it do? If we had a class called Dog we would want it to perform these actions:
8
Characteristics / Attributes Think about attributes or as they are also called instance variables Important information about the object E.g. a chair: 4 legs?, colour?, material? Instance variables are available to ALL methods in the class!
9
What attributes do we want our dog to know?
10
Attributes of the dog
11
2. Interface and Prototypes Interface: way in which one can use an object of a particular class With Java we need the ability to: a.Declare a Dog as follows: Dogfluffie;
12
2. Interface and Prototypes…2 b.Instantiate (create) a new Dog object: fluffie = new Dog( >); Does this object need any arguments (or parameters) to set it up?
13
c.Gather additional information about itself : d.Change its attributes: 2. Interface and Prototypes…3
14
2. Interface and Prototypes…4 d.Send the dog a message to do these things:
15
2. Interface and Prototypes…5 Develop prototypes for the methods Make sure method names follow java conventions
16
2. Interface and Prototypes…6 constructor method: Defines the “default” look or state of an object E.g. a student object must have a first name, last name, student number (and other items)
17
2. Interface and Prototypes…7 Constructor (continued) public Dog( >) constructor has same name as class In our example it may need arguments Age? Name? Fur colour?
18
2. Interface and Prototypes…8 Returning information Send information to the dog Get it to bark Get it to roll over
19
2. Interface and Prototypes…9 Getting information Ask the object (Dog) to give us this information What’s your name dog?
20
2. Interface and Prototypes…10 Change information Ask the dog to change some information Change your dog’s name
21
3. Sample program using class Program serves two purposes Help clarify how our new class is to be used Checks our interface design to see if it is satisfactory Will it do what we need it to do?
22
4. Class definition skeleton class Dog { //instance variables, if needed … // constructor no return type public Dog( >) { statements }
24
Implementing the Triangle Class
25
Implementing the Triangle class Implementation: writing the method bodies and declaring any instance variables Start with any method Start with the easy methods!
26
For your class… Determine it’s behaviour Determine it’s attributes Design 3 ‘get’ methods Design 3 ‘set’ methods Make sure you include any required arguments (stuff to make the method work e.g. Color c)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.