mat 2170
play

Mat 2170 Classes Week 10 Contents Creating Inheritance Classes - PowerPoint PPT Presentation

Mat 2170 Week 10 Classes & Objects Week 10 Mat 2170 Classes Week 10 Contents Creating Inheritance Classes & Objects Methods Lab 10 The GSquare Definition Spring 2014 The GSmartSquare Class Definition Notes Access Review


  1. Mat 2170 Week 10 Classes & Objects Week 10 Mat 2170 Classes Week 10 Contents Creating Inheritance Classes & Objects Methods Lab 10 The GSquare Definition Spring 2014 The GSmartSquare Class Definition Notes Access Review

  2. Student Responsibilities Mat 2170 Week 10 Classes & Objects Week 10 Classes Reading: Textbook, Chapter 6, Chapter 7.1 – 7.3 Contents Creating Inheritance Lab: Inheritance Methods Lab 10 Attendance The GSquare Definition The GSmartSquare Class Definition Notes Access Review

  3. Java Classes Mat 2170 Week 10 Classes & Class : a collection of (usually related) data, along with the Objects methods or operations capable of accessing it Week 10 Classes Contents Data members : the storage components of a class Creating Inheritance Methods Member functions : the messages or methods of a class, Lab 10 used to access and modify the data members The GSquare Definition The We must instantiate (declare) an object of the class before GSmartSquare Class Definition we are allowed to store data in it or send it a message Notes Access Review

  4. Java Objects Mat 2170 Week 10 Classes & Fundamental ( primitive ) Data Types Objects Examples : int , double , boolean Week 10 Objects of a fundamental type have: Classes Contents 1. a state described by a single value. Creating Inheritance Methods Java Classes Lab 10 Examples : String , GRect , GPoint The GSquare Definition Objects of a class have: The GSmartSquare 1. a state: a collection of values or attributes Class Definition 2. messages which the object understands and can act upon Notes Access Review

  5. Defining Our Own Classes Mat 2170 Week 10 The standard form of a class definition in Java: Classes & Objects public class name extends superclass Week 10 { Classes class body Contents } Creating Inheritance Methods The extends clause on the header line specifies the name of Lab 10 the superclass . The GSquare Definition The If the extends clause is missing, the new class becomes a GSmartSquare Class Definition direct subclass of Object , which is the root of Java’s class Notes hierarchy. Access Review

  6. Class Contents Mat 2170 Week 10 Classes & The body of a class consists of a collection of Java Objects definitions that are generically called entries . Week 10 Classes Contents The most common entries are: Creating Inheritance 1. constructors — how to create an instance (an object with Methods initial value/s) of the class Lab 10 The GSquare 2. methods — the methods associated with the class Definition The 3. instance variables — any necessary local objects GSmartSquare Class 4. named constants — any necessary constants for the class Definition Notes Access Review

  7. Creating Classes Mat 2170 Week 10 We can design exactly the structure needed to store related Classes & data Objects Week 10 We can designate exactly how data may be accessed or Classes modified Contents Creating Inheritance We learned how to use classes, now we’ll learn how to Methods modify them, and create our own by: Lab 10 The GSquare adding messages to existing classes Definition The adding attributes / data to existing classes GSmartSquare Class Definition overriding messages in existing classes Notes building a class from “scratch” Access Review

  8. Thinking about clocks . . . Mat 2170 Week 10 Classes & Objects Week 10 Classes Contents A Wall Clock. . . An Alarm Clock. . . Creating Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition Notes A Grandfather Clock. . . A Wristwatch. . . Access Review

  9. Thinking about clocks . . . Mat 2170 Week 10 A Wall clock IS-A kind of clock Classes & Objects ⇒ a clock that hangs on a wall Week 10 An Alarm clock IS-A kind of clock Classes ⇒ a clock with an alarm Contents Creating A Grandfather clock IS-A kind of clock Inheritance ⇒ a clock with a pendulum and chimes Methods Lab 10 A Wristwatch IS-A kind of clock The GSquare ⇒ a clock that straps to your arm Definition The GSmartSquare Class Definition Wall clocks , Alarm clocks , Grandfather clocks , and Notes Wristwatches are specialized clocks Access Review

  10. Inheritance Hierarchy Mat 2170 Week 10 Classes & Objects Week 10 Classes Clock Contents IS−A relationships Creating Inheritance Methods Lab 10 The GSquare Definition Grandfather Wall Clock Alarm Clock Wristwatch The Clock GSmartSquare Class Definition Notes Access Review

  11. Extending Classes Wouldn’t it be nice to be able to create specialized Java Mat 2170 Week 10 objects without starting from scratch? Of course it would! Classes & Objects For example: Week 10 Classes Blinking rectangles Contents Moving circles Creating Inheritance Arbitrary precision numbers Methods Lab 10 The GSquare Definition Inheritance The GSmartSquare Class is the object–oriented programming mechanism for Definition Notes specialization Access . Review

  12. Inheritance Mat 2170 Week 10 Classes & Objects Inheritance is the ability to define a new class by using an Week 10 existing class as a basis. Classes Contents Creating The new class inherits the attributes and behaviors of the Inheritance parent class. Methods Lab 10 The GSquare Definition The The new class IS–A specialized version of the parent class. GSmartSquare Class Definition Notes Access Review

  13. Derived Classes Mat 2170 Week 10 A derived class IS–A new version of its parent class. Classes & Objects Week 10 It has the same members its parent class has. Classes Contents Creating It can add members — both methods and data Inheritance Methods Lab 10 It can re–define members of the parent class, over-riding The GSquare Definition the parent’s definition. The GSmartSquare Class For example, re-defining what it means to move() by giving a Definition new implementation for the function. Notes Access Review

  14. Inheritance Mat 2170 Week 10 A natural way to reuse code Classes & Objects Programming by extension rather than re-invention — Week 10 building on what we already have, rather than starting from Classes scratch. Contents Object-oriented paradigm is well–suited for this style of Creating programming Inheritance Methods Lab 10 The GSquare Definition Terminology The GSmartSquare Parent , base, or superclass Class Definition Notes Derived or subclass Access Review

  15. Class Message Examples Mat 2170 Week 10 Constructors : Create an object — initialize data members Classes & GRect rectA = new GRect(0.0, 0.0, 20.0, 30.0); Objects GRect rectB = new GRect(x, y, width, height); Week 10 Classes Contents Mutators or Setters : Change the state or attributes Creating MyCircle.setColor(Color.RED); Inheritance Methods Lab 10 Inspectors or Getters : Determine something about the state The GSquare Definition double bh = Box.getHeight() The GSmartSquare Class Definition Facilitators : Perform a task Notes MyCircle.move(); Access Review

  16. Derived Class Definition Mat 2170 Week 10 General form : Classes & Objects public class DerivedClassName extends ParentClass Week 10 { Classes // Members go here Contents } Creating Inheritance Methods Example from GSquare Class: Lab 10 The GSquare Definition public class GSquare extends GRect The { GSmartSquare Class Definition // Rest of the definition goes here Notes } Access Review

  17. A Square and More. . . Mat 2170 A GSquare IS-A GRect Week 10 Classes & Objects which Week 10 Classes Is restricted to equal width and height Contents Creating Can be created with a given color Inheritance Methods Lab 10 The GSquare Definition The GSmartSquare Class Definition See Lab Writeup Notes Access Review

  18. The GSmartSquare Class Mat 2170 Week 10 GSmartSquare IS-A A GSquare Classes & Objects which knows: Week 10 Classes Contents How to find its window Creating Inheritance Whether its left or right edge has gone out of the window Methods Lab 10 Whether its top or bottom edge has gone out of the window The GSquare Definition Whether its fits in its window The GSmartSquare See Lab Writeup Class Definition You will be completing this class in lab Notes Access Review

  19. A Square and Still More. . . GMovingSquare IS-A A GSmartSquare which: Mat 2170 Week 10 Classes & Keeps track of a displacement ( x - and y -coordinate) Objects Knows how to move by this displacement Week 10 Classes We can derive this class from GSmartSquare by adding: Contents Creating displacement : a new attribute to keep track of ∆x and Inheritance ∆y for the object Methods Lab 10 move : a message to make a move based on this The GSquare displacement Definition The a constructor that initializes the displacement as well GSmartSquare Class as the other usual attributes of a GSquare object Definition Notes See Lab Writeup Access You will be completing this class in lab Review

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend