CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Object Oriented Programming
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Generics and The ArrayList Class
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Object-Oriented Design CSC 212. Announcements Ask more questions! If I am not making myself clear, it is your opportunity to explain what is confusing.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
UML Class Diagram: class Rectangle
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Inheritance using Java
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
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,
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Question of the Day  Move one matchstick to produce a square.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Abstract classes and interfaces
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Java – Inheritance.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Final and Abstract Classes
Presentation transcript:

CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types

Polygons All polygons share certain details  Defined by vertices/edges  Have area and circumference But how can this be implemented?  How area computed differs by polygon  Number of vertices or edges Also, cannot have a “polygon”  Only have triangles, squares, dodecagons…

Abstract Keyword Abstract classes is a class  Declared via keyword abstract  Can declare fields & methods  Can also declare abstract methods Abstract methods promise functionality  These methods only contain declaration  Method will need to be defined in subclasses Subclasses that do not define all abstract methods are also abstract

Using Abstract Classes Abstract classes cannot be instantiated  Only contains common data & functionality Can have variables of that type however  Can refer to any subclass instances  Since instances exist, abstract methods define  Thus, can call abstract methods with variables

Using An Abstract Class public abstract class Polygon { protected Vertex[] verts; public abstract double area(); public double circumference() { double retVal = 0; for (int i = 1; i < verts.length; i++) { retVal+=verts[i].distance(verts[i-1]); } retVal+=verts[0].distance(verts[verts.length- 1]); return retVal; } } public class Square extends Polygon { /* Constructor skipped due to lack of space */ public double area() { return Math.pow(verts[1].distance(verts[0]), 2); } }

Using An Abstract Class (2) Polygon[] polys = new Polygon[5]; polys[0] = new Square(...); polys[1] = new Enneagon(...); polys[2] = new Tetrakaidecagon(...); polys[3] = new Chiliagon(...); polys[4] = new Myriagon(...); double totalArea=0.0; double totalCircum=0.0; for (int i=0; i<polys.length; i++) { totalCircum += polys[i].circumference(); totalArea += polys[i].area(); }

Abstract Class Summary Specify common data & methods  Can contain fields, methods, & abstract methods  Any class with abstract method(s) is abstract  Cannot instantiate class if declared abstract Subclasses can be instantiated  But must define abstract methods

Goal of Interfaces We want Polygons to be drawable  But other classes should be drawable, too Could define abstract class Drawable  But cannot extend Polygon & Drawable Interfaces guarantee functionality  Can cut across class definitions  Provides second form of inheritance

Interfaces Declare public abstract methods  Cannot define any other methods Declare necessary constants  Fields must be public static final Classes implement interfaces  Can implement more than 1 interface  Must define all methods from interface

Declaring Interfaces public interface Drawable { public void setColor(Color c); public void setPosition(double x,double y); public void draw(Graphics g); } public interface tDrawable extends Drawable { public void setTransparency(int tLevel); }

Using Interfaces public class DrawableSquare extends Square implements Drawable{ private Color c; private double x; private double y; /* Constructor skipped due to lack of space */ public void setColor(Color col) { c = col; } public void setPosition (double x_pos, double y_pos) { x=x_pos; y=y_pos; } public void draw(Graphics g) { g.drawRect(x, y, side, side, c); } }

Using Interfaces Cannot instantiate an interface… …but can have variables of interface type  Refer to instances of classes implementing interface  Classes required to implement interface’s methods public void drawRed(Drawable d, Graphics g) { d.setColor(red); d.draw(g); }

Last Word on Interfaces Interfaces have sub-interfaces  Sub-interface extends super-interface Inherits methods Adds to methods that must be implemented Classes implementing sub-interface implement super-interface  Must implement all abstract methods

Typecasting Polygon poly = new Square(); Square s = poly; Square s = ((Square)poly); Directs Java to compile illegal code Errors now occur at runtime  This code will compile, but cannot actually run int i = 9; Square s = ((Square)i);

Narrowing Conversions Object obj = new String(“bye”); String t = obj; // NO! NO! NO! Java cannot compile narrowing coversions  Assign superclass to subclass variable  Could be illegal; compiler will not allow it  These assignments must be typecast

Handling Variety of Objects Suppose field could refer to any object  Note: This is true for the rest of the class Define thousands of fields and use one  Good luck writing that one! Could make field be of type Object  Every class is a subclass of Object  Would require lots of typecasting  Talk to prior CSC212 students about this

Generic Types Recent release of Java added generics Class includes/uses a type parameter Actual type filled in at instantiation  Code runs as if were written using that type  Different instances can use different types  Actual type becomes part of variable type

Class With Generics public class ONode { private Object data; public ONode(Object d) { data = d; } public Object getData() { return data; } public void setData(Object dat) { data = dat; } } public class Node { private T data; public Node(T d) { data = d; } public T getData() { return data; } public void setData(T dat) { data = dat; } }

Using Generics Integer i; Scanner s;... ONode n = new ONode(5); i = ((Integer)n.getData()); s = ((Scanner)n.getData()); // Compiles fine, error when run n.setData(s); i = ((Integer)n.getData()); // Compiles fine, error when run s = ((Scanner)n.getData()); Integer i; Scanner s;... Node n = new Node (5); i = n.getData(); s = n.getData(); // Will not compile n.setData(s); // Also will not compile Node m = new Node (s); i = m.getData(); // Now this does not compile s = m.getData();

Before Next Lecture… Complete week #4 assignment Continue programming assignment #1  It is due 1 week from today Start reviewing for Midterm #1  It will be 1 week from Monday Monday’s lecture completes this lecture and talks about arrays  Read through Section 3.1 of the book