Inheritance, Polymorphism, and Interfaces. Oh My

Slides:



Advertisements
Similar presentations
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.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance Recitation - 02/22/2008 CS 180 Department of Computer Science, Purdue University.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance using Java
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Types of Programming Languages
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Inheritance, Polymorphism, and Interfaces
Inheritance Chapter 7 Chapter 7.
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
CSE 1030: Implementing GUI Mark Shtern.
Computer Programming with JAVA
Java – Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Fundaments of Game Design
Chapter 8: Class Relationships
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

Inheritance, Polymorphism, and Interfaces. Oh My Chapter 8 Part 2 Modified by JJ

Announcements EXAM 2 in 2 weeks! Homework 7 due Wednesday at 11:55 PM

Sorting an Array of Fruit Objects Initial (non-working) attempt to sort an array of Fruit objects View class definition, listing 8.16 class Fruit View test class, listing 8.17 class FruitDemo Result: Exception in thread “main” Sort tries to invoke compareTo method but it doesn’t exist

Sorting an Array of Fruit Objects Working attempt to sort an array of Fruit objects – implement Comparable, write compareTo method View class definition, listing 8.18 class Fruit Result: Exception in thread “main” Sort tries to invoke method but it doesn’t exist

compareTo Method An alternate definition that will sort by length of the fruit name

Abstract Classes Class ShapeBasics is designed to be a base class for other classes Method drawHere will be redefined for each subclass It should be declared abstract – a method that has no body This makes the class abstract You cannot create an object of an abstract class – thus its role as base class

Abstract Classes Not all methods of an abstract class are abstract methods Abstract class makes it easier to define a base class Specifies the obligation of designer to override the abstract methods for each subclass

Abstract Classes Cannot have an instance of an abstract class But OK to have a parameter of that type View abstract version, listing 8.19 abstract class ShapeBase

Summary An interface contains Class which implements an interface must Headings of public methods Definitions of named constants No constructors, no private instance variables Class which implements an interface must Define a body for every interface method specified Interface enables designer to specify methods for another programmer

Summary Interface is a reference type Can be used as variable or parameter type Interface can be extended to create another interface Dynamic (late) binding enables objects of different classes to substitute for one another Must have identical interfaces Called polymorphism

Summary Derived class obtained from base class by adding instance variables and methods Derived class inherits all public elements of base class Constructor of derived class must first call a constructor of base class If not explicitly called, Java automatically calls default constructor

Summary this calls constructor of same class Within constructor this calls constructor of same class super invokes constructor of base class Method from base class can be overridden Must have same signature If signature is different, method is overloaded

Summary Overridden method can be called with preface of super Private elements of base class cannot be accessed directly by name in derived class Object of derived class has type of both base and derived classes Legal to assign object of derived class to variable of any ancestor type