1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.

Slides:



Advertisements
Similar presentations
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
Advertisements

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.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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 abstract classes and casting Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Basics & Access Modifier
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
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.
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.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Java Polymorphism. What we have learned Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?
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,
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
// 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");
Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
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 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
Abstract Classes & Object Slicing. Object Slicing.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Object-Oriented Programming (part 4 – Abstract Classes)
Modern Programming Tools And Techniques-I
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.
Interface.
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1

2 / 41 Abstract Classes

3 / 41 Abstract classes  From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected float area; // Constructors… // get/set methods… }

4 / 41 Abstract classes  So we can do: GeometricObject gObj = new GeometricObject(“AnObject, Red”);

5 / 41 Abstract classes  So we can do: GeometricObject gObj = new GeometricObject(“AnObject, Red”); But does it make sense to do this? What is a “GeometricObject” by itself?

6 / 41 Abstract classes  Solution:  Make it abstract!

7 / 41 Abstract classes  Solution:  Make it abstract! public abstract class GeometricObject {

8 / 41 Abstract classes  Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.)

9 / 41 Abstract classes  Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.)  Then define “concrete” concepts as subclasses. (Circle, Rectangle, etc.)

10 / 41 Abstract classes  Used for defining classes for “abstract” concepts. (‘GeometricObject’, ‘Animal’, etc.)  Then define “concrete” concepts as subclasses. (Circle, Rectangle, etc.)  More strictness = less room for ambiguity/error.

11 / 41 Abstract classes  Every GeometricObject has an area.

12 / 41 Abstract classes  Every GeometricObject has an area.  But getArea() defined differently for concrete objects.

13 / 41 Abstract classes  Every GeometricObject has an area.  But getArea() defined differently for concrete objects.  (Not defined for the GeometricObject class)

14 / 41 Abstract classes // Circle public class Circle extends GeometricObject { public float getArea() { return Math.PI * radius * radius; }

15 / 41 Abstract classes // Circle public class Circle extends GeometricObject { public float getArea() { return Math.PI * radius * radius; } // Rectangle public class Rectangle extends GeometricObject { public float getArea() { return width * height; }

16 / 41 Abstract classes GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); }

17 / 41 Abstract classes GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); } Will not Compile! Cannot find “getArea()” in the GeometricObject class.

18 / 41 Abstract classes  So make getArea() an abstract method in GeometricObject.

19 / 41 Abstract classes  So make getArea() an abstract method in GeometricObject.  Only a declaration in GeometricObject: public abstract class GeometricObject {... public abstract float getArea(); }

20 / 41 Abstract classes GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); } Will now work! getArea() defined in GeometricObject

21 / 41 Abstract class characteristics  Class has abstract methods = has to be an abstract class

22 / 41 Abstract class characteristics  Class has abstract methods = has to be an abstract class  But: Abstract class can have no abstract methods.

23 / 41 Abstract class characteristics  Class has abstract methods = has to be an abstract class  But: Abstract class can have no abstract methods.  Subclass can be abstract, superclass can be concrete.

24 / 41 Abstract class characteristics  Concrete subclasses must define superclass abstract methods.  Circle, Rectangle have to define their own getArea();  Abstract methods can be defined, but usually aren’t.  Abstract classes cannot be instantiated.

25 / 41 Abstract class characteristics  Abstract class can be used as a data type though.  Example: GeometricObject gObj; GeometricObject[] objs = new GeometricObject[2];

26 / 41 Abstract class characteristics  Why is this allowed: GeometricObject[] objs = new GeometricObject[2];  They can’t be instantiated, so why allow as data types?

27 / 41 Abstract class characteristics GeometricObject[] objs = new GeometricObject[2]; objs[0] = new Circle(3); objs[1] = new Rectangle(1, 2); for (GeometricObject i : objs) { System.out.println(i.getArea()); } Can take advantage of polymorphism!

28 / 41 Abstract classes  Let’s say we need to check if areas of GeometricObjects are equal

29 / 41 Abstract classes  Let’s say we need to check if areas of GeometricObjects are equal  Define method called equalArea(...)

30 / 41 Abstract classes public static void main(String[] args) { Circle c1 = new Circle(3.0); Rectangle r1 = new Rectangle(2, 3); Rectangle r2 = new Rectangle(1, 6); SOP(“c1 and r1 have equal area: “ + equalArea(c, r1)); SOP(“r2 and r1 have equal area: “ + equalArea(r2, r1)); }

31 / 41 Abstract classes  How do you define equalArea(...)?

32 / 41 Abstract classes  How do you define equalArea(...)? public static boolean equalArea( ? obj1, ? obj2) { return obj1.getArea() == obj2.getArea(); }

33 / 41 Abstract classes  Could define it for all different comparisons: public static boolean equalArea( Circle c1, Rectangle r1) { return c1.getArea() == r2.getArea(); }

34 / 41 Abstract classes  Could define it for all different comparisons: public static boolean equalArea( Circle c1, Circle c2) { return c1.getArea() == c2.getArea(); }

35 / 41 Abstract classes  Could define it for all different comparisons: public static boolean equalArea( Rectangle r1, Rectangle r2) { return r1.getArea() == r2.getArea(); }

36 / 41 Abstract classes  Could define it for all different comparisons: public static boolean equalArea( Rectangle r1, Rectangle r2) { return r1.getArea() == r2.getArea(); } Tedious...

37 / 41 Abstract classes  Make it general thanks to abstract methods/classes: public static boolean equalArea( GeometricObject g1, GeometricObject g2) { return g1.getArea() == g2.getArea(); }

38 / 41 Abstract classes  Why not use the Object class? public static boolean equalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem?

39 / 41 Abstract classes  Why not use the Object class? public static boolean equalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem? Object class has no getArea() method!

40 / 41 Abstract classes  Why not use the Object class? public static boolean equalArea( Object g1, Object g2) { return g1.getArea() == g2.getArea(); } What’s the problem? Object class has no getArea() method! How would you fix it?

41 / 41 Abstract classes  Why not use the Object class? public static boolean equalArea( Object g1, Object g2) { GeometricObject go1 = (GeometricObject)g1; GeometricObject go2 = (GeometricObject)g2; return go1.getArea() == go2.getArea(); } Cast the objects

42 / 41 Next Lecture...  Interfaces.