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.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Java Programming Abstract classes and Interfaces.
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.
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.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
More about classes and objects Classes in Visual Basic.NET.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Lesson 3 Interfaces, Inhteritance, Polymorphism. What is an Java interface?  Like a class but only contains abstract methods and final variables example:
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.,
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Chapter 10 Inheritance and Polymorphism
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Peyman Dodangeh Sharif University of Technology Fall 2014.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 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,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance and Polymorphism
Interfaces.
Java Programming Language
Polymorphism and access control
Interfaces CS163 Fall 2018.
Week 6 Object-Oriented Programming (2): Polymorphism
Announcements & Review
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

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 specifying interfaces independently of their implementing classes

Example: An Interface for Circle import java.awt.Graphics; public interface CircleInterface { public void draw(Graphics g); public void move(int xDistance, int yDistance); public boolean equals(Object other); public String toString(); } An interface contains public method signatures and any necessary imports

Implementing an Interface import java.awt.*; public class Circle implements CircleInterface { // Variables and methods } import java.awt.*; public class Wheel extends Circle { // Variables and methods } Circle must implement draw, move, equals, and toString

The Client’s Perspective Circle c1 = new Circle(10,10,100,100,Color.blue); CircleInterface c2 = new Circle(); CircleInterface[] list = new CircleInterface[25]; Clients just look at the interface to see what they can do with circles An interface name can be substituted for the name of any implementing class

Implementing Several Interfaces import java.awt.*; import java.io.Serializable; public class Circle implements CircleInterface, Cloneable, Comparable, Serializable { // Variables and methods } Circle must implement draw, move, equals, and toString Circle must also implement clone and compareTo

Abstract and Concrete Classes An abstract class defines data and methods common to all subclasses, but is never instantiated A concrete class inherits some data and methods, defines others, and is instantiated

Example: Other Shapes RectangleTriangleCircle Wheel Rectangles and triangles are not really special types of circles, but have similar attributes and behavior

Define an AbstractShape Class RectangleTriangleCircle Wheel AbstractShape AbstractShape is never instantiated, but simply holds data and methods for its subclasses Each shape has a position, size, and color Most shapes move in a similar manner

An Interface for All Shapes import java.awt.Graphics; public interface Shape { public void draw(Graphics g); public void move(int xDistance, int yDistance); public boolean equals(Object other); public String toString(); }

Defining an Abstract Class abstract public class AbstractShape implements Shape{ protected Color color; protected int x, y, width, height; // Methods go here } abstract means that the class is not instantiated import java.awt.*; public class Circle extends AbstractShape { // Methods go here }

Polymorphism The move, draw, toString, and equals methods are polymorphic, in that they’re included in all classes but are implemented differently in some of them Try to exploit polymorphism to –reduce the amount of redundant code –reduce the conceptual overhead for clients

// In the AbstractShape class abstract public void draw(Graphics g); The keyword abstract requires all subclasses to implement a method An abstract method is implemented differently in each subclass Abstract Methods

// In the AbstractShape class abstract public double getArea(); When a Method Can’t Be Supported // In the Rectangle class public double getArea(){ return width * height; } // In the Line class public double getArea(){ throw new UnsupportedOperationException("Lines have no area"); return 0; }

// In the AbstractShape class public void move(int xDistance, int yDistance) x += xDistance; y += yDistance; } Overriding a Method // In the Triangle class public void move(int xDistance, int yDistance) super.move(xDistance, yDistance); // Code to adjust the other vertices goes here }

// In the AbstractShape class public final void setColor(Color c){ color = c; } The keyword final prohibits any subclass from overriding a method. Final Methods

Types of Polymorphism Behavioral – several classes implement one interface Structural – several classes implement one abstract class

The Advantages of Polymorphism // Create an array to hold up to 3 shapes of any type Shape[] shapes = new Shape[3]; // Add several shapes to the array shapes[0] = new Circle(50,50,100,100, Color.red); shapes[1] = new Rectangle(0,0,50,50, Color.blue); shapes[2] = new Triangle(0,0,100,100,60,30, Color.red); // Move them and draw them all for (int i = 0; i < shapes.length; i++){ shapes[i].move(50, 50); shapes[i].draw(g); }

Watch Out for Type Mismatches // Set the number of spokes of all wheels to 6 for (int i = 0; i < shapes.length; i++) shapes[i].setSpokes(6); Causes a compile-time error, because setSpokes is not included in the Shape interface and the array’s element type is Shape

Casting to the Rescue // Set the number of spokes of all wheels to 6 for (int i = 0; i < shapes.length; i++){ Wheel w = (Wheel)shapes[i]; w.setSpokes(6); } The compiler is now happy, but if shapes[i] is not actually a Wheel at run time, a ClassCastException will be thrown

Ask First // Set the number of spokes of all wheels to 6 for (int i = 0; i < shapes.length; i++){ if (shapes[i] instanceof Wheel){ Wheel w = (Wheel)shapes[i]; w.setSpokes(6); } Now both the compiler and the JVM will be happy

For Friday Exceptions and error handling Serialization (file transfers)