Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Chapter 1 Inheritance University Of Ha’il.
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.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 240 Week 3. List’Em java Grep [-r] directoryName fileSelectionPattern substringSelectionPattern Run Demo java LineCount [-r] directoryName fileSelectionPattern.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Topic 4 Inheritance.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Inheritance (Part 4) Abstract Classes.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
03/10/14 Inheritance-2.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Interfaces.
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Interfaces.
Chapter 11 Inheritance and Polymorphism Part 1
Summary.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CIS 110: Introduction to computer programming
Presentation transcript:

Inheritance (Part 5) Odds and ends 1

Static Methods and Inheritance  there is a significant difference between calling a static method and calling a non-static method when dealing with inheritance  there is no dynamic dispatch on static methods  therefore, you cannot override a static method  if you use a variable name instead of the class name to invoke the static method, you get the method that belongs to the declared type of the variable 2

3 public abstract class Dog { private static int numCreated = 0; public static int getNumCreated() { return Dog.numCreated; } public class Mix { private static int numMixCreated = 0; public static int getNumCreated() { return Mix.numMixCreated; } public class Komondor { private static int numKomondorCreated = 0; public static int getNumCreated() { return Komondor.numKomondorCreated; } notice

4 public class WrongCount { public static void main(String[] args) { Dog mutt = new Mix(); Dog shaggy = new Komondor(); System.out.println( mutt.getNumCreated() ); System.out.println( shaggy.getNumCreated() ); System.out.println( Mix.getNumCreated() ); System.out.println( Komondor.getNumCreated() ); } } prints Dog version Mix version Komondor version

What's Going On?  there is no dynamic dispatch on static methods  because the declared type of mutt is Dog, it is the Dog version of getNumCreated that is called  because the declared type of shaggy is Dog, it is the Dog version of getNumCreated that is called 5

Hiding Methods  notice that Mix.getNumCreated and Komondor.getNumCreated work as expected  if a subclass declares a static method with the same name as a superclass static method, we say that the subclass static method hides the superclass static method  you cannot override a static method, you can only hide it  hiding static methods is considered bad form because it makes code hard to read and understand 6

 the client code in WrongCount illustrates two cases of bad style, one by the client and one by the implementer of the Dog hierarchy 1. the client should not have used an instance to call a static method 2. the implementer should not have hidden the static method in Dog 7

Using superclass methods 8

Other Methods  methods in a subclass will often need or want to call methods in the immediate superclass  a new method in the subclass can call any public or protected method in the superclass without using any special syntax  a subclass can override a public or protected method in the superclass by declaring a method that has the same signature as the one in the superclass  a subclass method that overrides a superclass method can call the overridden superclass method using the super keyword 9

Dog equals  we will assume that two Dog s are equal if their size and energy are the public boolean equals(Object obj) { boolean eq = false; if(obj != null && this.getClass() == obj.getClass()) { Dog other = (Dog) obj; eq = this.getSize() == other.getSize() && this.getEnergy() == other.getEnergy(); } return eq; } 10

Mix equals (version 1)  two Mix instances are equal if their Dog subobjects are equal and they have the same public boolean equals(Object obj) { // the hard way boolean eq = false; if(obj != null && this.getClass() == obj.getClass()) { Mix other = (Mix) obj; eq = this.getSize() == other.getSize() && this.getEnergy() == other.getEnergy() && this.breeds.size() == other.breeds.size() && this.breeds.containsAll(other.breeds); } return eq; } 11 subclass can call public method of the superclass

Mix equals (version 2)  two Mix instances are equal if their Dog subobjects are equal and they have the same breeds  Dog equals already tests if two Dog instances are equal  Mix equals can call Dog equals to test if the Dog subobjects are equal, and then test if the breeds are equal  also notice that Dog equals already checks that the Object argument is not null and that the classes are the same  Mix equals does not have to do these checks again 12

@Override public boolean equals(Object obj) { boolean eq = false; if(super.equals(obj)) { // the Dog subobjects are equal Mix other = (Mix) obj; eq = this.breeds.size() == other.breeds.size() && this.breeds.containsAll(other.breeds); } return eq; } 13 subclass method that overrides a superclass method can call the original superclass method

Dog public String toString() { String s = "size " + this.getSize() + "energy " + this.getEnergy(); return s; } 14

Mix public String toString() { StringBuffer b = new StringBuffer(); b.append(super.toString()); for(String s : this.breeds) b.append(" " + s); b.append(" mix"); return b.toString(); } 15 size and energy of the dog breeds of the mix

Dog hashCode // similar to code generated by public int hashCode() { final int prime = 31; int result = 1; result = prime * result + this.getEnergy(); result = prime * result + this.getSize(); return result; } 16 use this.energy and this.size to compute the hash code

Mix hashCode // similar to code generated by public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + this.breeds.hashCode(); return result; } 17 use this.energy, this.size, and this.breeds to compute the hash code

Review 18

Review 1. Inheritance models the ______ relationship between classes. 2. Dog is a ______ of Object. 3. Dog is a ______ of Mix. 4. Can a Dog instance do everything a Mix instance can? 5. Can a Mix instance do everything a Dog instance can? 6. Is a Dog instance substitutable for a Mix instance? 7. Is a Mix instance substitutable for a Dog instance? 19

8. Can a subclass use the private fields of its superclass? 9. Can a subclass use the private methods of its superclass? 10. Suppose you have a class X that you do not want anyone to extend. How do you enforce this? 11. Suppose you have an immutable class X. Someone extends X to make it mutable. Is this legal? 12. What do you need to do to enforce immutability? 20

13. Suppose you have a class Y that extends X. a. Does each Y instance have a X instance inside of it? b. How do you construct the X subobject inside of the Y instance? c. What syntax is used to call the superclass constructor? d. What is constructed first–the X subobject or the Y object? e. Suppose Y introduces a brand new method that needs to call a public method in X named xMethod. How does the new Y method call xMethod? f. Suppose Y overrides a public method in X named xMethod. How does the overriding Y method call xMethod? 21

14. Suppose you have a class Y that extends X. X has a method with the following value must be a multiple of 2 If Y overrides the method which of the following are acceptable preconditions for the overriding method: value must be a multiple of 2 value must be odd value must be a multiple of 2 and must be less than 100 value must be a multiple of 10 none 22

14. Suppose you have a class Y that extends X. X has a method with the following – A String of length 10 If Y overrides the method which of the following are acceptable postconditions for the overriding method: – A String of length 9 or 10 – The String "weimaraner" – An int – The same String returned by toString – A random String of length 10 23

15. Suppose Dog toString has the following Javadoc: /* * Returns a string representation of a dog. * The string is the size of the dog followed by a * a space followed by the energy. The string representation of the dog. */ Does this affect subclasses of Dog? 24

Inheritance Recap  inheritance allows you to create subclasses that are substitutable for their ancestors  inheritance interacts with preconditions, postconditions, and exception throwing  subclasses  inherit all non-private features  can add new features  can change the behaviour of non-final methods by overriding the parent method  contain an instance of the superclass  subclasses must construct the instance via a superclass constructor 25