1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Abstract Class, Packages and interface from Chapter 9
Object Oriented Programming
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 Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Inheritance Inheritance Reserved word protected Reserved word super
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
More about classes and objects Classes in Visual Basic.NET.
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,
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Unit 9 1 Unit 9: Enhanced class design H In this unit: Abstract classes Packages basic programming concepts object oriented programming topics in computer.
UML Class Diagram: class Rectangle
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
UML Basics & Access Modifier
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
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,
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.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
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.
 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.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
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 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance ITI1121 Nour El Kadri.
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Week 6 Object-Oriented Programming (2): Polymorphism
Implementing Non-Static Features
Agenda About Homework for BPJ lesson 36 About practice of last class
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Dr. R Z Khan Handout-3 Classes
Final and Abstract Classes
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 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A Definition.  Abstract Methods: An Example.  Abstract Classes and Polymorphism.  A programming Example.  Summary.  Preview: Interfaces.

2 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes: A Definition An abstract class represents a generic concept in a class hierarchy. It has NO implementation. Question: Why do we need abstract classes? Answer: Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated (no class objects created). Take, for example, liquid in the real world. Have you ever seen an instance of liquid? No. What you see instead are instances of water, milk, and (our favorite) honey. Liquid represents the abstract concept of things that we all can drink (and more!). It doesn't make sense for an instance of liquid to exist.

3 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes: A Definition (Cont’d) Similarly in Java, you may want to model an abstract concept without being able to create an instance of it. For example, the Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to classes like Integer and Float, both of which implement specific kinds of numbers (see diagram below). A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class is a class that can only be inherited (subclassed). It cannot be instantiated. java.lang Number Integer Float Double Character Abstract superclass Implemented subclass

4 Lecture 06(Abstract Classes)Lecture 9 Declaring Abstract Classes To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration: If one attempts to create an instance of an abstract class, say AbstractClass, the compiler refuses to compile the program and displays an error similar to the following It is illegal to create an instance of an abstract class C:\ics_courses\semester002\Ghouti_Slides\Lecture9_Programs\AbstractClassTest.java:3: class Liquid is an abstract class. It can't be instantiated. Liquid ref1 = new Liquid(); ^1 error abstract class AbstractClass { } class body public class AbstractClassTest { public static void main(String args[]) { Liquid ref1 = new Liquid(); } // End of main() method } // End of class AbstractClassTest abstract class Liquid { int x, y; String name; } // End of abstract class Liquid Abstract Class

5 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: A Definition Another definition of abstract classes can be stated as follows: An abstract class is any class with at least one abstract method. Question: What is an abstract method? Answer: An abstract method is a method that does not contain an implementation. However, an abstract class can have instance data and non-abstract methods. The implementation details of the abstract methods are left to the inheriting classes (subclasses). Abstract classes have three distinguishing characteristics: 1- The declaration of an abstract class must include the word abstract, which is usually placed just before the word class. 2- Some of the methods in an abstract class may be abstract methods. An abstract method is a “dummy” method that has no body. Ex.public abstract double doubleValues(); 3- It is illegal to create an instance of an abstract class

6 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example Let's look at an example of when you might want to create an abstract class with an abstract method in it. In any drawing application, you can draw circles, rectangles, lines, triangles, and so on. Each of these graphic objects share certain states (position, bounding box) and behavior (move, resize, draw). You can take advantage of these similarities and declare them all to inherit from the same parent object, say abstract class Shape. Shape Circle Line Rectangle Triangle However, the graphic objects are also substantially different in many ways: drawing a circle is quite different from drawing a rectangle. The graphics objects cannot share these types of states or behavior. On the other hand, all the Shape objects must know how to draw themselves; they just differ in how they are drawn. This is a perfect situation for an abstract superclass.

