COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Object Oriented Programming
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Abstract Classes.
Chapter 10 Classes Continued
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Abstract classes and Interfaces. Abstract classes.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
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.,
Peyman Dodangeh Sharif University of Technology Fall 2014.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CIT 590 Intro to Programming Lecture 13. Agenda A few topics that you have seen but might not have fully grasped Static Public, private, protected etc.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
© 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.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Interfaces Warm-up: What is an abstract class? What is type casting?
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Final and Abstract Classes
Inheritance and Polymorphism
EKT 472: Object Oriented Programming
One class is an extension of another.
Chapter 13 Abstract Classes and Interfaces
One class is an extension of another.
More inheritance, Abstract Classes and Interfaces
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Final and Abstract Classes
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

COP 2800 Lake Sumter State College Mark Wilson, Instructor

Abstract Classes and Methods

 Superclasses are created through the process called "generalization" Common features (methods or variables) are factored out of object classifications (ie. classes). Those features are formalized in a class. This becomes the superclass The classes from which the common features were taken become subclasses to the newly created super class  Often, the superclass does not have a "meaning" or does not directly relate to a "thing" in the real world It is an artifact of the generalization process  Because of this, abstract classes cannot be instantiated  They act as place holders for abstraction

 Declared with abstract  Can not be instantiated  No constructor  Can be subclassed  Can contain concrete and abstract methods  If a class includes abstract methods, then the class itself must be declared abstract  Cannot be declared as final

 An abstract method cannot be contained in a non-abstract class  If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract.  A non-abstract subclass extended from an abstract class must implement all the abstract methods, even if they are not used in the subclass  A subclass can be abstract even if its superclass is concrete

 Has a signature but no implementation  Is a placeholder. Method must exist No meaningful implementation within this class  Any class which contains an abstract method MUST also be abstract  Any class which has an incomplete method definition cannot be instantiated (ie. it is abstract)  If a method can be implemented within an abstract class, and implementation should be provided

 Declared with abstract  No implementation  No braces, semicolon after the parameter list  Must be implemented (overridden) by a subclass  An abstract method cannot be defined as final or static

abstract class Shape { private int positionX; private int positionY; public void move (int newX, int newY) { positionX = newX; positionY = newY; } abstract draw(); // abstract resize(); // abstract abstract double getArea(); // methods abstract double getPerimeter(); // }

public class circle extends Shape{ private double radius; public void draw(){... } public void resize(){... } public double getArea(){... } public double getPerimeter(){... }

public class ShapeShifter { public static void main (String [] args) { Shape[] shapeList = { new Circle(3.0), new Rectangle(3.0, 4.0), new Rectangle(2.5, 7.5), new Circle(2.5), new Square(5.0) } for (int i = 0; i < shapeList.length; i++) { System.out.print (shapeList[i].toString( ) + “ ”); System.out.print (shapeList[i].area( ) + “ ”); System.out.println (shapeList[i].perimeter( )); }

Interfaces

 Polymorphism  Multiple inheritance, sort of  Class only extends a single superclass but, implements as many interfaces as it needs  Interfaces can cross class hierarchies

 Special case of abstract class  Declares abstract methods therefore no implementations  Can define constants ( final )  Defines a usage contract between classes  By definition public  By definition abstract

 A class can implement multiple interfaces: the interfaces are listed in the implements clause, separated by commas  A class that implements an interface, must define all methods in the interface  A class that implements an interface can implement other methods as well

public class Something implements Doable { public void doThis(){ // whatever } public void doThat(){ // whatever } // etc. } public interface Doable { public static final String NAME; void doThis(); int doThat(); void doThis2 (float value, char ch); boolean doTheOther (int num); }

public interface Speaker { public void speak(); } class Philosopher extends Human implements Speaker { public void speak() {…} public void pontificate() {…} } class Dog extends Animal implements Speaker { public void speak() {…} } Speaker guest; guest = new Philosopher(); guest.speak(); guest = Dog(); guest.speak(); Speaker special; special = new Philosopher(); special.pontificate(); // compiler error Speaker special; special = new Philosopher(); ((Philosopher)special).pontificate();

 You can write methods that work with more than one class interface RuleSet { boolean isLegal(Move m, Board b); void makeMove(Move m); }  Every class that implements RuleSet must have these methods class CheckersRules implements RuleSet { // one implementation public boolean isLegal(Move m, Board b) {... } public void makeMove(Move m) {... } }  This assignment is legal because a rulesOfThisGame object is a RuleSet object class ChessRules implements RuleSet {... } // another implementation class LinesOfActionRules implements RuleSet {... } // and another RuleSet rulesOfThisGame = new ChessRules();  This statement is legal because, whatever kind of RuleSet object rulesOfThisGame is, it must have isLegal and makeMove methods if (rulesOfThisGame.isLegal(m, b)) { makeMove(m); }

 Abstract Classes: Can have data fields Methods may have an implementation Classes and abstract classes extend abstract classes. Class cannot extend multiple abstract classes Substitution principle is assumed  Interfaces: Can only have constants Methods have no implementation Classes and abstract classes implement interfaces Interfaces can extend multiple interfaces A class can implement multiple interfaces Substitution principle not assumed

 Make a class that doesn’t extend anything when your class doesn’t pass the IS-A test for any other type  Extend a class only when you need to make a more specific version of a class AND need to override or add new behaviors  Use an abstract class to define a template for a group of classes or when you want to guarantee nobody makes objects of that type  Use an interface to define a role other classes can play regardless of where the classes are in the inheritance tree.

 Abstract classes can’t be instantiated  Abstract class can have concrete and abstract methods  A class must be abstract if it has any abstract methods  Abstract methods has no implementation and the declaration ends with a semicolon  All abstract methods must be implemented in the first concrete subclass in the inheritance tree

 Extending only one class avoids the diamond of death  Interface is like a 100% abstract class  Create interfaces with interface instead of class  Implement interfaces with implements keyword  Classes can implement multiple interfaces  Classes that implement interfaces must implement all the methods of the interface  All interface methods are implicitly public and abstract  Interfaces can apply to any class regardless of the class heirarchy

 Review of concepts, syntax and terminology  Mid-term exam ~ 100 objective style questions T-F Multiple guess Fill in the blank Short answer, including code fragments Matching  Anything covered in the reading assignments or the lectures is fair game