100 likes | 225 Vues
This document outlines essential concepts of inheritance in object-oriented programming using examples. It reviews superclass and subclass relationships, emphasizing their roles in code reuse and design. Key examples include constructing a Rectangle class with specific attributes and methods, exploring constructor overloading, and utilizing the "extends" keyword in Java. The agenda also features upcoming assignments like creating a Fan class, assessing prior exercises, and preparing for quizzes. Students are guided to develop practical skills through UML designs and test programs to enhance their understanding.
E N D
Inheritance – Subclasses and Superclasses Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com May 2, 2013
Agenda • Announcements – CS Seminar this Friday, May 3, 1-2pm, ground floor CCSB • Assess Programming Exercises 8.1, 8.8 • Inheritance • Assignments for next class
Programming Exercise 8.1 – Rectangle class and test program • Design a class named Rectangle. The class should contain: • Two double fields named width and height. The default values are 1 for both. • A no-arg constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A getArea() method that returns the area of this rectangle. • A getPerimeter() method that returns the perimeter. • Draw the UML diagram and implement the class. Write a test program that creates two Rectangle objectts – one with width 4 and height 40 and one with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle.
Inheritance • Allows you to define new classes from existing classes and reuse data fields and methods • Superclass is general class • Subclasses are specializations • Example • Superclass: Geometric Object • Subclasses: Circle and Rectangle
Class Activity • Download files from class website: • GeometricObject.java • Circle2.java • Rectangle2.java • Discuss • “extends” keyword • constructor chaining • Overloading vs. overriding • Test with examples in Dr. Java Interactions pane
Assignments for Next Class • Quiz 7 (Take-home) • Read Sections 11.1-11.5 • Programming Exercises 11.1, 11.2