Lecture 14 - Abstract Classes

Slides:



Advertisements
Similar presentations
CS 211 Inheritance AAA.
Advertisements

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
Inheritance using Java
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
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 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Chapter 8 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Java Inheritance.
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
One class is an extension of another.
Class Inheritance (Cont.)
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Lecture 14- Abstract Classes
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Abstract Classes Page
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Java Inheritance.
Fundaments of Game Design
Chapter 8 Inheritance Part 2.
Lecture 0311 – Polymorphism
Presentation transcript:

Lecture 14 - Abstract Classes Professor Adams

Abstract classes “When you extend an existing class, you have the choice of whether or not to redefine the method(s) of the super class…” When you want to force subclasses to redefine the method(s), make them abstract methods in the superclass. Lewis & Loftus p. 449

Abstract classes “A class that defines an abstract method, or that inherits an abstract method without overriding it, must be declared as abstract. You can also declare classes with no abstract methods as abstract. This prevents programmers from creating instances of that class but allows them to create their own subclasses.”

Abstract classes You can not construct an object of an abstract class but you can have an object reference whose type is an abstract class Example from text: BankAccount an Account; anAccount = new BankAccount(); class BankAccount in text on page 448 is not abstract but an abstract method was added to it on page 449 making it abstract anAccount = new SavingsAccount(); class SavingsAccount extended BankAccount anAccount = null;

Abstract classes Can have instance fields Can have some concrete methods

Rules for Abstract Classes An abstract class can not be instantiated An abstract class can’t have instances A class that can have have instances is said to be concrete An abstract class provides a prototype for other classes to follow

Subclasses of an Abstract Class will inherit the variables and methods of the abstract class will have the same basic characteristics are free to redefine variables and methods and add new ones must override any abstract methods of its parent.

Abstract Classes generally contain at least one abstract method are not required by the compiler to be declared abstract – but we require it are any classes containing at least one abstract method can contain non-abstract methods

Abstract Methods have the word abstract in their declaration do not have a body end their declarations with a semi-colon must be overridden in children of containing class are generally methods whose bodies will change from one subclass to another

Standard UML Notation + public - private # protected {abstract} top compartment – class name middle compartment – class attributes name : type bottom compartment – class methods returnType name (parameterList types)

Some Lewis & Loftus slides Abstract Classes Some Lewis & Loftus slides

Abstract Classes An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract: public abstract class Whatever { // contents }

Abstract Classes An abstract class often contains abstract methods with no definitions the abstract modifier must be applied to each abstract method An abstract class typically contains non-abstract methods (with bodies) A class declared as abstract does not need to contain abstract methods

Abstract Classes The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) The use of abstract classes is a design decision – it helps us establish common elements in a class that is too general to instantiate

Indirect Use of Members An inherited member can be referenced directly by name in the child class, as if it were declared in the child class But even if a method or variable is not inherited by a child, it can still be accessed indirectly through parent methods See FoodAnalysis.java (page 403) See FoodItem.java (page 404) See Pizza.java (page 405)