AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Reusable Classes.  Motivation: Write less code!
Classes and SubClasses Super Parent Sub Child IS - A.
OOP: Inheritance By: Lamiaa Said.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
UML Class Diagram: class Rectangle
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
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.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
AP Computer Science A – Healdsburg High School 1 Unit 3 - Objects and Classes Lab: First Steps - Example.
Inheritance A Review of Objects, Classes, and Subclasses.
Classes and Objects Including inheritance. OOP — Object-Oriented Programming In OOP system, the class is a fundamental unit. An OOP program models a.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Class Inheritance Part I
Sections Inheritance and Abstract Classes
Inheritance, Abstract Classes, Interface and Polymorphism
Inheritance Notes: Using Object Oriented Design
Objects as a programming concept
Interface, Subclass, and Abstract Class Review
Java Unit 11: Inheritance I
UML Class Diagram: class Rectangle
Inherited Classes in Java
Chapter 14 Abstract Classes and Interfaces
OOP Aga private institute for computer science 5th grade
Unit 3 - Introduction to Inheritance - Example
Presentation transcript:

AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling superclass’ constructors and methods

AP Computer Science A – Healdsburg High School 2 Abstract Class – A class which at least one of the methods is left “abstract”, or without the code implemented. public abstract class Polygon { private int numberOfSides; public Polygon() {/* code not shown */} public abstract double getArea(); /* other methods not shown */ }

AP Computer Science A – Healdsburg High School 3 Abstract Class – A class which at least one of the methods is left “abstract”, or without the code implemented. public abstract class Polygon { private int numberOfSides; public Polygon() {/* code not shown */} public abstract double getArea(); /* other methods not shown */ } public class Rectangle extends Polygon { private double myLength; private double myWidth; public Rectangle() {/* code not shown */} public double getArea() { return myLength * myWidth; } /* other methods not shown */ }

AP Computer Science A – Healdsburg High School 4 Example: Inheritance Diagram for GridWorld’s Grid AbstractGrid BoundedGridUnboundedGrid Abstract Grid Class Subclasses of AbstractGrid

AP Computer Science A – Healdsburg High School 5 Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { void fill(int x); int getCurrentAmount(); int getMaximumCapacity(); }

AP Computer Science A – Healdsburg High School 6 Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { void fill(int x); int getCurrentAmount(); int getMaximumCapacity(); } public class Car implements Fillable {... public void fill(int gallons) {fuelAmount += gallons;} public int getCurrentAmount() {return fuelAmount;} public int getMaximumCapacity() {return fuelTankCapacity;} }

AP Computer Science A – Healdsburg High School 7 Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { void fill(int x); int getCurrentAmount(); int getMaximumCapacity(); } public class Car implements Fillable {... public void fill(int gallons) {fuelAmount += gallons;} public int getCurrentAmount() {return fuelAmount;} public int getMaximumCapacity() {return fuelTankCapacity;} } public class VendingMachine implements Fillable {... public void fill(int quantity) {currentStock += quantity;} public int getCurrentAmount() {return currentStock;} public int getMaximumCapacity() {return 20;} }

AP Computer Science A – Healdsburg High School 8 Example: An Interface used in GridWorld Simulation A class that implements an Interface MUST implement ALL of the methods defined in the Interface. AbstractGrid BoundedGridUnboundedGrid AbstractGrid Class Subclasses of AbstractGrid Grid Grid Interface

AP Computer Science A – Healdsburg High School 9 Example: Our Address Book Program Database Addressbook Interface Database AddressBook implements Database A class that implements an Interface MUST implement ALL of the methods defined in the Interface.

AP Computer Science A – Healdsburg High School 10 Calling Superclass’s Constructors public class Walker extends Biped { // Constructor public Walker(int x, int y, Image leftPic, Image rightPic) { super(x, y, leftPic, rightPic);... } Biped Walker Calls Biped’s constructor If present, must be the first statement The number / types of parameters passed to super must match parameters of one of the superclass’s constructors.

AP Computer Science A – Healdsburg High School 11 Calling Superclass’s Constructors (cont’d) One of the superclass’s constructors is always called, but you don’t have to have an explicit super statement.

AP Computer Science A – Healdsburg High School 12 Calling Superclass’s Constructors (cont’d) Superclass’s constructor calls its superclass’s constructor, and so on, all the way up to Object’s constructor. Biped Walker Object super( ) super(...)

AP Computer Science A – Healdsburg High School 13 Calling Superclass’s Methods public class CharlieChaplin extends Walker {... public void nextStep () { turnFeetIn(); super.nextStep(); turnFeetOut(); }... } Walker CharlieChaplin Calls Walker’s nextStep super.someMethod refers to someMethod in the nearest class, up the inheritance line, where someMethod is defined.