CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Java Programming Abstract classes and Interfaces.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
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.
Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
This keyword.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
© Amir Kirsh Object Oriented Programming with Java Written by Amir Kirsh.
UML Basics & Access Modifier
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
10/7/2015IT 1791 Java’s World in UML Object Shape {abstract} This is done implicitly Shape {abstract} - ShapeName: String # setShapeName(newShapeName:
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Chapter 11 Inheritance and Polymorphism §11.1 Concept of Inheritance §11.2 Accessibility in Inheritance §11.3 Constructor/Destructor in Inheritance §11.4.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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….
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 11 Inheritance and Polymorphism.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
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.
Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.
1 More About Derived Classes and Inheritance Chapter 9.
Object-Oriented Programming (part 4 – Abstract Classes)
Chapter 11 Inheritance and Polymorphism
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Inheritance ITI1121 Nour El Kadri.
Advanced Program Design with C++
Interface.
Java’s World in UML Object Shape {abstract} This is done implicitly
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
CS 200 Creating Classes Jim Williams, PhD.
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract concepts comes closer to fulfilled utopia than that of eternal peace.” --Theodor W. Adorno

Example Drawing Application Create different shapes: circle, rectangle, …

So now we can instantiate a GeometricObject as follows: GeometricObject g = new GeometricObject(); But does it make sense ?

Solution – Abstract Classes! Do not allow instantiation of GeometricObject class How? 6 Declare it as abstract as follows: public abstract class GeometricObject { … }

Abstract Class Want to add methods to compute area and perimeter of any GeometricObject

8 Abstract Class Added methods to compute area and perimeter for any GeometricObject!

9 public abstract class GeometricObject { private String color = "white"; private boolean filled; protected GeometricObject() { filled = false; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public String toString() { return "color: " + color + " and filled: " + filled; }

10 public class Circle extends GeometricObject { private double radius; public Circle() { radius = 1.0; } public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return radius * radius * Math.PI; } public double getPerimeter() { return 2 * radius * Math.PI; }

11 public class Rectangle extends GeometricObject { private double width; private double height; public Rectangle() { width = 1.0; height = 1.0; } public Rectangle(double width, double height) { this.width = width; this.height = height; } public double getWidth() { return width; } public void setWidth(double w){ width = w; } public double getHeight() { return height; } public void setHeight(double h) { height = h; } public double getArea() { return width * height; } public double getPerimeter() { return 2 * (width + height); }

public class Main { public static void main(String[] args) { GeometricObject[] objects = new GeometricObject[5]; objects[0] = new Circle(5); objects[1] = new Rectangle(5, 3); for(int i = 0; i < objects.length; i++){ if(objects[i] != null) System.out.println("Area: " + objects[i].getArea() + "Perimeter: " + objects[i].getPerimeter()); } } Will give error!! cannot find symbol: getArea( ) and getPerimeter( )

13 public abstract class GeometricObject { private String color = "white"; private boolean filled; protected GeometricObject() { filled = false; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public String toString() { return "color: " + color + " and filled: " + filled; } public abstract double getArea(); public abstract double getPerimeter(); } Solution

public class Main { public static void main(String[] args) { GeometricObject[] objects = new GeometricObject[5]; objects[0] = new Circle(5); objects[1] = new Rectangle(5, 3); for(int i = 0; i < objects.length; i++){ if(objects[i] != null) System.out.println("Area: " + objects[i].getArea() + "Perimeter: " + objects[i].getPerimeter()); } } Will compile successfully now!

15 public abstract class GeometricObject { private String color = "white"; private boolean filled; protected GeometricObject() { filled = false; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public String toString() { return "color: " + color + " and filled: " + filled; } public double getArea() { return 0; } public double getPerimeter { return 0; } } Why not create concrete getArea( ) and getPerimeter( ) methods with no bodies??

Abstract Class Characteristics A class that contains abstract methods must be abstract. However, it is possible to declare an abstract class that contains no abstract methods. A subclass can be abstract even if its superclass is concrete. 16

Abstract Class, contd. All derived classes of an abstract class will have to implement its abstract method(s), or they too will be abstract An abstract class cannot be instantiated An abstract class can be used as a data type though. 17 GeometricObject g; GeometricObject[] geo = new GeometricObject[10];

Why is the following allowed? Why allow abstract classes to be used as data types if they can not be instantiated? 18 GeometricObject[] geo = new GeometricObject[10];

19 public class Main { public static void main(String[] args) { GeometricObject[] objects = new GeometricObject[5]; objects[0] = new Circle(5); objects[1] = new Rectangle(5, 3); for(int i = 0; i < objects.length; i++){ if(objects[i] != null) System.out.println("Area: " + objects[i].getArea() + "Perimeter: " + objects[i].getPerimeter()); } } Allows you to take advantage of polymorphism!

20 public class TestGeometricObject { public static void main(String[] args) { Circle c = new Circle(5); Rectangle r1 = new Rectangle(5, 3); Rectangle r2 = new Rectangle(3, 3); System.out.println(“c and r1 have equal area: " + equalArea(c, r1)); System.out.println(“r1 and r2 have equal area: " + equalArea(r1, r2)); } // what should the definition of equalArea() look like? public static boolean equalArea( ? obj1, ? obj2) { return obj1.getArea() == obj2.getArea(); }

21 public class TestGeometricObject { public static void main(String[] args) { Circle c = new Circle(5); Rectangle r1 = new Rectangle(5, 3); Rectangle r2 = new Rectangle(3, 3); System.out.println(“c and r1 have equal area: " + equalArea(c, r1)); System.out.println(“r1 and r2 have equal area: " + equalArea(r1, r2)); } // what should the definition of equalArea() look like? public static boolean equalArea( Circle o1, Rectangle o2){ return o1.getArea() == o2.getArea(); } public static boolean equalArea( Rectangle o1, Rectangle o2){ return o1.getArea() == o2.getArea(); } Option 1

22 public class TestGeometricObject { public static void main(String[] args) { Circle c = new Circle(5); Rectangle r1 = new Rectangle(5, 3); Rectangle r2 = new Rectangle(3, 3); System.out.println(“c and r1 have equal area: " + equalArea(c, r1)); System.out.println(“r1 and r2 have equal area: " + equalArea(r1, r2)); } public static boolean equalArea(GeometricObject obj1, GeometricObject obj2) { return obj1.getArea() == obj2.getArea(); }

23 public class TestGeometricObject { public static void main(String[] args) { GeometricObject geoObject1 = new Circle(5); GeometricObject geoObject2 = new Rectangle(5, 3); System.out.println(“geoObject1 and geoObject2 have area: " + equalArea(geoObject1, geoObject2)); System.out.println(“geoObject2 and geoObject3 have area: " + equalArea(geoObject2, geoObject3)); } public static boolean equalArea(Object obj1, Object obj2) { // why not declare the parameters as Object type? return obj1.getArea() == obj2.getArea(); } Will give an error as Object class has no getArea( ) method!

24 public class TestGeometricObject { public static void main(String[] args) { GeometricObject geoObject1 = new Circle(5); GeometricObject geoObject2 = new Rectangle(5, 3); System.out.println(“geoObject1 and geoObject2 have area: " + equalArea(geoObject1, geoObject2)); System.out.println(“geoObject2 and geoObject3 have area: " + equalArea(geoObject2, geoObject3)); } public static boolean equalArea(Object obj1, Object obj2) { // why not declare the parameters as Object type? GeometricObject g1 = (GeometricObject)obj1; GeometricObject g2 = (GeometricObject)obj2; return g1.getArea() == g2.getArea(); }

Get more info!