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,

Slides:



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

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
Classes and Objects in Java
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
More about inheritance Exploring polymorphism 3.0.
10-Aug-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is an abstract description of objects.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
What is inheritance? It is the ability to create a new class from an existing class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance, Abstract & Interface Pepper With help from and
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Classes and Objects in Java
Programming in Java CSCI-2220 Object Oriented Programming.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
BY:- TOPS Technologies
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Catie Welsh April 18,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Classes and Objects in Java
Week 8 Lecture -3 Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Overloading and Constructors
More inheritance, Abstract Classes and Interfaces
Lecture 19 - Inheritance (Contd).
METHOD OVERRIDING in JAVA
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Classes and Objects in Java
CLASSES, AND OBJECTS A FIRST LOOK
Chapter 9 Carrano Chapter 10 Small Java
Overview of Java 6-Apr-19.
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Inheritance and Polymorphism Part 1
Classes and Objects in Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

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, due Tues 2/17 at 10:00pm  Midterm 1 on Wed 2/18 at 6:00pm  Interview prep resources will come as soon as I have time to compile them (target: after Midterm 1)

Areas I Will Work On:  Writing bigger (remind me!)  Improve pacing – this may mean less individual work time  Will cold call if no volunteers (so work with each other) Important:  If you’re feeling behind, please reach out! , office hours, individual appointments.

ModifierOwn ClassPackageSubclassEverywhere public protected  default (package)  private 

 Models IS-A relationship between classes  Subclass can override behavior in superclass Match the method signature exactly.  Dynamic Method Lookup: ◦ Search for method in own class (dynamic type). ◦ Search for method in parent class (of dynamic type). ◦ All objects in Java inherit from Object. Account AccountFacebookAccount

public class Account { protected String username, password; public Account(String username, String password) { this.username = username; this.password = password; } public boolean login(String password) { /** CODE HERE **/ } } public class Account extends Account { public Account(String username, String password) { super(username, password); } public void fetch () { /** CODE HERE **/ } } public class FacebookAccount extends Account { public FacebookAccount(String username, String password) { super(username, password); loadNewsFeed(); } public void loadNewsFeed() { /** CODE HERE **/ } public void pokeFriend(Friend f) { /** CODE HERE **/ } } Account AccountFacebookAccount

public class Animal { protected String name, noise; protected int age; public Animal(String name, int age) { this.name = name; this.age = age; this.noise = "Huh?"; } public String makeNoise() { if (age < 5) { return noise.toUpperCase(); } else { return noise; } public void greet() { System.out.println("Animal " + name + " says: " + makeNoise()); }

Observations  Cat IS-A Animal (so extend)  Cat has different noise – so override value  Cat has different behavior for greet() – so override method  All other behavior is the same Animal String int name noise age String void makeNoise() greet() Cat voidgreet()

public class Cat extends Animal { public Cat(String name, int age) { super(name, age); this.noise = "Meow!"; public void greet() { System.out.println("Cat " + name + " says: " + makeNoise()); } So what does this print? public class Tester { public static void main(String[] args) { Animal a = new Animal("Fido", 5); a.greet(); Cat c = new Cat("Garfield", 10); c.greet(); } What if we had this as our constructor? public Cat(String name, int age) { this.noise = "Meow!"; super(name, age); } What if we had this as our constructor? public Cat(String name, int age) { this.noise = "Meow!"; super(name, age); } Where is makeNoise() defined? What does this annotation do?

 Static Type: Declared type of variable; known at compile time. Used by compiler to type-check.  Dynamic Type: Actual type of variable; known only at run time Used at runtime to call methods.  Cast changes static type and is promise to compiler that you will provide compatible type. Still executes based on dynamic type at runtime.

 Example 1: Assign specific to more general variable. Animal a = new Cat("Garfield", 10); a.greet(); Compiles and runs correctly. Calls Cat’s greet() method.  Example 2: Assign general to more specific variable. Cat c = new Animal("Pikachu", 10);  Compiler error.  Example 3: Casting Animal a = new Cat("Garfield", 10); Cat c = (Cat) a; Compiles correctly; potential runtime error.

public class Dog extends Animal { public Dog(String name, int age) { super(name, age); noise = "Woof!"; public void greet() { System.out.println("Dog " + name + " says: " + makeNoise()); } public void playFetch() { System.out.println("Fetch, " + name + "!"); } } Animal String int name noise age String void makeNoise() greet() Cat voidgreet() Dog void greet() playFetch()

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) ______________________ c.greet(); // (B) ______________________ d.greet(); // (C) ______________________ a = c; a.greet(); // (D) ______________________ ((Cat) a).greet(); // (E) ______________________ } Static TypeDynamic Type aAnimal cCat dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) ______________________ d.greet(); // (C) ______________________ a = c; a.greet(); // (D) ______________________ ((Cat) a).greet(); // (E) ______________________ } Static TypeDynamic Type aAnimal cCat dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) ______________________ a = c; a.greet(); // (D) ______________________ ((Cat) a).greet(); // (E) ______________________ } Static TypeDynamic Type aAnimal cCat dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; a.greet(); // (D) ______________________ ((Cat) a).greet(); // (E) ______________________ } Static TypeDynamic Type aAnimal cCat dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; a.greet(); // (D) Cat Garfield says: Meow! ((Cat) a).greet(); // (E) ______________________ } Static TypeDynamic Type aAnimalAnimal Cat cCat dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; a.greet(); // (D) Cat Garfield says: Meow! ((Cat) a).greet(); // (E) Cat Garfield says: Meow! } Static TypeDynamic Type aAnimalCat c dDog

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; a.greet(); // (D) Cat Garfield says: Meow! ((Cat) a).greet(); // (E) Cat Garfield says: Meow! a = new Dog("Hieronymus", 10); d = a; } }

public class TestAnimals { public static void main(String[] args) { Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; a.greet(); // (D) Cat Garfield says: Meow! ((Cat) a).greet(); // (E) Cat Garfield says: Meow! a = new Dog("Hieronymus", 10); d = a; d = (Dog) a; } Will this work at runtime?

Invocation of overridden methods:  Compiler plays it safe and only lets us do things allowed by static type.  The actual method invoked is based on dynamic type. Invocation of hidden static methods or hidden variables:  Actual method invoked or variable accessed is based on static type. Melanie’s awesome cheatsheet: Source: CS61B Sp’15 Lecture 9 Slide 35

 See Melanie’s cheatsheet:  Calling an instance method: ◦ Check: Static type (or any parent) has method signature. (Else: Compile-Time Error) ◦ Lookup: Start from dynamic type. If method found, use it, else search parent. (Repeat)  Calling a static method: ◦ Check: Same as above. ◦ Lookup: Start from static type, or use parent.  Invocation of a hidden variable: ◦ Access is based on static type of variable or method being called. Sources: Melanie’s Cheatsheet CS61B Sp’15 Lecture 9 Slide 35