CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
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 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
Chapter 6 Class Hierarchies, Inheritance, and Interfaces  This is a very good chapter for us at this point in time.  We have been programming hard this.
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.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Using Jeroo To Teach Object-Oriented Concepts By Christian Digout.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Object Oriented Programming in Java Habib Rostami Lecture 6.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Inheritance A Review of Objects, Classes, and Subclasses.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
© 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.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Object-Oriented Programming: Inheritance and Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
 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.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Object-Oriented Programming Concepts
University of Central Florida COP 3330 Object Oriented Programming
Final and Abstract Classes
One class is an extension of another.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
LI: to calculate the perimeter of regular polygons.
Can perform actions and provide communication
One class is an extension of another.
Can perform actions and provide communication
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9: Polymorphism and Inheritance
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Can perform actions and provide communication
Object-Oriented Programming: Inheritance and Polymorphism
CIS 199 Final Review.
Final and Abstract Classes
Lecture 8 Object Oriented Programming (OOP)
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

CS 116 Object Oriented Programming II Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer

Topics Abstract Classes and Methods

Database of Figures You have been asked to lead a team of software engineers to design a database of figures Part of a graphical software Purpose To store position of various figures Store dimensions of the figure (length, breadth, radius, etc) Calculate area of the figures Challenge Figures have many, many shapes Circle, rectangle, square etc. Each figure has a different formula to calculate area and perimeter

Database of Figures Registry 4

Possible way forward Define a superclass called Figure that encapsulates the common attributes x coordinate and y coordinate public class Figure{ private int x; private int y; public Figure(int xcor, int ycor){ this.x=xcor; this.y=ycor; } //accessor and mutators public float distance(int x1, int y1){ //code to calculate the Euclid distance between two points. }

Database of Figures Registry Now, you can create subclasses Square, EquiTriangle and Circle that inherits the superclass Figure It is expected that the subclasses will have additional attributes (such as length for class Square and radius for class Circle) Subclasses will have additional methods (such as area and perimeter implementing the appropriate formula) How do you force the subclasses to implement area and perimeter?

abstract Classes and Methods An abstract class is a class that is not completely implemented. Purpose is to provide an appropriate superclass from which other classes can inherit and thus share a common design. Usually, the abstract class contains at least one abstract method. An abstract method provides specification but does not provide an implementation. The abstract method is used as a pattern for a method the subclasses should implement. A class with even one abstract method should be declared abstract. 7

Defining an abstract class To declare a class as abstract, include the abstract keyword in the class header: accessModifier abstract class ClassName { // class body } 8

Defining an abstract Method To declare a method as abstract, include the abstract keyword in the method header, and end the header with a semicolon: accessModifier abstract returnType methodName( argument list ); Note: The semicolon at the end of the header indicates that the method has no code. We do not use opening and closing curly braces { }. 9

A Possible Design of the Figures Database We can define a Figure hierarchy. The superclass is Figure, which is abstract. (In the diagram, Figure is an orange box to indicate that it is abstract.) We will derive three subclasses: Circle, EquiTriangle and Square 10 Object Figure -x :int -y :int +Constructors, +accessor/Mutator methods +distance() Square -length :double -noSides :int +Constructors +accessor/Mutator methods +area() +perimeter() EquiTriangle -length :double -noSides :int +Constructors +accessor/Mutator methods +area() +perimeter() Circle -radius :double +Constructors +accessor/Mutator methods +area() +perimeter() Figure is an abstract class It cannot be instantiated It can be subclassed Note 1: + indicates public – indicates private

A Possible Design of the Figures Database Open the zip file provided on the website and follow along Go through the Figure.java and Figclient.java files

Is there a better design? Both are regular polygons

13 Object Figure -x, -y +Constructors, +accessor/Mutator methods +distance() Square +Constructors +accessor/Mutator methods +area() EquiTriangle +Constructors +accessor/Mutator methods +area() Circle -radius +Constructors +accessor/Mutator methods +area() +perimeter() Polygon -lenght -noSides +Constructors, +accessor/Mutator methods +distance() +perimeter() Polygon is an abstract class that inherits from another abstract class Figure

Is there a better design? Go back to the in-class exercise and implement the Polygon abstract class.

More on abstract Classes An abstract class cannot be used to instantiate objects (because the class is not complete). An abstract class can be extended. subclasses can complete the implementation and objects of those subclasses can be instantiated An object reference to an abstract class can be declared. (Notice the word reference and not instantiation) We use this capability in polymorphism, which will be discussed later. 15

Subclasses of abstract Classes A subclass of an abstract class can implement all, some, or none of the abstract methods. If the subclass does not implement all of the abstract methods, it must also be declared as abstract. Our Circle subclass adds a radius instance variable and implements the draw method. Our Square subclass adds a length instance variable and implements the draw method. See Examples 10.15, 10.16, 10.17, & in text 16

Restrictions for Defining abstract Classes Classes must be declared abstract if the class contains any abstract methods abstract classes can be extended An object reference to an abstract class can be declared abstract classes cannot be used to instantiate objects 17

Restrictions for Defining abstract Methods abstract methods can be declared only within an abstract class An abstract method must consist of a method header followed by a semicolon abstract methods cannot be called abstract methods cannot be declared as private or static A constructor cannot be declared abstract 18