Recitation 6 Inheritance.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 31 Part 3.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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 Polymorphism Daniel Liang, Introduction to Java Programming.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Object-oriented Programming in Java
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Polymorphism, Abstract Classes & Interfaces
CSC 205 Java Programming II
Object-Oriented Programming: Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Object-Oriented Programming: Polymorphism
Java Programming Language
CSC 113: Computer programming II
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Inheritance and Polymorphism
Methods (toString, equals)
Polymorphism, Abstract Classes & Interfaces
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Chapter 11 Inheritance and Polymorphism
Recitation 7 October 7, 2011.
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

Recitation 6 Inheritance

Terms Superclass: A class from which one ore more classes are derived. Subclass: A class that inherits from a superclass

Terms Overriding: When a subclass has the same method as a superclass Super keyword: Used to access superclass methods from the the subclass. Not needed if the method isn’t overridden in the current class. In that case you can call it directly by its name as if it were declared in the current class.

Object Class Is Extended by Everything!

Program explanation The program is derived from AStringHistory and AStringDatabase, which can be found in the JavaTeacing repository. Instead of storing strings, this program stores points. There are 2 different implementations, APointHistory and APointDatabase Generally, both of the two classes can take 2 integer values as arguments. Then they create a point by the 2 values and store it in their properties. APointDatabase extends APointHistory and has 3 new methods, remove(), member(), and clear(); Class APointHistoryUsingArrayList is another implementation of PointHistory. It uses an arraylist, instead of a pre-defined array, to store points. The good part of arrayList is the size of an arraylist can be changed dynamically according to number of element.

Play Around with the Driver Type cast: because APointDatabase extends APointHistory, can we convert a class, which type is APointDatabase, into APointHistory? Or can we do this inversely? getClass() and getSuperclass(): check the code using getClass() and getSuperclass() (in line 29 to line31) and their outputs. Try to use these two methods with other classes. Can you generalize what are the results of the two methods? Override: notice that there are 2 different toString() methods in APointDatabase, one calls super and the other does not. Test the results of both methods and see how keyword super works. And comment out both methods and see the output.

Play Around with the Driver instanceof operator: line 42 to line 45 in Driver class tests the instanceof operator. The instanceof operator takes two operands and returns a boolean value. Basically it checks whether or not a given object is an instance of a class. Tricky thing: we know that APointDatabase extends APointHistory, so what is the result of database instanceof APointHistory? Comment out line 9 and remove the comment sign of line 11, test the arrayList implementation of APointHistory, get some familiarity of arrayList.

To Submit After you're done experimenting, rewrite the main method to have a few of your own statements that use "instanceof", "getClass()", and an overriden "toString()" method. Take a screenshot of your code and the console output and submit it on Sakai

Resources Arraylist: http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html Instanceof: http://stackoverflow.com/questions/7526817/use- of-instance-of-in-java getClass() and getSuperclass(): http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html