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.

Slides:



Advertisements
Similar presentations
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Advertisements

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.
Chapter 10: Introduction to Inheritance
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.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Abstract Classes.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
Inheritance using Java
Lecture 3 Casting Abstract Classes and Methods Interfaces.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
© 2004 Pearson Addison-Wesley. All rights reserved April 17, 2006 Polymorphism (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
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
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Object Oriented Programming
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
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:
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Polymorphism Chapter 10.
1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism in Methods
Web Design & Development Lecture 9
Lecture 12 Inheritance.
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Lecture 14 - Abstract Classes
Class Inheritance (Cont.)
Interface.
Java Programming Language
Lecture 14- Abstract Classes
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
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.
Overriding Methods & Class Hierarchies
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Java Inheritance.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Inheritance Part 2.
2009 Test Key.
Presentation transcript:

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   We use the modifier abstract on the class header to declare a class as abstract b b An abstract class often contains abstract methods (like an interface does), though it doesn’t have to

Abstract Classes b b The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract b b An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) b b The use of abstract classes is a design decision; it helps us establish common elements in a class that is to general to instantiate b b Usually concrete classes extend abstract ones, but the opposite is also possible. b b Can an abstract method be private?

3 References and abstract classes  Suppose the following two classes were defined: b public abstract class Figure  public class Rectangle extends Figure b Are these instantiations correct? Rectangle r = new Rectangle(...); Figure f = new Rectangle(...); Figure f = new Figure(...);

Interfaces b b A Java interface is a collection of abstract methods and constants   An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, it is usually left off b b An interface is used to formally define a set of methods that a class will implement b b A class can implement multiple interfaces b b The interfaces are listed in the implements clause, separated by commas

5 Interface example  We want to define an interface so that classes which implement it have meaningful string representation b Will the following definition accomplish this purpose? interface Printable { String toString(); } b Such interfaces are called marker interfaces b java.lang.Cloneable is another marker interface b Is this one different? interface Printable {}

6 Interface inheritance I  Just as class extends class an interface can extend interface b Unlike classes interface can extend more than one interface b Thus in Java we have - single implementation inheritance - multiple interface inheritance

7 Interface inheritance II public interface MyInterface1 { void method1(); } public interface MyInterface2 { String method2(); } public interface MyInterface3 extends MyInterface1, MyInterface2 { void method3(); } b b If you write a class that implements MyInterface2 you will have to implement method1(),method2() and method3().

8 When to use interfaces?  Interfaces allow multiple inheritance b Inheriting from a class allows implementation inheritance but class can extend only one other class b Any major class should be an implementation of an interface.

9 Polymorphism via Interfaces b b An interface defines a type just like any class in java. b b This means you can use interface names wherever you used primitive or object types.   For example look at FunctionDrawingApplet of Ex. 12