CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
try { Assert(Life. Real); Assert(Life
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 5: HW6 and Interfaces.
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.
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.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
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:
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UML Class Diagram: class Rectangle
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Inheritance using Java
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
What is inheritance? It is the ability to create a new class from an existing class.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Section 6: Interfaces, Midterm Slides by Vinod Rathnam and Geoffrey Liu (with material from Alex Mariakakis, Kellen Donohue, David Mailhot, and Hal Perkins)
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Lecture 5:Interfaces and Abstract Classes
Abstract classes and interfaces
Sixth Lecture ArrayList Abstract Class and Interface
UML Class Diagram: class Rectangle
Section 5: HW6 and Interfaces
Section 5: HW6 and Interfaces
Abstract classes and interfaces
Java Programming Language
Advanced Java Programming
Section 4: Interfaces Parsing Data and
Section 5: HW6 and Interfaces
Chapter 14 Abstract Classes and Interfaces
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
Abstract classes and interfaces
Section 5: HW6 and Interfaces
Presentation transcript:

CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1

Classes, Interfaces, Types The fundamental unit of programming in Java is the class definition – everything is defined in some class But Java also provides interfaces… Classes can extend other classes and implement interfaces… Interfaces can extend other interfaces… Some classes are abstract… And somehow this is all related to types! How does this work? How are these things connected? What is their intended use? More in the fullness of time, but let’s get started… 2

Classes, Objects, and Java Ignoring static cruft for now… Everything is an instance of a class (an object) Every class defines data and methods Every class extends exactly one other class Object if no superclass is explicitly named A class inherits superclass fields and methods Every class also defines a type – i.e., class Foo defines type Foo, and also has all inherited types, e.g., Object Not explored in depth today, but later… So a class is both specification and implementation 3

But… How do we express relationships between classes? Inheritance captures what we want if one class “is-a” specialization of another class Cat extends Mammal { … } But that’s not really right if classes share a behavior or concept but don’t have an “is-a” relationship: E.g., Strings, Sets, and Dates are “Comparable” (we can ask if x is “less than” y) but there are no “is-a” relationships involved And what if we want a class with multiple properties? Can’t extend multiple classes, even if that would do what we want… 4

Java Interfaces Pure type declaration. Example (without generics): public interface Comparable { int compareTo(Object other); } Defines a type ( Comparable here). Can contain: Method specifications (no implementations) Named constants Interface elements are implicitly public Constants are also implicitly final, static Methods are also implicitly abstract (means: specified only, no implementation provided…) Cannot create instances of interfaces – they’re abstract and do not contain implementations of methods e.g., can’t do Comparable c = new Comparable(); 5

Implementing Interfaces A class can implement one or more interfaces: class Gadget implements Comparable{ … } Semantics: The implementing class and its instances have the interface type(s) as well as the class type The class must provide or inherit an implementation of all methods defined in the interface(s) Approximately correct – need to fix for abstract classes (later) 6

Using Interface Types An interface defines a type, so we can declare variables and parameters of that type Key point: A variable with an interface type can refer to an object of any class implementing that type Examples: List x = new ArrayList (); List y = new LinkedList (); Variables x and y both have type List 7

Programming with Interface Types This is not new. You’ve seen this with Java collections: class ArrayList implements List {…} class LinkedList implements List {…} (Generic types omitted for simplicity for now) Client code: void mangle(List victim) { … } Method argument can be anything that has type List (like an ArrayList or LinkedList ) 8

Guidelines for Interfaces Provide interfaces for significant types / abstractions Write code using interface types like Map wherever possible; only use specific classes like HashMap or TreeMap when you need to (creating new objects is the most obvious example) Allows code to work with different implementations later Consider providing classes with complete or partial interface implementation for direct use or subclassing Both interfaces and classes are appropriate in various circumstances 9