SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
UML Class Diagram: class Rectangle
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
LECTURE 07 Programming using C# Inheritance
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Inheritance using Java
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Polymorphism & Interfaces
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
UML Review – class diagrams SE 2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Inheritance and Access Control CS 162 (Summer 2009)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Inheritance and Polymorphism
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
UML Review – class diagrams SE-2030 Dr. Mark L. Hornick 1.
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Sections Inheritance and Abstract Classes
Interfaces.
One class is an extension of another.
Abstract Classes.
Defining Your Own Classes Part 1
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Abstract Classes Page
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes

SE-1020 Dr. Mark L. Hornick 2 What if some attributes and behaviors are exactly the same in two (or more) different classes? Sometimes, instantiable classes may share common behavior for some methods For example: Consider classes that represent Dogs and Cats. Basic behaviors like getName() or getAge() might be identical…..while other behaviors may differ, like eat() or speak() In such cases, it would be nice to be able to implement that common behavior in only one place, to avoid unnecessary duplication… Remember, interfaces only declare methods, but cannot define them

SE-1020 Dr. Mark L. Hornick 3 Why not do this by having both Dog and Cat extend another class like Pet? Maybe, but consider these issues: Is Pet really a specific type of animal? What if we’re only dealing with Dogs or Cats? Do we really need a Pet class? Can we prevent a Pet from being created? Sometimes, a class represents the general aspects of more specific (ie derived) classes, but it doesn’t make sense to actually create objects of the general class. This situation can be handled with a special type of a class that is called an abstract class

SE-1020 Dr. Mark L. Hornick 4 An abstract class is defined with the modifier abstract public abstract class Pet {…} An abstract class can define any number of attributes and methods, like a regular class No instances can be created of an abstract class An abstract class may extend another abstract class

SE-1020 Dr. Mark L. Hornick 5 Example of an abstract class definition public abstract class Pet { protected String name; // attr defn protected int age; // attr defn … // implementation of shared behavior public void getName() { return name; } // defn public void getAge() { return age; } // defn … }

SE-1020 Dr. Mark L. Hornick 6 Abstract classes can also inherit interface behaviors, but do not have to implement them – delegating the implementation to subclasses public abstract class Pet implements Animal { protected String name; // attr defn protected int age; // attr defn … // implementation of shared behavior in Pet-derived subclasses: public void getName() { return name; } // defn public void getAge() { return age; } // defn … // Animal methods do not have to be implemented here, although they can be if desired. If not implemented here, they MUST be implemented in regular classes that extend Pet. }

SE-1020 Dr. Mark L. Hornick 7 The Java extends keyword is used to declare that a class inherits behaviors (and attributes) of another class, including abstract classes public class Dog extends Pet implements Mammal{ public void shedFur() { // defn of Mammal method } // only behaviors and attributes that are not implemented in Pet have to be defined here; Pet-defined attributes and behaviors are inherited by Dog // defn of Animal method inherited by Pet public void speak() { System.out.println(“Woof”); } }

In UML the relationship between class, abstract classes, and interfaces is illustrated as follows: SE-1020 Dr. Mark L. Hornick 8 The Generalization connector is used to illustrate that a class extends either a regular class or an abstract class Note the italics used for the abstract Pet class

SE-1020 Dr. Mark L. Hornick 9 Polymorphism again allows an interface variable to refer to objects from different classes that implement the interface For example, if Cat and Dog both extend an abstract class called Pet, then the following statements are valid Pet myDog, myCat; myCat = new Dog();... myCat = new Cat(); A Dog or a Cat can be used anyplace that expects a Pet as a method argument: public void feed( Pet p ) In a collection (e.g. ArrayList )

SE-1020 Dr. Mark L. Hornick 10 Inheritance versus Interface Use the Java interface to declare a behavior that must be implemented among related classes Methods on related classes are used the same way Example: ArrayList & LinkedList both share common behavior from the List interface Use extension inheritance to share common implementation Dog, Cat, Goldfish share implementation of getName() & getAge()

SE-1020 Dr. Mark L. Hornick 11 An abstract method is a method of an abstract class… …with the keyword abstract, and it ends with a semicolon instead of a method body public abstract void play(); Instantiable (non-abstract) subclasses of an abstract superclass with abstract methods MUST implement the inherited abstract methods Similar to overriding, but in this case overriding an unimplemented method

Abstract Pet class with an abstract play() method: Note that the abstract play() method is shown in italics in the abstract Pet class, but shown in regular font where implemented in the regular classes. Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 12

SE-1020 Dr. Mark L. Hornick 13 Why abstract methods? An abstract class may declare abstract methods that MUST be implemented in any (regular) class extending the abstract class In this way, an abstract method is like an interface method, which means that the declared behavior MUST be implemented at some point in the inheritance heirarchy.

SE-1020 Dr. Mark L. Hornick 14 Rules for abstract methods… abstract methods may not be declared private or static Since private and static methods cannot be overridden in subclasses