Download presentation
Presentation is loading. Please wait.
1
A first Look at Classes
2
There are two types of programming used today:
Procedural – This is what we have been doing Object Oriented – uses objects that are made up of methods and procedures
3
Encapsulation refers to the combining of data and code into a single object. Data hiding refers to an object’s ability to hide its data from code that is outside the object.
4
OOP also helps with object reusability
OOP also helps with object reusability. An object is not a stand-alone program, but is used by programs that need its service.
5
Example Think of your alarm clock as an object. It has the following fields: The current second (a value in the range of 0-59) The current minute ( a value in the range of 0-59) The current hour (a value in the range of 1-12) The time the alarm is set for (a valid hour and minute Whether the alarm is on or off (“on” or “off”)
6
Fields are data values that define the state that the alarm clock is currently in. You (the user) cannot directly manipulate these fields because they are private. To change a value, you must use one of the object’s methods, which are: Set time Set alarm time Turn alarm on Turn alarm off
7
You can change the data by using these methods
You can change the data by using these methods. Since you can activate these methods and you are outside of the alarm clock, these methods are public. The alarm clock also has private methods…ones that are internal that you do not have access to. Such methods are: Increment the current second Increment the current minute Increment the current hour Sound alarm
8
Classes can be more than a container for a program
Classes can be more than a container for a program. A class can specify the fields and methods that a particular type of object may have. A class is not an object, but it can be a description of an object. Each object that is created from a class is called an instance of the class.
9
Creating a class When writing the code for a class, it is often helpful to draw a UML diagram (Unified Modeling Language). The diagram is a box that is divided into 3 sections (class name, fields, and methods).
10
Example Rectangle length width setLength() setWidth() getLength()
getWidth() getArea()
11
Writing the code public class Rectangle { } The key word public is an access specifier. Public means that code that is written outside of the Rectangle.java file may access this code.
12
public class Rectangle { private double length; private double width; } Here, the access specifier private means that code outside of this program cannot access the variables length and width.
13
It is common practice in OOP to make all of a class’s fields private and to provide access to those fields through methods.
14
public class Rectangle { private double length; private double width; /*The setLength method stores a value in the length field */ public void setLength(double len) length = len; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.