Student Responsibilities Mat 2170 Week 10 ◮ Reading: Textbook, Chapter 6, Chapter 7.1 – 7.3 Classes & Objects ◮ Lab: Inheritance ◮ Attendance Spring 2014 1 2 Java Classes Java Objects ◮ Class : a collection of (usually related) data, along with the ◮ Fundamental ( primitive ) Data Types methods or operations capable of accessing it ◮ Examples : int , double , boolean ◮ Objects of a fundamental type have: ◮ Data members : the storage components of a class 1. a state described by a single value. ◮ Java Classes ◮ Member functions : the messages or methods of a class, used to access and modify the data members ◮ Examples : String , GRect , GPoint ◮ Objects of a class have: ◮ We must instantiate (declare) an object of the class before we 1. a state: a collection of values or attributes are allowed to store data in it or send it a message 2. messages which the object understands and can act upon 3 4 Defining Our Own Classes Class Contents ◮ The standard form of a class definition in Java: ◮ The body of a class consists of a collection of Java definitions that are generically called entries . public class name extends superclass { class body } ◮ The most common entries are: 1. constructors — how to create an instance (an object with initial value/s) of the class ◮ The extends clause on the header line specifies the name of the superclass . 2. methods — the methods associated with the class 3. instance variables — any necessary local objects ◮ If the extends clause is missing, the new class becomes a direct 4. named constants — any necessary constants for the class subclass of Object , which is the root of Java’s class hierarchy. 5 6
Creating Classes Thinking about clocks . . . ◮ We can design exactly the structure needed to store related data ◮ We can designate exactly how data may be accessed or modified A Wall Clock. . . An Alarm Clock. . . ◮ We learned how to use classes, now we’ll learn how to modify them, and create our own by: ◮ adding messages to existing classes ◮ adding attributes / data to existing classes ◮ overriding messages in existing classes ◮ building a class from “scratch” A Grandfather Clock. . . A Wristwatch. . . 7 8 Thinking about clocks . . . Inheritance Hierarchy A Wall clock IS-A kind of clock ⇒ a clock that hangs on a wall An Alarm clock IS-A kind of clock Clock ⇒ a clock with an alarm IS−A relationships A Grandfather clock IS-A kind of clock ⇒ a clock with a pendulum and chimes A Wristwatch IS-A kind of clock ⇒ a clock that straps to your arm Grandfather Wall Clock Alarm Clock Wristwatch Clock Wall clocks , Alarm clocks , Grandfather clocks , and Wristwatches are specialized clocks 9 10 Extending Classes Inheritance Wouldn’t it be nice to be able to create specialized Java objects without starting from scratch? Of course it would! ◮ Inheritance is the ability to define a new class by using an For example: existing class as a basis. ◮ Blinking rectangles ◮ Moving circles ◮ The new class inherits the attributes and behaviors of the ◮ Arbitrary precision numbers parent class. Inheritance ◮ The new class IS–A specialized version of the parent class. is the object–oriented programming mechanism for specialization . 11 12
Derived Classes Inheritance ◮ A derived class IS–A new version of its parent class. ◮ A natural way to reuse code ◮ Programming by extension rather than re-invention — building on what we already have, rather than starting from scratch. ◮ It has the same members its parent class has. ◮ Object-oriented paradigm is well–suited for this style of programming ◮ It can add members — both methods and data ◮ It can re–define members of the parent class, over-riding the ◮ Terminology parent’s definition. ◮ Parent , base, or superclass ◮ For example, re-defining what it means to move() by giving a new implementation for the function. ◮ Derived or subclass 13 14 Class Message Examples Derived Class Definition ◮ Constructors : Create an object — initialize data members General form : GRect rectA = new GRect(0.0, 0.0, 20.0, 30.0); public class DerivedClassName extends ParentClass GRect rectB = new GRect(x, y, width, height); { // Members go here ◮ Mutators or Setters : Change the state or attributes } MyCircle.setColor(Color.RED); Example from GSquare Class: ◮ Inspectors or Getters : Determine something about the state double bh = Box.getHeight() public class GSquare extends GRect { ◮ Facilitators : Perform a task // Rest of the definition goes here } MyCircle.move(); 15 16 A Square and More. . . The GSmartSquare Class A GSquare IS-A GRect GSmartSquare IS-A A GSquare which which knows: ◮ Is restricted to equal width and height ◮ How to find its window ◮ Can be created with a given color ◮ Whether its left or right edge has gone out of the window ◮ Whether its top or bottom edge has gone out of the window ◮ Whether its fits in its window See Lab Writeup See Lab Writeup You will be completing this class in lab 17 18
A Square and Still More. . . Technical Notes I GMovingSquare IS-A A GSmartSquare which: ◮ Keeps track of a displacement ( x - and y -coordinate) ◮ When we need to refer to a parent class constructor or another ◮ Knows how to move by this displacement of its methods from within a derived class method, we preface the call with the keyword: super . We can derive this class from GSmartSquare by adding: Example ( GSquare ): super(x, y, size, size); ◮ displacement : a new attribute to keep track of ∆x and ∆y for the object ◮ When we need to distinguish a derived class method from a ◮ move : a message to make a move based on this parent class method or a parameter, we preface the call with displacement the keyword: this . ◮ a constructor that initializes the displacement as well as Example ( GSquare ): this(x, y, size, Color.BLACK); the other usual attributes of a GSquare object See Lab Writeup You will be completing this class in lab 19 20 Technical Notes II Notes on Lab 10 ◮ The graphics window is able to clear objects from itself with the ◮ When no receiver is specified the message is sent to the current remove(obj) message. It can erase everything with the removeAll() message. object. In other words, message() is the same as this.message() within the class. ◮ You must be clear in your mind between methods in the circle Example ( GSquare ): setColor(color); classes and those in the project (program) class, and how they Is equivalent to: this.setColor(color); differ. ◮ We can also use the keyword this to call a constructor from ◮ This lab is worth 50 points, and is not due for two weeks (rather another constructor within the same class. than the usual one) from Thursday. Take this as a hint to begin working on it early. There will be another new lab next week. 21 22 Provided for you: Moving the particles Controlling Access to Entries // Move all particles in the window // For every object in our graphics window... for (int j = 0; j < getElementCount(); j++) ◮ Each entry in a Java class is marked with a keyword to control { which classes have access to that entry. // Get the jth object GObject gObject = getElement(j); ◮ The types of access are termed public , private , and protected . // Make sure it is a Particle if (gObject instanceof GParticle) { ◮ The text uses only public and private . All entries are marked // Since we have a particle, as private unless there is a compelling reason to export them. // cast it as such and move it. ((GParticle) gObject).move(); } } 23 24
Designer versus Client View of Class Members Access Privileges public All classes in the program have access; public entries in a class are said to be exported by that Clients can access these class. Access is limited to the class itself, making that Public members private entry completely invisible outside the class. Access is restricted to the class that defines these protected entities, along with any of its subclasses or any Protected members classes in the same package. & Private members ( no keyword ) The entry is visible only to classes in the same Clients cannot access these! package, and are called package–private . 25 26 The Structure of Memory Binary notation — a Byte ◮ bit : a fundamental unit of information found in one of two possible states: off and on — or false / true — or 0 / 1. ◮ byte : 8 bits 0 0 1 0 1 1 0 0 ◮ nybble : 4 bits or half a byte 1 = 0 ◮ word : 4 bytes = 2 2 4 = 0 2 10 8 = 8 kilo (K) = = 1,024 16 = 0 2 20 mega (M) = = 1,048,576 32 = 32 64 = 0 2 30 giga (G) = = 1,037,741,824 128 = 0 42 ◮ A 64KB computer from the early 1970’s would have had 64 × 1024 or 65,536 bytes of memory. ◮ A 512MB machine would have 512 × 1,048,576 or 536,870,912 bytes of memory. 27 28 Hexadecimal — Base 16 — 4 Bits Each Memory and Addresses Within the memory system of a typical computer, every byte is identified by a numeric address , starting at 0 . 0000 Hex digit Value 0 0 1 0 1 0 1 0 0001 0002 0003 A 10 A 2 B 11 1000 1001 C 12 1002 2 A D 13 10 x 1 = 10 FFFD E 14 FFFE 2 x 16 = 32 FFFF 42 F 15 If we use two bytes to hold addresses, the largest address is: FFFF 16 or 15 × 16 3 + 15 × 16 2 + 15 × 16 1 + 15 × 16 0 or 65,535. It takes 4 bytes, or a word, to be able to address a gigabyte of memory. 29 30
Recommend
More recommend