CS18000: Problem Solving and Object-Oriented Programming

Slides:



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

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.
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.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
©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, Static and Dynamic Typing. Announcements  Project 0 due Fri 2/13 at 11:59pm 24 slip hours (unused hours roll over)  HW3 released tonight,
Inheritance using Java
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Programming in Java CSCI-2220 Object Oriented Programming.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Polymorphism.
Interfaces.
Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Inheritance.
12 Data abstraction Packages and encapsulation
Interfaces and Inheritance
ATS Application Programming: Java Programming
Object Oriented Programming
Lecture 23 Polymorphism Richard Gesick.
More on Classes & Arrays
CSC 113 Tutorial QUIZ I.
Class Inheritance (Cont.)
More inheritance, Abstract Classes and Interfaces
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Polymorphism CMSC 202, Version 4/02.
Java Inheritance.
CMSC 202 Interfaces.
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
Inheritance and Polymorphism
Review of Previous Lesson
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Chapter 11 Inheritance and Encapsulation and Polymorphism
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:

CS18000: Problem Solving and Object-Oriented Programming Welcome slide for display pre-bell.

Abstract Classes Polymorphism Dynamic Binding

Polymorphism “Many forms” Animals can take on many forms … yet exhibit similar behaviors. Java allows a superclass variable to contain a reference to a subclass object The compiler chooses the subclass implementation of any overridden methods account.withdraw(amount); could be savings acct, checking acct, money market acct, ….

Code Reuse Suppose you’re modeling animals Dog Cat Fish Horse … Lots of redundancy, so you create a superclass Animal All other subclasses extend Animal Q: But what does “new Animal()” mean? A: Nothing--don’t want to create a “generic” animal Draw the modeled animals horizontally, then draw an Animal class that is the superclass for them (tree like).

Abstract Classes The Java solution for Animal: an abstract class Declaring a class abstract means that it cannot be instantiated Some methods may be unimplemented (just like an interface) But an abstract class may also include some implemented methods for default behavior The following example comes from Animal1: enter code live in DrJava.

Animal public abstract class Animal { abstract void speak(); public static void main(String[] args) { Animal[] animals = new Animal[2]; animals[0] = new Cat(); animals[1] = new Dog(); for (int i = 0; i < animals.length; i++) animals[i].speak(); } Could add a concrete method “shed” in Animal to implement common behavior between dogs and cats.

Cat and Dog public class Cat extends Animal { void speak() { System.out.printf("Meow\n"); } public class Dog extends Animal { System.out.printf(“Bark\n");

Dynamic Binding Methods are selected at runtime based on the class of the object referenced, not the class of the variable that holds the object reference Example Animal[] animals = new Animal[100]; animals[i].speak(); If animals[i] is a Dog,calls the speak() method in Dog, even though the variable is of type Animal “Binding” refers to the connection between name of the method and the method body being called. We say that the method name “speak” is bound to the method implementation in “Dog”. Drag out the first bullet: use a drawing on the chalkboard. The speak() example is weak since Animal does not have a concrete speak() method. Use shed instead and override it in Dog.

Why polymorphism? Allows generic treatment of objects An array of Animals Some are Dogs Some are Cats Some are new animal classes defined after the superclass code is written Programmer must be disciplined: the overridden methods should implement “consistent” or “expected” behavior Example: In Java, all GUI widgets are a subclass of Component; allows uniform treatment by GUI code See the GUI hierarchy reminder on the next slide.

Reminder: Subclass Object Contains its fields as well as all the fields defined in its superclasses… Dog object Fields defined in Object name The following example come from Animal2. Edit the code from Animal1 live to modify. Don’t forget to note what happens when adding a name constructor to Animal and compiling without a name constructor in the other classes. Fields defined in Animal … name Fields defined in Dog …

Revised: Dog public class Dog extends Animal { private String name; public Dog(String name) { super(name); this.name = name + " Barker"; } public String getName() { return name; void speak() { System.out.printf("Bark\n");

Revised: Animal public abstract class Animal { private String name; public Animal(String name) { this.name = name; } abstract void speak(); public static void main(String[] args) { Animal[] animals = new Animal[2]; animals[0] = new Cat("Garfield"); animals[1] = new Dog("Snoopy"); for (int i = 0; i < animals.length; i++) animals[i].speak(); Dog d = new Dog("Marmaduke"); System.out.println(d.getName()); Animal a = d; System.out.println(a.getName()); Summary: Don’t do this.

Abstract Methods Methods may be declared abstract Provide only the header (no body) Class must then be declared abstract Methods in an interface are implicitly declared abstract When subclassing an abstract class Generally provide method bodies for abstract methods If abstract methods remain, then subclass is still abstract and must be declared so

Example: Abstract Methods abstract public class AbstractParent { abstract void doOne(); abstract void doTwo(); } abstract class AbstractChild extends AbstractParent { void doOne() { System.out.println("in AbstractChild"); class ConcreteGrandChild extends AbstractChild { void doTwo() { System.out.println("in ConcreteGrandChild"); public static void main(String[] args) { ConcreteGrandChild cc = new ConcreteGrandChild(); cc.doOne(); cc.doTwo(); Output… hello there in AbstractChild in ConcreteGrandChild