220 likes | 343 Views
Day 9. Encapsulation. 4.1 Class and Method Definitions. Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth. 4.1 Class and Method Definitions.
E N D
Day 9 Encapsulation
4.1 Class and Method Definitions • Objects can represent objects in the real world, like automobiles, houses, employee records, and so forth
4.1 Class and Method Definitions • Objects can represent objects in the real world, like automobiles, houses, student records, and so forth. • A class is the definition of a kind of object. The class is a general description of what an automobile (or object) can do.
A Class as an Outline Class Name: AutomobileData: amount of fuel ____________speed ___________license plate ____________Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.
An object that satisfies the class definition of an Automobile is said to instantiate the Automobile class. • Thus, objects are individual automobiles, while the Automobile class is a description of what an automobile is and does.
A Class as an Outline Class Name: AutomobileData: amount of fuel ____________speed ___________license plate ____________Methods (actions) increase Speed: How: Press on gas pedal. stop: How: Press on brake pedal.
Instantiations of the Class Automobile: Object Name: SamsCarData: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK” First Instantiation
Instantiations of the Class Automobile: Object Name: SamsCarData: amount of fuel: 10 gallons speed: 55 mphlicense plate: “135 XJK”Object Name: WillsCarData: amount of fuel: 12 gallons speed: 51 mphlicense plate: “153 ZSA” First Instantiation Second Instantiation
In a program that uses the class Automobile, the only actions an Automobile can take are increaseSpeed and stop. • These actions are called methods. All objects of the class Automobile have the same methods.
UML class diagram • There’s no way we are gonna keep writing out large descriptions of Automobile as we just did. There’s quick way of describing the task to the programmer: • UML stands for Universal Modeling Language, or we can simply call it a class diagram.
A Class Outline as a UML Class Diagram Class Name Data Methods (actions)
Instance Variables • The data is referred to as instance variables • We will call the methods methods
Instance Variables The following lines from the start of the class definition define three instance variables: public double fuel;public double speed;public String license;
Now let’s write the main program In a new file with a different class name, which includes the line public static void main (String [] args)we will create an object of Automobile
In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();
In File #2 To create our first object, we write in the body Automobile SamsCar = new Automobile();
Using Methods • When you use a method, you are said to invoke or call it. • You have already invoked methods. For example, your programs have invoked nextInt() with objects of the class Scanner.
Types of Methods • There are two kinds of methods: (1) those that return a single value(2) those that perform some action other than returning a single value
Quotation “There are two kinds of people in the world, those that divide the world into two and those that don’t”
Write out the class and its methods .. Give it a shotwe’ll take it up later