Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()

Slides:



Advertisements
Similar presentations
Java Programming Abstract classes and Interfaces.
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
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,
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Java Interfaces Overview Java Interfaces: A Definition.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Abstract Classes and Interfaces
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
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,
Polymorphism & Interfaces
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
1 Interfaces Sec contains this material. Corresponding lectures on ProgramLive CD is an alternative way to learn the material. Top finalists from.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Interfaces and Inner Classes
Interfaces and Polymorphism CS 162 (Summer 2009).
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
Web Design & Development Lecture 9
Review Session CS2110 Prelim #1.
Inheritance and Polymorphism
Interface.
Interfaces Professor Evan Korth.
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 Spring 2018 Lecture 7: Interfaces and Abstract Classes
More inheritance, Abstract Classes and Interfaces
Interfaces.
Abstract classes, Interfaces
CS/ENGRD 2110 FALL 2016 Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
Object Oriented Programming
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
Chapter 14 Abstract Classes and Interfaces
Interfaces.
Chapter 13 Abstract Classes and Interfaces Part 01
Object-Oriented Java Programming
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Recitation 4 Abstract classes, Interfaces

A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area() radius __5__ Square area() size ____

Demo 1: Complete this function /** Return the sum of the areas of * the shapes in s */ static double sumAreas(Shape[] s) { } 1.Operator instanceof and casting are required 2.Adding new Shape subclasses breaks sumAreas Abstract Classes

A Partial Solution: Add method area to class Shape: Abstract Classes public double area() { return 0; } public double area() { throw new RuntimeException(“area not overridden”); }

Problems not solved 1.What is a Shape that isn’t a Circle, Square, Triangle, etc? What is only a shape, nothing more specific? a. Shape s= new Shape(...); Should be disallowed Abstract Classes 2. What if a subclass doesn’t override area()? a.Can’t force the subclass to override it! b.Incorrect value returned or exception thrown.

Solution: Abstract classes public abstract class Shape { public double area() { return 0; } Abstract Classes Abstract class Means that it can’t be instantiated. new Shape() illegal

Solution: Abstract methods public abstract class Shape { public abstract double area(); } Abstract Classes Abstract method Subclass must override. ● Can also have implemented methods ● Place abstract method only in abstract class. ● Semicolon instead of body.

Demo 2: A better solution We modify class Shape to be abstract and make area() an abstract method. ●Abstract class prevents instantiation of class Shape ●Abstract method forces all subclasses to override area() Abstract Classes

Abstract Classes, Abstract Methods 1.Cannot instantiate an object of an abstract class. (Cannot use new-expression) 2.A subclass must override abstract methods. Abstract Classes

Interfaces

Problem Mammal HumanParrotDog Whistler Interfaces Bird Animal Where is the best place to implement whistle() ?

No multiple inheritance in Java! class Whistler { void breathe() { … } } class Animal { void breathe() { … } } class Human extends Animal, Whistler { } Interfaces Which breathe() should java run in class Human? new Human().breathe();

Why not make it fully abstract? class abstract Whistler { abstract void breathe(); } class abstract Animal { abstract void breathe(); } class Human extends Animal, Whistler { } Interfaces Java doesn’t allow this, even though it would work. Instead, Java has another construct for this purpose, the interface

Solution: Interfaces public interface Whistler { void whistle(); int MEANING_OF_LIFE= 42; } class Human extends Mammal implements Whistler { } Interfaces Must implement all methods in the implemented interfaces ●methods are automatically public and abstract ●fields are automatically public, static, and final (i.e. constants)

Multiple interfaces public interface Singer { void singTo(Human h); } class Human extends Mammal implements Whistler, Singer { } Interfaces Classes can implement several interfaces! They must implement all the methods in those interfaces they implement. Must implement singTo(Human h) and whistle()

Solution: Interfaces Mammal HumanParrotDog Whistler Interfaces Bird Animal Interface Whistler offers promised functionality to classes Human and Parrot!

Casting to an interface Human h= new Human(); Object o= (Object) h; Animal a= (Animal) h; Mammal m= (Mammal) h; Singer s= (Singer) h; Whistler w= (Whistler) h; All point to the same memory address! Interfaces Singer Human Mammal Animal Object Whistler

Casting to an interface Human h= new Human(); Object o= h; Animal a= h; Mammal m= h; Singer s= h; Whistler w= h; Interfaces Singer Human Mammal Animal Object Whistler Automatic up-cast Forced down-cast

Casting up to an interface automatically class Human … implements Whistler { void listenTo(Whistler w) {...} } Human h = new Human(...); Human h1= new Human(...); h.listenTo(h1); Interfaces Human Mammal Animal Object Whistler Arg h1 of the call has type Human. Its value is being stored in w, which is of type Whistler. Java does an upward cast automatically. It costs no time; it is just a matter of perception.

Demo 3: Implement Comparable Implement interface Comparable in class Shape: public interface Comparable { /** = a negative integer if this object < c, = 0 if this object = c, = a positive integer if this object > c. Throw a ClassCastException if c can’t be cast to the class of this object. */ int compareTo(T c); }

Shape implements Comparable public class Shape implements Comparable {... /** … */ public int compareTo(Shape s) { double diff= area() - s.area(); return (diff == 0 ? 0 : (diff < 0 ? -1 : +1)); }

Beauty of interfaces Arrays.sort sorts an array of any class C, as long as C implements interface Comparable without needing to know any implementation details of the class. Classes that implement Comparable: Boolean Byte Double Integer String BigDecimal BigInteger Calendar Time Timestamp and 100 others

String sorting Arrays.sort(Object[] b) sorts an array of any class C, as long as C implements interface Comparable. String implements Comparable, so you can write String[] strings=...;... Arrays.sort(strings); During the sorting, when comparing elements, a String’s compareTo function is used

And Shape sorting, too! Arrays.sort(Object[] b) sorts an array of any class C, as long as C implements interface Comparable. Shape implements Comparable, so you can write Shape[] shapes=...;... Arrays.sort(shapes); During the sorting, when comparing elements, a Shape’s compareTo function is used

Abstract Classes vs. Interfaces ●Abstract class represents something ●Sharing common code between subclasses ●Interface is what something can do ●A contract to fulfill ●Software engineering purpose Similarities: ●Can’t instantiate ●Must implement abstract methods ●Later we’ll use interfaces to define “abstract data types” ○(e.g. List, Set, Stack, Queue, etc)