SlideShare a Scribd company logo
1
TCSS 143, Autumn 2004
Lecture Notes
Inheritance
2
Inheritance: Definition
 inheritance: a parent-child relationship
between classes
 allows sharing of the behavior of the parent
class into its child classes
 one of the major benefits of object-oriented
programming (OOP) is this code sharing between
classes through inheritance
 child class can add new behavior or override
existing behavior from parent
3
Inheritance terms
 superclass, base class, parent class:
terms to describe the parent in the relationship,
which shares its functionality
 subclass, derived class, child class: terms
to describe the child in the relationship, which
accepts functionality from its parent
 extend, inherit, derive: become a subclass
of another class
4
Inheritance in Java
 in Java, you specify another class as your
parent by using the keyword extends
 public class CheckingAccount
extends BankAccount {
 the objects of your class will now receive all of the state
(fields) and behavior (methods) of the parent class
 constructors and static methods/fields are not inherited
 by default, a class's parent is Object
 Java forces a class to have exactly one parent
("single inheritance")
 other languages (C++) allow multiple inheritance
5
Inheritance Example
class BankAccount {
private double myBal;
public BankAccount() { myBal = 0; }
public double getBalance() { return myBal; }
}
class CheckingAccount extends BankAccount {
private double myInterest;
public CheckingAccount(double interest) { }
public double getInterest() { return myInterest; }
public void applyInterest() { }
}
 CheckingAccount objects have myBal and myInterest fields,
and getBalance(), getInterest(), and applyInterest() methods
6
Multiple layers of inheritance
 it is possible to extend a class that itself is a child
class; inheritance chains like this can be arbitrarily
deep
public class TransactionFeeCheckingAccount
extends CheckingAccount {
private static final double FEE = 2.00;
public void chargeFee() {
withdraw(FEE);
}
}
7
Inheritance Hierarchies
 Deeper layered chain of classes, many children
extending many layers of parents
8
"Has-a" Relationships
 "Has-a" relationship: when one object contains
another as a field
public class BankAccountManager {
private List myAccounts;
// ...
}
 a BankAccountManager object "has-a" List inside
it, and therefore can use it
9
"Is-a" relationships
 "Is-a" relationships represent sets of abilities;
implemented through interfaces and inheritance
public class CheckingAccount
extends BankAccount {
// ...
}
 a CheckingAccount object "is-a" BankAccount
 therefore, it can do anything an BankAccount can do
 it can be substituted wherever a BankAccount is needed
 a variable of type BankAccount may refer to a
CheckingAccount object
10
Using the account classes
 CheckingAccount inherits BankAccount's methods
CheckingAccount c = new CheckingAccount(0.10);
System.out.println(c.getBalance());
c.applyInterest();
 a BankAccount variable can refer to a
CheckingAccount object
BankAccount b2 = new CheckingAccount(0.06);
System.out.println(b2.getBalance());
 an Object variable can point to either account type
Object o = new BankAccount();
Object o2 = new CheckingAccount(0.09);
11
Some code that won't compile
 CheckingAccount variable can't refer to BankAccount
(not every BankAccount "is-a" CheckingAccount)
CheckingAccount c = new BankAccount();
 cannot call a CheckingAccount method on a variable of type
BankAccount (can only use BankAccount behavior)
BankAccount b = new CheckingAccount(0.10);
b.applyInterest();
 cannot use any account behavior on an Object variable
Object o = new CheckingAccount(0.06);
System.out.println(o.getBalance());
o.applyInterest();
12
Mixing inheritance, interfaces
 It is legal for a class to extend a parent and to implement any number of
interfaces
public class BankAccount {
// ...
}
public class NumberedAccount
extends BankAccount implements Comparable {
private int myNumber;
public int compareTo(Object o) {
return myNumber - ((BankAccount)o).myNumber;
}
}
13
Overriding behavior
 Child class can replace the behavior of its parent's
methods by redefining them
 you have already done this ... where?
public class BankAccount {
private double myBalance; // ....
public String toString() {
return getID() + " $" + getBalance();
} }
public class FeeAccount extends BankAccount {
private static final double FEE = 2.00;
public String toString() { // overriding
return getID() + " $" + getBalance()
+ " (Fee: $" + FEE + ")";
} }
14
Overriding behavior example
BankAccount b = new BankAccount("Ed", 9.0);
FeeAccount f = new FeeAccount("Jen", 9.0);
System.out.println(b);
System.out.println(f);
 Output:
Ed $9.0
Jen $9.0 (Fee: $2.0)
15
UML class diagrams
 an industry-standard way to
draw pictures of your
classes and their
relationships to each other
 classes are boxes that list
the fields, methods, and
constructors of the type
 classes that have
inheritance relationships, or
are tightly related to each
other, are connected by
arrows
16
Class Diagram: Single Class
 attributes (fields) are written as
accessModifier name : type
where accessModifier is one of
 - for private
 + for public
 # for protected
 example: - mySize: int
 methods are written as
accessModifier name(arg1 : type1,
arg2 : type2, ...) : returnType
 omit the : returnType if returnType is void
 example: + withdraw(amount : double) : boolean
17
Class diagram: inheritance
 inheritance relationships
 hierarchies drawn top-down with arrows from child to parent
 if parent is an interface, write its class name in << >> and
draw white dashed arrows
 if parent is a class, use black arrows
18
Class diagram: associations
 associational relationships
1. multiplicity (how many)
2. name (what relationship the objects have)
3. navigability (who has relationship with whom)
19
Class diagram: example
20
Access modifiers
 public: visible to all other classes
public class BankAccount
 private: visible only to the current class, its
methods, and every instance (object) of its class
 a child class cannot refer to its parent's private members!
private String myID;
 protected (this one's new to us): visible to the
current class, and all of its child classes
protected int myWidth;
 package (default access; no modifier): visible to all
classes in the current "package" (seen later)
int myHeight;
21
Access modifier problem (pt.1)
public class Parent {
private int field1;
protected int field2;
public int field3;
private void method1() {}
public void method2() {}
protected void setField1(int value) {
field1 = value;
}
}
22
Access modifier problem (pt.2)
public class Child extends Parent {
public int field4;
public Child() { // Which are legal?
field4 = 0; // _________
field1++; // _________
field2++; // _________
field3++; // _________
method1(); // _________
method2(); // _________
setField1(field4); // _________
}
}
23
Some code that won't compile
public class Point2D {
private int x, y;
public Point2D(int x, int y) {
this.x = x; this.y = y;
}
}
public class Point3D extends Point2D {
private int z;
public Point3D(int x, int y, int z) {
this.x = x; this.y = y; // can't do this!
this.z = z;
}
}
24
super keyword
 used to refer to superclass (parent) of current class
 can be used to refer to parent class's methods,
variables, constructors to call them
 needed when there is a name conflict with current class
 useful when overriding and you want to keep the old
behavior but add new behavior to it
 syntax:
super(args); // call parent’s constructor
super.fieldName // access parent’s field
super.methodName(args); // or method
25
super example
public class BankAccount {
private double myBalance;
public void withdraw(double amount) {
myBalance -= amount;
} }
public class FeeAccount
extends BankAccount {
public void withdraw(double amount) {
super.withdraw(amount);
if (getBalance() < 100.00)
withdraw(2.00); // charge $2 fee
} }
 didn't need to say super.getBalance() because the FeeAccount
subclass doesn't override that method (it is unambiguous which version of
the method we are talking about)
26
super and constructors
 if the superclass has a constructor that requires any arguments (not ()),
you must put a constructor in the subclass and have it call the super-
constructor (call to super-constructor must be the first statement)
public class Point2D {
private int x, y;
public Point2D(int x, int y) {
this.x = x; this.y = y;
}
}
public class Point3D extends Point2D {
private int z;
public Point3D(int x, int y, int z) {
super(x, y); // calls Point2D constructor
this.z = z;
}
}
27
The instanceof keyword
 Performs run-time type check on the object referred to
by a reference variable
 Usage: object-reference instanceof type
(result is a boolean expression)
 if type is a class, evaluates to true if the variable refers to an
object of type or any subclass of it.
 if type is an interface, evaluates to true if the variable refers
to an object that implements that interface.
 if object-reference is null, the result is false.
 Example:
Object o = myList.get(2);
if (o instanceof BankAccount)
((BankAccount)o).deposit(10.0);
28
Some instanceof problems
Object o = new BankAccount(...);
BankAccount c = new CheckingAccount(...);
BankAccount n = new NumberedAccount(...);
CheckingAccount c2 = null;
T/F ???
o instanceof Object _______
o instanceof BankAccount _______
o instanceof CheckingAccount _______
c instanceof BankAccount _______
c instanceof CheckingAccount _______
n instanceof CheckingAccount _______
n instanceof Comparable _______
n instanceof NumberedAccount _______
c2 instanceof Object _______
29
Which method gets called?
BankAccount b = new FeeAccount("Ed", 9.00);
b.withdraw(5.00);
System.out.println(b.getBalance());
 Will it call the withdraw method in
BankAccount, leaving Ed with $4?
 Will it call the withdraw method in
FeeAccount, leaving Ed with $2 (after his $2
fee)?
30
A hint about the right behavior
ArrayList list = new ArrayList();
list.add(new BankAccount("Ed", $9.0));
for (int i = 0; i < list.size(); i++) {
Object o = list.get(i);
System.out.println(o);
}
 Does it print "Object@FED87C" or
"Ed $9.00"?
31
The answer: dynamic binding
 The version of withdraw from FeeAccount
will be called
 The version of an object's method that gets
executed is always determined by that object's
type, not by the type of the variable
 The variable should only be looked at to
determine whether the code would compile;
after that, all behavior is determined by the
object itself
32
Static and Dynamic Binding
 static binding: methods and types that are
hard-wired at compile time
 static methods
 referring to instance variables
 the types of the reference variables you declare
 dynamic binding: methods and types that are
determined and checked as the program is
running
 non-static (a.k.a virtual) methods that are called
 types of objects that your variables refer to
33
Polymorphism
 inheritance, like interfaces, provides a way to
achieve polymorphism in Java
 polymorphism: the ability to use identical
syntax on different data types, causing possibly
different underlying behavior to execute
 example: If we have a variable of type
BankAccount and call withdraw on it, it might
execute the version that charges a fee, or the
version from the checking account that tallies
interest, or the regular version, depending on the
type of the actual object.
34
Type-casting and objects
 You cannot call a method on a reference unless the
reference's type has that method
Object o = new BankAccount("Ed", 9.00);
o.withdraw(5.00); // doesn't compile
 You can cast a reference to any subtype of its current
type, and this will compile successfully
((BankAccount)o).withdraw(5.00);
35
Down-casting and runtime
 It is illegal to cast a reference variable into an
unrelated type (example: casting a String
variable into a BankAccount)
 It is legal to cast a reference to the wrong
subtype; this will compile but crash when the
program runs
 Will crash even if the type you cast it to has the
method in question
((String)o).toUpperCase(); // crashes
((FeeAccount)o).withdraw(5.00); // crashes
36
A dynamic binding problem
class A {
public void method1() { System.out.println(“A1”); }
public void method3() { System.out.println(“A3”); }
}
class B extends A {
public void method2() { System.out.println(“B2”); }
public void method3() { System.out.println(“B3”); }
}
A var1 = new B();
Object var2 = new A();
var1.method1();
var1.method2();
var2.method1(); OUTPUT???
((A)var2).method3();
37
A problem with interfaces
public interface Shape2D {
int getX();
int getY();
double getArea();
double getPerimeter();
}
 Every shape will implement getX and getY the
same, but each shape probably implements
getArea and getPerimeter differently
38
A bad solution
public class Shape implements Shape2D {
private int myX, myY;
public Shape(int x, int y) {
myX = x; myY = y;
}
public int getX() { return myX; }
public int getY() { return myY; }
// subclasses should override these, please
public double getArea() { return 0; }
public double getPerimeter() { return 0; }
}
 BAD: the Shape class can be instantiated
 BAD: a subclass might forget to override the methods
39
Abstract classes
 abstract class: a hybrid between an interface and a
class
 used to define a generic parent type that can contain method
declarations (like an interface) and/or method bodies (like a
class)
 like interfaces, abstract classes that cannot be instantiated
(cannot use new to create any objects of their type)
What goes in an abstract class?
 implement common state and behavior that will be
inherited by subclasses (parent class role)
 declare generic behaviors that subclasses must
implement (interface role)
40
Abstract class syntax
 put abstract keyword on class header and on
any generic (abstract) methods
 A class can be declared abstract even though it
has no abstract methods
 Any class with abstract methods must be declared
abstract, or it will not compile
 Variables of abstract types may be declared,
but objects of abstract types cannot be
constructed
41
Abstract class example
public abstract class Shape implements Shape2D {
private int myX, myY;
public Shape(int x, int y) {
myX = x; myY = y;
}
public int getX() { return myX; }
public int getY() { return myY; }
public abstract double getArea();
public abstract double getPerimeter();
}
 Shape class cannot be instantiated
 all classes that extend Shape must implement getArea and
getPerimeter or else must also be declared abstract
42
Extending an abstract class
public class Rectangle extends Shape {
private int myWidth, myHeight;
public Rectangle(int x, int y, int w, int h) {
super(x, y);
myWidth = w; myHeight = h;
}
public double getArea() {
return myWidth * myHeight;
}
public double getPerimeter() {
return 2*myWidth + 2*myHeight;
}
}
// ... example usage ...
Shape rect = new Rectangle(1, 2, 10, 5);
43
Interface / abstract class chart
44
Questions, Practice problem
 What are the differences between interfaces
and abstract classes? Why is it useful that both
exist in the Java language? When should one
use an abstract class and when an interface?
 Modify our old IntArrayList and
IntLinkedList so that they have a common
abstract base class AbstractIntList that
contains the common functionality between
them.
45
Inner classes
 inner class: a class defined inside of another
class
 can be created as static or non-static
 we will focus on standard non-static inner classes
 usefulness:
 inner classes can be "hidden" from other classes
(encapsulation at class level)
 useful when implementing data structures (e.g.
linked lists) or for GUI programming
46
Inner class behavior
 provide syntactic nesting of classes,
plus an association to the particular
object of the outer "enclosing" class that
created them
 each object I of the inner class is implicitly bound to
the object O (of the outer class) that created it
 can refer to it as OuterClassName.this if necessary
 an object of an inner class can access the fields and
methods of the outer object that created it (including
private fields and methods)
47
Inner class example
public class MyArrayList {
private Object[] items;
public Iterator iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator {
int index = 0;
public boolean hasNext() {
return currentItem < size();
}
public Object next() {
if (!hasNext())
throw new NoSuchElementException();
else return items[index++];
}
}
}
48
References
 Koffman/Wolfgang Ch. 3, pp. 125-149, 155-
159; Appendix B.1, pp. 738-744
 The Java Tutorial: Implementing Nested Classes.
http://java.sun.com/docs/books/tutorial/java/
javaOO/nested.html
 The Java Tutorial: Inheritance.
http://java.sun.com/docs/books/tutorial/java/
concepts/inheritance.html

More Related Content

PDF
Object oriented programming
PPTX
Inheritance
PPT
Inheritance (1)
PPT
object oriented programming language by c++
DOCX
Introduction to object oriented programming concepts
PPTX
Inheritance
PDF
PPTX
OOP C++
Object oriented programming
Inheritance
Inheritance (1)
object oriented programming language by c++
Introduction to object oriented programming concepts
Inheritance
OOP C++

What's hot (19)

PPT
inhertance c++
PPT
Lecture07
PDF
Constructor & destructor
PDF
Java unit2
DOCX
JAVA Notes - All major concepts covered with examples
PDF
PPTX
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPTX
Inheritance
DOCX
CIS 407 STUDY Inspiring Innovation--cis407study.com
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPT
PPTX
Inheritance and Interfaces
PPT
Synapseindia dotnet development chapter 14 event-driven programming
PPTX
Opp concept in c++
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PDF
Pythia Reloaded: An Intelligent Unit Testing-Based Code Grader for Education
PPTX
Friend functions
PPTX
Class and object
DOCX
Sdtl assignment 03
inhertance c++
Lecture07
Constructor & destructor
Java unit2
JAVA Notes - All major concepts covered with examples
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Inheritance
CIS 407 STUDY Inspiring Innovation--cis407study.com
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Inheritance and Interfaces
Synapseindia dotnet development chapter 14 event-driven programming
Opp concept in c++
OBJECT ORIENTED PROGRAMING IN C++
Pythia Reloaded: An Intelligent Unit Testing-Based Code Grader for Education
Friend functions
Class and object
Sdtl assignment 03
Ad

Similar to 06 inheritance (20)

PPT
Inheritance
PDF
Chapter- 3 Inheritance and Polymorphism-1x4.pdf
PPT
Object Oriented Programming Concept.Hello
PDF
Inheritance
PDF
Class notes(week 6) on inheritance and multiple inheritance
PDF
Java Programming - 04 object oriented in java
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPT
M251_Meeting 5 (Inheritance and Polymorphism).ppt
PPTX
Multiple Inheritance
PPTX
OOPS IN C++
PPT
Class 7 - PHP Object Oriented Programming
PPT
Java class
PDF
Inheritance
ODP
Ppt of c++ vs c#
PPT
Inheritance
PPT
Jar chapter 5_part_i
PPTX
Object oriented programming in php 5
PPTX
Object oriented programming in php 5
PPT
Visula C# Programming Lecture 7
PPTX
Inheritance in Java beginner to advance with examples.pptx
Inheritance
Chapter- 3 Inheritance and Polymorphism-1x4.pdf
Object Oriented Programming Concept.Hello
Inheritance
Class notes(week 6) on inheritance and multiple inheritance
Java Programming - 04 object oriented in java
Chapter 6 and inheritance OOP C++ tu ioe
M251_Meeting 5 (Inheritance and Polymorphism).ppt
Multiple Inheritance
OOPS IN C++
Class 7 - PHP Object Oriented Programming
Java class
Inheritance
Ppt of c++ vs c#
Inheritance
Jar chapter 5_part_i
Object oriented programming in php 5
Object oriented programming in php 5
Visula C# Programming Lecture 7
Inheritance in Java beginner to advance with examples.pptx
Ad

Recently uploaded (20)

PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
composite construction of structures.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Geodesy 1.pptx...............................................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Digital Logic Computer Design lecture notes
PPT
Project quality management in manufacturing
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
composite construction of structures.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Geodesy 1.pptx...............................................
Embodied AI: Ushering in the Next Era of Intelligent Systems
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT-1 - COAL BASED THERMAL POWER PLANTS
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Foundation to blockchain - A guide to Blockchain Tech
UNIT 4 Total Quality Management .pptx
CYBER-CRIMES AND SECURITY A guide to understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
bas. eng. economics group 4 presentation 1.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Digital Logic Computer Design lecture notes
Project quality management in manufacturing
Operating System & Kernel Study Guide-1 - converted.pdf
R24 SURVEYING LAB MANUAL for civil enggi

06 inheritance

  • 1. 1 TCSS 143, Autumn 2004 Lecture Notes Inheritance
  • 2. 2 Inheritance: Definition  inheritance: a parent-child relationship between classes  allows sharing of the behavior of the parent class into its child classes  one of the major benefits of object-oriented programming (OOP) is this code sharing between classes through inheritance  child class can add new behavior or override existing behavior from parent
  • 3. 3 Inheritance terms  superclass, base class, parent class: terms to describe the parent in the relationship, which shares its functionality  subclass, derived class, child class: terms to describe the child in the relationship, which accepts functionality from its parent  extend, inherit, derive: become a subclass of another class
  • 4. 4 Inheritance in Java  in Java, you specify another class as your parent by using the keyword extends  public class CheckingAccount extends BankAccount {  the objects of your class will now receive all of the state (fields) and behavior (methods) of the parent class  constructors and static methods/fields are not inherited  by default, a class's parent is Object  Java forces a class to have exactly one parent ("single inheritance")  other languages (C++) allow multiple inheritance
  • 5. 5 Inheritance Example class BankAccount { private double myBal; public BankAccount() { myBal = 0; } public double getBalance() { return myBal; } } class CheckingAccount extends BankAccount { private double myInterest; public CheckingAccount(double interest) { } public double getInterest() { return myInterest; } public void applyInterest() { } }  CheckingAccount objects have myBal and myInterest fields, and getBalance(), getInterest(), and applyInterest() methods
  • 6. 6 Multiple layers of inheritance  it is possible to extend a class that itself is a child class; inheritance chains like this can be arbitrarily deep public class TransactionFeeCheckingAccount extends CheckingAccount { private static final double FEE = 2.00; public void chargeFee() { withdraw(FEE); } }
  • 7. 7 Inheritance Hierarchies  Deeper layered chain of classes, many children extending many layers of parents
  • 8. 8 "Has-a" Relationships  "Has-a" relationship: when one object contains another as a field public class BankAccountManager { private List myAccounts; // ... }  a BankAccountManager object "has-a" List inside it, and therefore can use it
  • 9. 9 "Is-a" relationships  "Is-a" relationships represent sets of abilities; implemented through interfaces and inheritance public class CheckingAccount extends BankAccount { // ... }  a CheckingAccount object "is-a" BankAccount  therefore, it can do anything an BankAccount can do  it can be substituted wherever a BankAccount is needed  a variable of type BankAccount may refer to a CheckingAccount object
  • 10. 10 Using the account classes  CheckingAccount inherits BankAccount's methods CheckingAccount c = new CheckingAccount(0.10); System.out.println(c.getBalance()); c.applyInterest();  a BankAccount variable can refer to a CheckingAccount object BankAccount b2 = new CheckingAccount(0.06); System.out.println(b2.getBalance());  an Object variable can point to either account type Object o = new BankAccount(); Object o2 = new CheckingAccount(0.09);
  • 11. 11 Some code that won't compile  CheckingAccount variable can't refer to BankAccount (not every BankAccount "is-a" CheckingAccount) CheckingAccount c = new BankAccount();  cannot call a CheckingAccount method on a variable of type BankAccount (can only use BankAccount behavior) BankAccount b = new CheckingAccount(0.10); b.applyInterest();  cannot use any account behavior on an Object variable Object o = new CheckingAccount(0.06); System.out.println(o.getBalance()); o.applyInterest();
  • 12. 12 Mixing inheritance, interfaces  It is legal for a class to extend a parent and to implement any number of interfaces public class BankAccount { // ... } public class NumberedAccount extends BankAccount implements Comparable { private int myNumber; public int compareTo(Object o) { return myNumber - ((BankAccount)o).myNumber; } }
  • 13. 13 Overriding behavior  Child class can replace the behavior of its parent's methods by redefining them  you have already done this ... where? public class BankAccount { private double myBalance; // .... public String toString() { return getID() + " $" + getBalance(); } } public class FeeAccount extends BankAccount { private static final double FEE = 2.00; public String toString() { // overriding return getID() + " $" + getBalance() + " (Fee: $" + FEE + ")"; } }
  • 14. 14 Overriding behavior example BankAccount b = new BankAccount("Ed", 9.0); FeeAccount f = new FeeAccount("Jen", 9.0); System.out.println(b); System.out.println(f);  Output: Ed $9.0 Jen $9.0 (Fee: $2.0)
  • 15. 15 UML class diagrams  an industry-standard way to draw pictures of your classes and their relationships to each other  classes are boxes that list the fields, methods, and constructors of the type  classes that have inheritance relationships, or are tightly related to each other, are connected by arrows
  • 16. 16 Class Diagram: Single Class  attributes (fields) are written as accessModifier name : type where accessModifier is one of  - for private  + for public  # for protected  example: - mySize: int  methods are written as accessModifier name(arg1 : type1, arg2 : type2, ...) : returnType  omit the : returnType if returnType is void  example: + withdraw(amount : double) : boolean
  • 17. 17 Class diagram: inheritance  inheritance relationships  hierarchies drawn top-down with arrows from child to parent  if parent is an interface, write its class name in << >> and draw white dashed arrows  if parent is a class, use black arrows
  • 18. 18 Class diagram: associations  associational relationships 1. multiplicity (how many) 2. name (what relationship the objects have) 3. navigability (who has relationship with whom)
  • 20. 20 Access modifiers  public: visible to all other classes public class BankAccount  private: visible only to the current class, its methods, and every instance (object) of its class  a child class cannot refer to its parent's private members! private String myID;  protected (this one's new to us): visible to the current class, and all of its child classes protected int myWidth;  package (default access; no modifier): visible to all classes in the current "package" (seen later) int myHeight;
  • 21. 21 Access modifier problem (pt.1) public class Parent { private int field1; protected int field2; public int field3; private void method1() {} public void method2() {} protected void setField1(int value) { field1 = value; } }
  • 22. 22 Access modifier problem (pt.2) public class Child extends Parent { public int field4; public Child() { // Which are legal? field4 = 0; // _________ field1++; // _________ field2++; // _________ field3++; // _________ method1(); // _________ method2(); // _________ setField1(field4); // _________ } }
  • 23. 23 Some code that won't compile public class Point2D { private int x, y; public Point2D(int x, int y) { this.x = x; this.y = y; } } public class Point3D extends Point2D { private int z; public Point3D(int x, int y, int z) { this.x = x; this.y = y; // can't do this! this.z = z; } }
  • 24. 24 super keyword  used to refer to superclass (parent) of current class  can be used to refer to parent class's methods, variables, constructors to call them  needed when there is a name conflict with current class  useful when overriding and you want to keep the old behavior but add new behavior to it  syntax: super(args); // call parent’s constructor super.fieldName // access parent’s field super.methodName(args); // or method
  • 25. 25 super example public class BankAccount { private double myBalance; public void withdraw(double amount) { myBalance -= amount; } } public class FeeAccount extends BankAccount { public void withdraw(double amount) { super.withdraw(amount); if (getBalance() < 100.00) withdraw(2.00); // charge $2 fee } }  didn't need to say super.getBalance() because the FeeAccount subclass doesn't override that method (it is unambiguous which version of the method we are talking about)
  • 26. 26 super and constructors  if the superclass has a constructor that requires any arguments (not ()), you must put a constructor in the subclass and have it call the super- constructor (call to super-constructor must be the first statement) public class Point2D { private int x, y; public Point2D(int x, int y) { this.x = x; this.y = y; } } public class Point3D extends Point2D { private int z; public Point3D(int x, int y, int z) { super(x, y); // calls Point2D constructor this.z = z; } }
  • 27. 27 The instanceof keyword  Performs run-time type check on the object referred to by a reference variable  Usage: object-reference instanceof type (result is a boolean expression)  if type is a class, evaluates to true if the variable refers to an object of type or any subclass of it.  if type is an interface, evaluates to true if the variable refers to an object that implements that interface.  if object-reference is null, the result is false.  Example: Object o = myList.get(2); if (o instanceof BankAccount) ((BankAccount)o).deposit(10.0);
  • 28. 28 Some instanceof problems Object o = new BankAccount(...); BankAccount c = new CheckingAccount(...); BankAccount n = new NumberedAccount(...); CheckingAccount c2 = null; T/F ??? o instanceof Object _______ o instanceof BankAccount _______ o instanceof CheckingAccount _______ c instanceof BankAccount _______ c instanceof CheckingAccount _______ n instanceof CheckingAccount _______ n instanceof Comparable _______ n instanceof NumberedAccount _______ c2 instanceof Object _______
  • 29. 29 Which method gets called? BankAccount b = new FeeAccount("Ed", 9.00); b.withdraw(5.00); System.out.println(b.getBalance());  Will it call the withdraw method in BankAccount, leaving Ed with $4?  Will it call the withdraw method in FeeAccount, leaving Ed with $2 (after his $2 fee)?
  • 30. 30 A hint about the right behavior ArrayList list = new ArrayList(); list.add(new BankAccount("Ed", $9.0)); for (int i = 0; i < list.size(); i++) { Object o = list.get(i); System.out.println(o); }  Does it print "Object@FED87C" or "Ed $9.00"?
  • 31. 31 The answer: dynamic binding  The version of withdraw from FeeAccount will be called  The version of an object's method that gets executed is always determined by that object's type, not by the type of the variable  The variable should only be looked at to determine whether the code would compile; after that, all behavior is determined by the object itself
  • 32. 32 Static and Dynamic Binding  static binding: methods and types that are hard-wired at compile time  static methods  referring to instance variables  the types of the reference variables you declare  dynamic binding: methods and types that are determined and checked as the program is running  non-static (a.k.a virtual) methods that are called  types of objects that your variables refer to
  • 33. 33 Polymorphism  inheritance, like interfaces, provides a way to achieve polymorphism in Java  polymorphism: the ability to use identical syntax on different data types, causing possibly different underlying behavior to execute  example: If we have a variable of type BankAccount and call withdraw on it, it might execute the version that charges a fee, or the version from the checking account that tallies interest, or the regular version, depending on the type of the actual object.
  • 34. 34 Type-casting and objects  You cannot call a method on a reference unless the reference's type has that method Object o = new BankAccount("Ed", 9.00); o.withdraw(5.00); // doesn't compile  You can cast a reference to any subtype of its current type, and this will compile successfully ((BankAccount)o).withdraw(5.00);
  • 35. 35 Down-casting and runtime  It is illegal to cast a reference variable into an unrelated type (example: casting a String variable into a BankAccount)  It is legal to cast a reference to the wrong subtype; this will compile but crash when the program runs  Will crash even if the type you cast it to has the method in question ((String)o).toUpperCase(); // crashes ((FeeAccount)o).withdraw(5.00); // crashes
  • 36. 36 A dynamic binding problem class A { public void method1() { System.out.println(“A1”); } public void method3() { System.out.println(“A3”); } } class B extends A { public void method2() { System.out.println(“B2”); } public void method3() { System.out.println(“B3”); } } A var1 = new B(); Object var2 = new A(); var1.method1(); var1.method2(); var2.method1(); OUTPUT??? ((A)var2).method3();
  • 37. 37 A problem with interfaces public interface Shape2D { int getX(); int getY(); double getArea(); double getPerimeter(); }  Every shape will implement getX and getY the same, but each shape probably implements getArea and getPerimeter differently
  • 38. 38 A bad solution public class Shape implements Shape2D { private int myX, myY; public Shape(int x, int y) { myX = x; myY = y; } public int getX() { return myX; } public int getY() { return myY; } // subclasses should override these, please public double getArea() { return 0; } public double getPerimeter() { return 0; } }  BAD: the Shape class can be instantiated  BAD: a subclass might forget to override the methods
  • 39. 39 Abstract classes  abstract class: a hybrid between an interface and a class  used to define a generic parent type that can contain method declarations (like an interface) and/or method bodies (like a class)  like interfaces, abstract classes that cannot be instantiated (cannot use new to create any objects of their type) What goes in an abstract class?  implement common state and behavior that will be inherited by subclasses (parent class role)  declare generic behaviors that subclasses must implement (interface role)
  • 40. 40 Abstract class syntax  put abstract keyword on class header and on any generic (abstract) methods  A class can be declared abstract even though it has no abstract methods  Any class with abstract methods must be declared abstract, or it will not compile  Variables of abstract types may be declared, but objects of abstract types cannot be constructed
  • 41. 41 Abstract class example public abstract class Shape implements Shape2D { private int myX, myY; public Shape(int x, int y) { myX = x; myY = y; } public int getX() { return myX; } public int getY() { return myY; } public abstract double getArea(); public abstract double getPerimeter(); }  Shape class cannot be instantiated  all classes that extend Shape must implement getArea and getPerimeter or else must also be declared abstract
  • 42. 42 Extending an abstract class public class Rectangle extends Shape { private int myWidth, myHeight; public Rectangle(int x, int y, int w, int h) { super(x, y); myWidth = w; myHeight = h; } public double getArea() { return myWidth * myHeight; } public double getPerimeter() { return 2*myWidth + 2*myHeight; } } // ... example usage ... Shape rect = new Rectangle(1, 2, 10, 5);
  • 43. 43 Interface / abstract class chart
  • 44. 44 Questions, Practice problem  What are the differences between interfaces and abstract classes? Why is it useful that both exist in the Java language? When should one use an abstract class and when an interface?  Modify our old IntArrayList and IntLinkedList so that they have a common abstract base class AbstractIntList that contains the common functionality between them.
  • 45. 45 Inner classes  inner class: a class defined inside of another class  can be created as static or non-static  we will focus on standard non-static inner classes  usefulness:  inner classes can be "hidden" from other classes (encapsulation at class level)  useful when implementing data structures (e.g. linked lists) or for GUI programming
  • 46. 46 Inner class behavior  provide syntactic nesting of classes, plus an association to the particular object of the outer "enclosing" class that created them  each object I of the inner class is implicitly bound to the object O (of the outer class) that created it  can refer to it as OuterClassName.this if necessary  an object of an inner class can access the fields and methods of the outer object that created it (including private fields and methods)
  • 47. 47 Inner class example public class MyArrayList { private Object[] items; public Iterator iterator() { return new MyIterator(); } private class MyIterator implements Iterator { int index = 0; public boolean hasNext() { return currentItem < size(); } public Object next() { if (!hasNext()) throw new NoSuchElementException(); else return items[index++]; } } }
  • 48. 48 References  Koffman/Wolfgang Ch. 3, pp. 125-149, 155- 159; Appendix B.1, pp. 738-744  The Java Tutorial: Implementing Nested Classes. http://java.sun.com/docs/books/tutorial/java/ javaOO/nested.html  The Java Tutorial: Inheritance. http://java.sun.com/docs/books/tutorial/java/ concepts/inheritance.html

Editor's Notes

  • #14: note how it does the same as the super but adds more the place they have done it before is when they&amp;apos;ve written toString
  • #20: draw one for the various accounts for Java lists?
  • #24: calling super lets the point3d set the super class&amp;apos;s fields
  • #27: calling super lets the point3d set the super class&amp;apos;s fields
  • #37: ** add other examples **
  • #43: let&amp;apos;s implement right triangle, circle together