ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….

Slides:



Advertisements
Similar presentations
Java Programming Abstract classes and Interfaces.
Advertisements

Object Oriented Programming
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.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Lecture 28: Abstract Classes & Inheritance Announcements & Review Lab 8 Due Thursday Image and color effects with 2D arrays Read: –Chapter 9 Cahoon & Davidson.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
UML Class Diagram: class Rectangle
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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,
CS 307 Fundamentals of Computer ScienceInterfaces and Abstract Classes 1 Topic 7 Interfaces and Abstract Classes “I prefer Agassiz in the abstract, rather.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.,
Chapter 10 Inheritance and Polymorphism
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
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.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
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.
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,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface 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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Interfaces & Abstract Classes (For CS III AP) March 2014.
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 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Interface.
IST311 / 602 Cleveland State University – Prof. Victor Matos
UML Class Diagram: class Rectangle
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Chapter 13 Abstract Classes and Interfaces
Abstract classes and Interfaces
CSE 1030: Implementing Non-Static Features
Announcements & Review
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
IST311 / 602 Cleveland State University – Prof. Victor Matos
Chapter 8 Class Inheritance and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….

What is a type?  All objects have a type.  Is Tom Brady of type 'defensive lineman' or 'quarterback'?  Is Wisconsin of type 'state' or 'continent'?  Is Homer Simpson of type 'cartoon character' or 'pastry'?  A type is defined by two sets  A set of values  A set of operations on those values  Consider the int type for example  What is the set of values? An ‘int’ must be at integer number in the interval [-2,147,483,648 to 2,147,483,647]  What are the operations? +, -, *, /, % etc…

Does Java have types (ADTs)?  Consider more complex types such as TV  What might the values be?  VOLUME – an int in the range [0, 100]  STATUS – a boolean  CHANNEL – an int in the range [0, 999]  What might the operations be?  powerOn/powerOff  increaseVolume/decreaseVolume  upChannel/downChannel

A Class in Java IS an ADT  A class has a name and defines  the set of values that instances may take on  the set of operations that can be performed on those instances  From the TV example on the previous slide:  Might a TV have the following values? {VOLUME=32, STATUS=true, CHANNEL=31} {VOLUME=99, STATUS=12, CHANNEL=true}  Given a TV instance named ‘tv’ could we perform the following: tv.unplug() tv.increaseVolume()

Class Example  Values are defined by instance variables  Operations are defined by public methods public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } }

What is the 'state' of an object?  The state of an object is given by the values of all instance variables. In a sense, the state is the 'value' of the object.  Consider the following code and give the state of the relevant object after each highlighted line of code is executed. 6 public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } TV lg1 = new TV(); lg1.powerOn(); lg1.increaseVolume(); lg1.upChannel(); TV lg2 = new TV(); lg2.powerOn(); lg2.upChannel(); TV lg1 = new TV(); lg1.powerOn(); lg1.increaseVolume(); lg1.upChannel(); TV lg2 = new TV(); lg2.powerOn(); lg2.upChannel();

What does the word 'abstract' really mean? 7  What does the term 'abstract' in 'abstract art' convey?  Abstract artists capture the essence without the details (non- representational).  Abstract in programming essentially means “not completely defined”  In Java, two things can be labeled as abstract:  Method  Class  Captures a property or behavior of an object while not defining the implementation.  It specifies only the values and operations (the 'what')  Does not specify the operational details (the 'how')

Abstract thinking in Java 8  Abstract Methods:  Only the signature is known. modifiers/return type/method name/formal parameters/exceptions  The method body (implementation) is unknown.  Abstract Classes:  Contain one or more abstract methods.  Cannot be instantiated  Non-abstract sub-classes must implement all abstract methods

Abstract Class Example 9 public abstract class Shape { private double width, height; public abstract double getArea(); Shape(double w, double h) { width = w; height = h; } public String toString() { return “[" + width + "," + height + “,“ + getArea() + “]”; }

Abstract Class Example 10 class Rectangle extends Shape { Rectangle(double w, double h) { super(w,h); } public double getArea() { return width * height; } public String toString() { return “Rectangle” + super.toString(); }

Abstract Class Example 11 public class Demo { public static void main(String[] args) { Shape[] shapes = new Shape[5]; // Initialize the elements of the array at random for(int i=0; i<shapes.length; i++) { int width = (int)(Math.random() * 30) + 1; int height = (int)(Math.random() * 30) + 1; // Half of the shapes are ovals and half are rectangles // The type of shape is determined at runtime if(Math.random() <.5) { shapes[i] = new Oval(width, height); } else { shapes[i] = new Rectangle(width, height); } // Compute and print the total area of all shapes in the array double totalArea = 0; for(int i=0; i<shapes.length; i++) { System.out.println(shapes[i]); totalArea += shapes[i].getArea(); } System.out.println(“Total area of all shapes is “ + totalArea); } public class Demo { public static void main(String[] args) { Shape[] shapes = new Shape[5]; // Initialize the elements of the array at random for(int i=0; i<shapes.length; i++) { int width = (int)(Math.random() * 30) + 1; int height = (int)(Math.random() * 30) + 1; // Half of the shapes are ovals and half are rectangles // The type of shape is determined at runtime if(Math.random() <.5) { shapes[i] = new Oval(width, height); } else { shapes[i] = new Rectangle(width, height); } // Compute and print the total area of all shapes in the array double totalArea = 0; for(int i=0; i<shapes.length; i++) { System.out.println(shapes[i]); totalArea += shapes[i].getArea(); } System.out.println(“Total area of all shapes is “ + totalArea); }

What is an interface in Java? 12  An interface is a purely abstract class. It contains only  abstract methods  final variables  Any implementing class will contain all of the specified methods and the interface imposes no constraints on how the methods will work (although it does describe what they will do).  To be useful, an interface must be implemented by another class. If class A implements interface B then class A defines all methods in B.  Represents an is-a relationship between A and B.  A class can implement multiple interfaces  All interface methods must be implemented

Interface Example (Comparable) 13  Consider comparing two variables in Java to determine if one variable is less-than another.  if(a < b) { … }  What are the constraints on the types of variables a and b?  Can variables a and b be String (text) objects? Can they be TV objects? Can they be Color objects?  Comparable is a built-in interface defining a method for comparing complex objects. Implementing classes must define how comparison should be done.

Interface Example (Comparable) 14 public interface Comparable { public int compareTo(T other); }  Compares this object with the specified other object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. SignMeaninga.compareTo(b) NegativeThis is less than Thata < b ZeroThis is equal to Thata = b PositiveThis is greater than Thata > b

Interface Example (Comparable Shapes) 15 public abstract class Shape implements Comparable { private double width, height; public abstract double getArea(); Shape(double w, double h) { width = w; height = h; } public String toString() { return “[" + width + "," + height + “,“ + getArea() + “]”; } public int compareTo(Object rhs) { Shape other = (Shape)rhs; if(getArea() < other.getArea()) return –1; else if(getArea() == other.getArea()) return 0; else return 1; }

Interfaces essentially allow “multiple inheritance” 16 Object Number String Integer Date > Comparable > Comparable The Integer, String and Date classes all implement the Comparable interface. They all claim to be “Comparable” objects. In UML, implementing and interface is indicated by the dashed lines. The Integer, String and Date classes all implement the Comparable interface. They all claim to be “Comparable” objects. In UML, implementing and interface is indicated by the dashed lines.