Recitation 7 October 7, 2011.

Slides:



Advertisements
Similar presentations
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Java Introduction 密碼學與應用 海洋大學資訊工程系 丁培毅.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Class Inheritance Part I
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Stacks.
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Recitation 6 Inheritance.
Inheritance Basics Programming with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 12 Abstract Classes and Interfaces
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Recitation 6 September 30, 2011.
Recitation 9 October 28, 2011.
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Recitation 7 October 7, 2011

As you come in . . . Please sit next to someone with whom you will collaborate for today’s recitation. Choose someone with whom you have NOT collaborated during a previous recitation.

Today’s Goals: Use inheritance and overriding to specialize the behavior of variable-sized collections of graphics objects Call superclass constructors More test-first programming

Properties of Inheritance All objects in Java have exactly one immediate superclass (Object if none specified) Subclasses inherit all constructors, methods, variables, and constants from their parent classes Subclasses can override methods from any superclass by defining a method with the same signature

Extending a class Use the extends keyword to subclass public class AStringDatabase extends AStringHistory implements StringDatabase { public void removeElement (String element) { … } int indexOf (String element) { … } void removeElement (int index) { … } void shiftUp (int startIndex) { … } public boolean member(String element) { … } public void clear() { … } } Use the extends keyword to subclass A subclass may have additional methods and implement extra interfaces

Invoking a super constructor public class AStringDatabase extends AStringHistory implements StringDatabase { boolean printWhenBeyondCapacity; // print when user tries to add to a full database public AStringDatabase(int maxCapacity, boolean printWhenPastCapacity) { super(maxCapacity); printWhenBeyondCapacity = printWhenPastCapacity; } . . . Use the super keyword to call the parent constructor Must be the first line in your constructor

Overriding the toString() method public String toString() { String retVal = “”; for (int i = 0; i < size; i++) retVal += contents[i] + “:”; return retVal; } stringDatabase.toString()  “James Dean:John Smith:”

Recitation Specification Download Recitation7.zip from the Recitations page Implement TriangleStack by extending GeneralObjectStack TriangleStack should have: A constructor that takes no parameters and simply calls the superclass constructor A constructor that takes an integer parameter (indicating the desired maximum capacity) and calls the superclass constructor that takes an int A method called “boolean isTriangle(Object obj)” that returns true if the provided Object is an instance of (i.e. implements the interface) Triangle. Note: this method is not public – it is default. An overridden version of push(Object obj). If the provided object is a Triangle (try calling isTriangle), then the superclass’s version of push is called on obj. Your code works if you can successfully run Recitation7.java

Extra challenge After getting checked off for the previous part, implement: AlignedTriangleStack & CorneredTriangleStack Download Extra.zip from the Recitations page, open it, and CLICK & DRAG the files into the appropriate packages

CorneredTriangleStack Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x and the y of the added triangle are set to be the same as the x and the y of the top triangle on the stack. (Try to reuse as many methods as possible.)

AlignedTriangleStack Create a constructor that takes no parameters and calls the superclass constructor with the constant CAPACITY Whenever a Triangle is pushed onto a non-empty stack, the x is set to be the x of the top triangle, and the y is set to be the y of the bottom of the top triangle. (Try to reuse as many methods as possible.)

Test extra challenge To test each part of the extra challenge, uncomment the appropriate line in main() Test each component individually before trying TransparentStackSwapperTester