Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Java 212: Inheritance and Polymorphism. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override (not.
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.
OOP: Inheritance By: Lamiaa Said.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Java 212: Inheritance and Polymorphism. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override (not.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 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.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Intro to OOP with Java, C. Thomas Wu
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance in the Java programming language J. W. Rider.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Modern Programming Tools And Techniques-I Inheritance
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Polymorphism CT1513.
Introduction to Java Programming
CIS 199 Final Review.
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Presentation transcript:

Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism

Review: Classes  User-defined data types  Defined using the “class” keyword  Each class has associated  Data members (any object type)  Methods that operate on the data  New instances of the class are declared using the “new” keyword  “Static” members/methods have only one copy, regardless of how many instances are created Java Programming: Program Design Including Data Structures2

Example: Shared Functionality Java Programming: Program Design Including Data Structures3 public class Student { String name; char gender; Date birthday; Vector grades; double getGPA() { … } int getAge(Date today) { … } public class Professor { String name; char gender; Date birthday; Vector papers; int getCiteCount() { … } int getAge(Date today) { … }

Java Programming: Program Design Including Data Structures4 public class Person { String name; char gender; Date birthday; int getAge(Date today) { … } public class Student extends Person { Vector grades; double getGPA() { … } public class Professor extends Person { Vector papers; int getCiteCount() { … }

Java Programming: Program Design Including Data Structures5 Inheritance  “is-a” relationship  Single inheritance:  Subclass is derived from one existing class (superclass)  Multiple inheritance:  Subclass is derived from more than one superclass  Not supported by Java  A class can only extend the definition of one class

Java Programming: Program Design Including Data Structures6 Inheritance (continued) modifier(s) class ClassName extends ExistingClassName modifier(s) { memberList }

Java Programming: Program Design Including Data Structures7 Inheritance: class Circle Derived from class Shape public class Circle extends Shape {. }

Inheritance  Allow us to specify relationships between types  Abstraction, generalization, specification  The “is-a” relationship  Examples?  Why is this useful in programming?  Allows for code reuse  More intuitive/expressive code

Code Reuse  General functionality can be written once and applied to *any* subclass  Subclasses can specialize by adding members and methods, or overriding functions

Inheritance: Adding Functionality  Subclasses have all of the data members and methods of the superclass  Subclasses can add to the superclass  Additional data members  Additional methods  Subclasses are more specific and have more functionality  Superclasses capture generic functionality common across many types of objects Java Programming: Program Design Including Data Structures10

Java Programming: Program Design Including Data Structures11 public class Person { String name; char gender; Date birthday; int getAge(Date today) { … } public class Student extends Person { Vector grades; double getGPA() { … } public class Professor extends Person { Vector papers; int getCiteCount() { … }

Brainstorming  What are some other examples of possible inheritance hierarchies?  Person -> student, faculty…  Shape -> circle, triangle, rectangle…  Other examples??? Java Programming: Program Design Including Data Structures12

Java Programming: Program Design Including Data Structures13 UML Diagram: Rectangle What if we want to implement a 3d box object?

Java Programming: Program Design Including Data Structures14 Objects myRectangle and myBox Rectangle myRectangle = new Rectangle(5, 3); Box myBox = new Box(6, 5, 4);

Java Programming: Program Design Including Data Structures15 UML Class Diagram: class Box Both a Rectangle and a Box have a surface area, but they are computed differently

Overriding Methods  A subclass can override (redefine) the methods of the superclass  Objects of the subclass type will use the new method  Objects of the superclass type will use the original Java Programming: Program Design Including Data Structures16

Java Programming: Program Design Including Data Structures17 class Rectangle public double area() { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } class Box public double area() { return getLength() * getWidth(); }

Java Programming: Program Design Including Data Structures18 final Methods  Can declare a method of a class final using the keyword final public final void doSomeThing() { //... }  If a method of a class is declared final, it cannot be overridden with a new definition in a derived class

Java Programming: Program Design Including Data Structures19 Calling methods of the superclass  To write a method’s definition of a subclass, specify a call to the public method of the superclass  If subclass overrides public method of superclass, specify call to public method of superclass: super.MethodName(parameter list)  If subclass does not override public method of superclass, specify call to public method of superclass: MethodName(parameter list)

Java Programming: Program Design Including Data Structures20 class Box public void setDimension(double l, double w, double h) { super.setDimension(l, w); if (h >= 0) height = h; else height = 0; }} Box overloads the method setDimension (Different parameters)

Java Programming: Program Design Including Data Structures21 Defining Constructors of the Subclass  Call to constructor of superclass:  Must be first statement  Specified by super parameter list public Box() { super(); height = 0; } public Box(double l, double w, double h) { super(l, w); height = h; }

Access Control  Access control keywords define which classes can access classes, methods, and members Java Programming: Program Design Including Data Structures22 ModifierClassPackageSubclassWorld publicYYYY protectedYYYN noneYYNN privateYNNN

Java Programming: Program Design Including Data Structures23 Polymorphism  Can treat an object of a subclass as an object of its superclass  A reference variable of a superclass type can point to an object of its subclass Person name, nameRef; PartTimeEmployee employee, employeeRef; name = new Person("John", "Blair"); employee = new PartTimeEmployee("Susan", "Johnson", 12.50, 45); nameRef = employee; System.out.println("nameRef: " + nameRef); nameRef: Susan Johnson wages are: $562.5

Java Programming: Program Design Including Data Structures24 Polymorphism  Late binding or dynamic binding (run-time binding):  Method to be executed is determined at execution time, not compile time  Polymorphism: to assign multiple meanings to the same method name  Implemented using late binding

Java Programming: Program Design Including Data Structures25 Polymorphism (continued)  The reference variable name or nameRef can point to any object of the class Person or the class PartTimeEmployee  These reference variables have many forms, that is, they are polymorphic reference variables  They can refer to objects of their own class or to objects of the classes inherited from their class

Java Programming: Program Design Including Data Structures26 Polymorphism and References Shape myShape = new Circle(); // allowed Shape myShape2 = new Rectangle(); // allowed Rectangle myRectangle = new Shame(); // NOT allowed

Java Programming: Program Design Including Data Structures27 Polymorphism (continued)  Can also declare a class final using the keyword final  If a class is declared final, then no other class can be derived from this class  Java does not use late binding for methods that are private, marked final, or static  Why not?

Java Programming: Program Design Including Data Structures28 Casting  You cannot automatically make reference variable of subclass type point to object of its superclass  Suppose that supRef is a reference variable of a superclass type and supRef points to an object of its subclass:  Can use a cast operator on supRef and make a reference variable of the subclass point to the object  If supRef does not point to a subclass object and you use a cast operator on supRef to make a reference variable of the subclass point to the object, then Java will throw a ClassCastException —indicating that the class cast is not allowed

Java Programming: Program Design Including Data Structures29 Polymorphism (continued)  Operator instanceof : determines whether a reference variable that points to an object is of a particular class type  This expression evaluates to true if p points to an object of the class BoxShape ; otherwise it evaluates to false : p instanceof BoxShape

Java Programming: Program Design Including Data Structures30 Abstract Methods  A method that has only the heading with no body  Must be implemented in a subclass  Must be declared abstract public double abstract area(); public void abstract print(); public abstract object larger(object, object); void abstract insert(int insertItem);

Java Programming: Program Design Including Data Structures31 Abstract Classes  A class that is declared with the reserved word abstract in its heading  An abstract class can contain instance variables, constructors, finalizers, and non-abstract methods  An abstract class can contain abstract methods

Java Programming: Program Design Including Data Structures32 Abstract Classes (continued)  If a class contains an abstract method, the class must be declared abstract  You cannot instantiate an object of an abstract class type; can only declare a reference variable of an abstract class type  You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass

Java Programming: Program Design Including Data Structures33 Abstract Class Example public abstract class AbstractClassExample { protected int x; public void abstract print(); public void setX(int a) { x = a; } public AbstractClassExample() { x = 0; }

Java Programming: Program Design Including Data Structures34 Interfaces  A class that contains only abstract methods and/or named constants  How Java implements multiple inheritance  To be able to handle a variety of events, Java allows a class to implement more than one interface

Java Programming: Program Design Including Data Structures35 Composition  Another way to relate two classes  One or more members of a class are objects of another class type  “has-a” relation between classes  For example, “every person has a date of birth”

Java Programming: Program Design Including Data Structures36 Composition Example

Java Programming: Program Design Including Data Structures37 Composition Example (continued)