Download presentation
Presentation is loading. Please wait.
Published byNathan Horton Modified over 9 years ago
1
© 2007 Lawrenceville Press Slide 1 Chapter 8 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
2
© 2007 Lawrenceville Press Slide 2 Chapter 8 Class An abstract data type Used to create, or instantiate, objects Provides encapsulation, also called information hiding
3
© 2007 Lawrenceville Press Slide 3 Chapter 8 The Circle Class
4
© 2007 Lawrenceville Press Slide 4 Chapter 8 A Class public class Circle { private static final double PI = 3.14; private double radius; public Circle() { radius = 1; } public void setRadius(double newRadius) { radius = newRadius; } access level class name body variables constructor method
5
© 2007 Lawrenceville Press Slide 5 Chapter 8 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
6
© 2007 Lawrenceville Press Slide 6 Chapter 8 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
7
© 2007 Lawrenceville Press Slide 7 Chapter 8 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);radiusPI Circle spot2 = new Circle(5);radiusPI 23.14 5
8
© 2007 Lawrenceville Press Slide 8 Chapter 8 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.
9
© 2007 Lawrenceville Press Slide 9 Chapter 8 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.
10
© 2007 Lawrenceville Press Slide 10 Chapter 8 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.
11
© 2007 Lawrenceville Press Slide 11 Chapter 8 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.
12
© 2007 Lawrenceville Press Slide 12 Chapter 8 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
13
© 2007 Lawrenceville Press Slide 13 Chapter 8 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.