1 Chapter 9a Abstract Classes & Dynamic Binding. 2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create.

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
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.
Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Chapter 10: Introduction to Inheritance
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Sahar Shabanah Lecture 2. Outline Review of O-O Concepts Inheritance Interfaces Polymorphism Casting Generics Java Documentation Exceptions 2.
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
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 12 Advanced Inheritance
Chapter 10 Inheritance and Polymorphism
Java Applets and other things. Applets Very like usual classes Use init() method instead of main Applets provide 4 methods init() start() stop() destroy()
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
CMSC 202 Polymorphism.
Overriding Method.
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Introduction to Programming with Java
Object Oriented Programming
Abstract Classes.
Polymorphism CT1513.
Polymorphism, Abstract Classes & Interfaces
CS18000: Problem Solving and Object-Oriented Programming
Abstract Classes Page
Java – Inheritance.
Polymorphism CMSC 202, Version 4/02.
Java Inheritance.
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
Inheritance and Polymorphism
Chapter 9 Polymorphism.
Chapter 11 Inheritance and Polymorphism Part 1
Topics OOP Review Inheritance Review Abstract Classes
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

1 Chapter 9a Abstract Classes & Dynamic Binding

2 Abstract Classes All classes so far have been concrete classes –Classes that can be used to create object We can also create abstract classes –Classes that can not be used to create objects –Abstract classes can be inherited as the basis for other classes The purpose of an abstract class is to be inherited by another class

3 Abstract Classes An abstract class is a class that contains at least one abstract method An abstract method –Defined using the keyword abstract –A method that has no method statements The class that inherits the abstract class must override the abstract methods

4 Abstract Class Definition public abstract class Animal { private String name; public String getName( ) { return name; } public abstract void speak( ); }

5 Inheriting Abstract Classes public class Dog extends Animal { * public void speak( ) { screen.println(“Woof”); }

6 Declaring Objects Declaring an object of an abstract class is illegal Animal someAnimal = new Animal( ); Declaring an object of a class that inherits an abstract class Dog myLab = new Dog(“Belle”); myLab.speak( );

7 Abstract Classes Why? –Abstract classes allow for the definition of class methods without having to define the method’s code Each class that inherits the abstract class defines its version of the code for each abstract method

8 Binding –refers to the point in time when all the information needed to call a function is known Two types of binding –Early (Compile time) –Late (Run time) Dynamic binding is another name for late binding

9 Dynamic Binding A subclass object is also considered to be an object of the superclass –Every Dog object is also an Animal object While we can not create an object of an abstract class we can create a reference of the abstract class As a result a reference of an abstract class type can “point” to any object of a class that inherited the abstract class

10 Abstract Class References public class Cow extends Animal public class Dog extends Animal Animal ref;// reference Cow aCow = new Cow(“Bossie”); Dog aDog = new Dog(“Sophie”); ref = aCow; ref.speak( ); ref = aDog; ref.speak( );

11 Dynamic Binding Two different methods named speak( ) are executed Which version is executed is determined at run-time based on the reference address ref = aCow; ref.speak( ); ref = aDog; ref.speak( );

12 Significance A abstract class reference allows you to act upon a diverse group of similar class objects No objects of type Animal but a reference of type Animal allows you to access object of type Cow and Dog

13 Arrays of Subclass Objects public class Cow inherits Animal public class Dog inherits Animal Animal[ ] ref = new Animal[2]; // reference array Cow aCow = new Cow(“Bossie”); Dog aDog = new Dog(“Sophie”); ref [0] = aCow; ref [1] = aDog; for ( i = 0; i < 2; i++) ref [i].speak( );