Download presentation
Presentation is loading. Please wait.
1
Can perform actions and provide communication
11/12/2018 5:39 PM Chapter 7 Objects A variable of a data type that is a class. Also called an instance of a class. Stores data Can perform actions and provide communication State of object refers to the data it stores Behavior of object refers to the action and communication it provides © 2012 EMC Publishing, LLC
2
Used to create, or instantiate, objects
11/12/2018 5:39 PM Chapter 7 Class An abstract data type Used to create, or instantiate, objects Provides encapsulation, also called information hiding © 2012 EMC Publishing, LLC
3
Chapter 7 The Circle Class
11/12/2018 5:39 PM Chapter 7 The Circle Class © 2012 EMC Publishing, LLC
4
access level class name body variables constructor method
11/12/2018 5:39 PM Chapter 7 A Class access level class name body public class Circle { private static final double PI = 3.14; private double radius; public Circle() { radius = 1; } public void setRadius(double newRadius) { radius = newRadius; } variables constructor Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains six (6) animations. A class declaration includes an access level. <press space bar> The keyword public is called an access modifier. A public class is visible to other classes. Class names should be a noun. <press space bar> A class name should begin with an uppercase letter and then an uppercase letter should also begin each word within the name. The body of the class starts with the first opening brace and ends with the matching closing brace. <press space bar> Within the body of a class are the members of the class. The variables of the class are declared first. <press space bar> The visibility of the member variables is controlled with access modifiers. Declaring a variable as private makes it visible to the class, but not to client code. This encapsulates the data and provides and provides information hiding. Variables define the state of an object. A class has at least one constructor. <press space bar> A constructor is automatically called when an object is created. The constructor is where variables are initialized. A class can have several methods. <press space bar> Methods define the behavior of an object. method © 2012 EMC Publishing, LLC
5
Chapter 7 Methods in a Class
11/12/2018 5:39 PM Chapter 7 Methods in a Class An accessor method is used to determine the value of a variable A modifier method is used to change the value of a variable A helper method is called from within a class by other methods © 2012 EMC Publishing, LLC
6
Chapter 7 Overloading Constructors
11/12/2018 5:39 PM Chapter 7 Overloading Constructors Constructors can be overloaded to provide more options for instantiating an object The compiler uses the number and types of parameters to determine which constructor to execute © 2012 EMC Publishing, LLC
7
Chapter 7 Instance and Class Variables
11/12/2018 5:39 PM Chapter 7 Instance and Class Variables Each object of a class has its own copy of the instance variables in the class. A class variable is declared with the keyword static and only one copy of a class variable is maintained for all objects to refer to. Circle spot1 = new Circle(2); radius PI Circle spot2 = new Circle(5); radius PI 2 3.14 5 © 2012 EMC Publishing, LLC
8
Chapter 7 Instance and Class Methods
11/12/2018 5:39 PM Chapter 7 Instance and Class Methods Instance methods must be called from an instance of the class. Accessor and modifier methods are always instance methods because they change the state of an object. Class methods are declared using the keyword static and can be called from the class itself. © 2012 EMC Publishing, LLC
9
Chapter 7 Differences Between Instance and Class Members
11/12/2018 5:39 PM Chapter 7 Differences Between Instance and Class Members Instance variables are created each time an object is declared. Class variables are created once for the class and then objects of the class refer to this copy. Instance methods can only be called from an object of the class. Class methods can be called from the class itself. © 2012 EMC Publishing, LLC
10
Chapter 7 The Object Class
11/12/2018 5:39 PM Chapter 7 The Object Class Superclass of all other classes. Classes, such as Circle and String, are subclasses: All subclasses inherit the Object methods, which include equals() and toString(). Inherited superclass methods can be redefined, or overridden, in subclasses. © 2012 EMC Publishing, LLC
11
Chapter 7 Classes Using Classes
11/12/2018 5:39 PM Chapter 7 Classes Using Classes A class containing a member variable that is a class data type. Demonstrates a has-a relationship. The class "has-a" class. © 2012 EMC Publishing, LLC
12
Chapter 7 Object-Oriented Development
Objects are selected to model a program specification Objects are created from new classes or from existing classes Objects send messages to other objects to perform a task © 2012 EMC Publishing, LLC
13
Chapter 7 Features of Object-Oriented Programming
11/12/2018 5:39 PM Chapter 7 Features of Object-Oriented Programming Reusability: existing classes can be used over and over again in different applications, which reduces development time and decreases the likelihood of bugs. Modular: Components are separately written and maintained. © 2012 EMC Publishing, LLC
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.