7 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) First you would declare an abstract class, Shape, to provide member variables and methods that were wholly shared by all subclasses, such as the coordinates (x and y) and moveTo() method. Shape also declares abstract methods, such as draw(), that need to be implemented by all subclasses, but in entirely different ways (no default implementation in the superclass makes sense). The Shape class would look something like this: abstract class Shape { int x, y; // member variable... void moveTo(int newX, int newY) {... } // End of moveTo method abstract void draw(); } // End of abstract class Shape Notice the semicolon “;” No curly brackets!

8 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) Note that if the class Shape is not declared abstract, the compiler will not compile the program and an error message will be issued as follows: C:\ics_courses\semester002\Ghouti_Slides\Lecture9_Programs\Shape.java:1: class Shape must be declared abstract. It does not define void draw() from class Shape. class Shape { ^ 1 error Each non-abstract subclass of Shape, such as Circle and Rectangle, would have to provide an implementation for the draw() method. Remember that an abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. class Rectangle extends Shape { void draw() { } } Method body Overriding Implementation II class Circle extends Shape { void draw() { } } Method body Overriding Implementation I

9 Lecture 06(Abstract Classes)Lecture 9 l To illustrate abstract classes, suppose that we need to develop a series of classes that represents specific geometric shapes, such as Circle and Rectangle.. The Shape class will serve solely as a starting point for defining more-specific shapes classes. Every shape has a location and a color, so Shape class will need instance variables that store x and y coordinates and a Color object. Behaviors like: shape can be displayed, draw method., A shape can be moved so we need a move method.

10 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) l Shape // Represents a geometric shape that can be displayed in a // graphics context import java.awt.*; public abstract class Shape { // Instance variables private int x; private int y; private Color color; // Constructor protected Shape(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; } // Abstract methods public abstract void draw(Graphics g); public abstract int getHeight(); public abstract int getWidth(); // Other instance methods public Color getColor() { return color; } public int getX() { return x; } public int getY() { return y; } public void move(int dx, int dy) { x += dx; y += dy; } public void setColor(Color color) { this.color = color; } Notice that the shape declared protected so No instances can be created Abstract methods

11 Lecture 06(Abstract Classes)Lecture 9 Circle and Rectangle subclasses Circle Rectangle import java.awt.*; public class Circle extends Shape { // Instance variables private int diameter; // Constructor public Circle(int x, int y, Color color, int diameter) { super(x, y, color); this.diameter = diameter; } // Instance methods public void draw(Graphics g) { g.setColor(getColor()); g.fillOval(getX(), getY(), diameter, diameter); } public int getHeight() { return diameter; } public int getWidth() { return diameter; } import java.awt.*; public class Rectangle extends Shape { // Instance variables private int width; private int height; // Constructor public Rectangle(int x, int y, Color color, int width, int height) { super(x, y, color); this.width = width; this.height = height; } // Instance methods public void draw(Graphics g) { g.setColor(getColor()); g.fillRect(getX(), getY(), width, height); } public int getHeight() { return height; } public int getWidth() { return width; } } Because x, y, and color are declared private in the Shape class, the draw method can’t access these variables directly. Instead, it calls the getColor, getX and getY methods

12 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes and Polymorphism References of abstract class can be declared, but they should refer to an instance of the non-abstract subclasses as shown below: Shape shp = new Rectangle(); In this way, abstract classes and polymorphism provide a natural way of managing similar objects with a compact code. Consider the following inheritance diagram: Game Referee Player Ball Coach Abstract Superclass

13 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes and Polymorphism (Cont’d) abstract class Game { // fields for location, color, etc. // other methods public abstract void draw(); } // End of abstract class Game class Player extends Game {... public void draw() { method body } // End of draw() method } // End of class Player class Ball extends Game {... public void draw() { method body } // End of draw() method } // End of class Ball public class TestGame { public static void main(String[] args) { Game[] ahly = new Game[4]; ahly[0] = new Player(); ahly[1] = new Ball(); ahly[2] = new Referee(); ahly[3] = new Coach(); for (i = 0; i < 4; i ++) ahly[i].draw(); } // End of main() method } // End of class TestGame

14 Lecture 06(Abstract Classes)Lecture 9 Programming Example Consider the type of the following messages: TextMessage. VoiceMessage. FaxMessage. Message. These are different types of messages with the following common features: Sender name. Sending Date. Fields getSenderName(). getDate(). Methods However, each type of these messages has its own way of readMessage() the sent message. So, the implementation details of the readMessage() is left to the inheriting subclasses (TextMessage, VoiceMessage, FaxMessage, and Message). Methods will be implemented differently in each subclass

15 Lecture 06(Abstract Classes)Lecture 9 Programming Example (Cont’d) abstract class Message { private String sender; private Date date; public Message (String from) { sender = from; } // End of constructor public Date getDate() { return Date; } // End of getDate() method public String getSenderName() { return sender; } // End of getSenderName() method public abstract void readMessage(); } // End of class Message class TextMessage extends Message { private String text; public TextMessage(String from, String txt) { super(from); text = txt; } // End of constructor public void readMessage () { System.out.println(text); } // End of readMessage () method } // End of class TextMessage

16 Lecture 06(Abstract Classes)Lecture 9 Summary  An abstract class represents a generic concept in a class hierarchy.  An abstract method is a method that does not contain an implementation.  An abstract class is any class with at least one abstract method.  An abstract class cannot be instantiated: the presence of abstract method means that it is incomplete.  An abstract class can have instance data and non-abstract methods.  Abstract methods act as placeholders that should be implemented in subclasses.  A subclass should implement all abstract methods or itself declared as abstract.  References of an abstract class can be declared, but they should refer to an instance of the non-abstract subclass.