Title: Inheritance
1Inheritance
- Inheritance allows the derivation of a new class
from an existing one, for the purpose of reuse,
enhancement, adaptation, etc. - superclass (a.k.a. base class)
- subclass (a.k.a. derived class, extended class)
- Inheritance models the is-a relationship.
- If class E is an extended class of class B, then
any object of E can act-as an object of B.
2Example Book.java
class Book protected int pages 1500
public void pageMessage()
System.out.println("Number of pages "
pages)
3Example Dictionary.java
class Dictionary extends Book private int
definitions 52500 public void
definitionMessage() System.out.println("Numb
er of definitions "
definitions) System.out.println("Definitions
per page "
definitions/pages)
4Example Words.java
class Words public static void main
(String args) Dictionary webster new
Dictionary() webster.pageMessage()
webster.definitionMessage()
Output
C\Examplesgtjava Words Number of pages
1500 Number of definitions 52500 Definitions per
page 35
5Extending Classes
- Protected visibility
- Super constructor
- Overriding
- Super reference
- Single vs. multiple inheritance
- A class may have only one superclass
6Example Book2.java
class Book2 protected int pages public
Book2(int pages) this.pages pages
public void pageMessage()
System.out.println("Number of pages "
pages)
7Example Dictionary2.java
class Dictionary2 extends Book2 private int
definitions public Dictionary2(int pages, int
definitions) super (pages)
this.definitions definitions public
void definitionMessage ()
System.out.println("Number of definitions "
definitions)
System.out.println("Definitions per page "
definitions/pages)
8Example Words2.java
class Words2 public static void main
(String args) Dictionary2 webster new
Dictionary2(1500, 52500) webster.pageMessage(
) webster.definitionMessage()
Output
C\Examplesgtjava Words2 Number of pages
1500 Number of definitions 52500 Definitions per
page 35
9Example Book3.java
class Book3 protected String title
protected int pages public Book3(String
title, int pages) this.title title
this.pages pages public void info()
System.out.println("Title " title)
System.out.println("Number of pages " pages)
10Example Dictionary3a.java
class Dictionary3a extends Book3 private int
definitions public Dictionary3a(String title,
int pages, int
definitions) super (title, pages)
this.definitions definitions public
void info() System.out.println("Dictionary
" title) System.out.println("Number of
definitions "
definitions) System.out.println("Definitions
per page "
definitions/pages)
11Example Dictionary3b.java
class Dictionary3b extends Book3 private int
definitions public Dictionary3b(String title,
int pages, int
definitions) super (title, pages)
this.definitions definitions public
void info() super.info()
System.out.println("Number of definitions "
definitions)
System.out.println("Definitions per page "
definitions/pages)
12Example Books.java
class Books public static void main (String
args) Book3 java new Book3("Introduction
to Java", 350) java.info()
System.out.println() Dictionary3a webster1
new Dictionary3a("Webster English
Dictionary", 1500, 52500)
webster1.info() System.out.println()
Dictionary3b webster2 new
Dictionary3b("Webster English Dictionary",
1500, 52500) webster2.info()
13Example Books.java
Output
C\Examplesgtjava Books Title Introduction to
Java Number of pages 350 Dictionary Webster
English Dictionary Number of definitions
52500 Definitions per page 35 Title Webster
English Dictionary Number of pages 1500 Number
of definitions 52500 Definitions per page 35
14Overloading vs. Overriding
- Overloading
- More than one methods have the same name but
different signatures - Overriding
- Replacing the implementation of a methods in the
superclass with one of your own. - You can only override a method with the same
signature. - You can only override instance methods.
15The Object Class
- The root of Java class hierarchy
- Defines some common methods
- public String toString()
- public boolean equals(Object other)
- If a class does not explicitly declare a
superclass, its superclass is Object.
16Abstract Class
- An abstract class is a class with partial
implementation. - It implements behaviors that are common to all
subclasses, but defers to the subclasses to
implement others (abstract methods). - An abstract class cannot be instantiated
- The subclass of an abstract class must override
the abstract methods of the parent, or it too
will be considered abstract
17Interface
- Interfaces are classes with no implementation.
- Interfaces represent pure design.
- Abstract classes represent mixed design and
implementation. - An interface consists of only abstract methods
and constants, i.e., static and final. - All methods and constants are public.
- No static methods.
- An interface cannot be instantiated
18Inheritance on Interface
- Inheritance relation also applies to interfaces
- superinterface and subinterface
- Effects of Multiple inheritance effected by
- An interface may extend multiple interfaces
- A class my implement multiple interfaces
19Type ConversionImplicit Conversion
- Numeric variables Any numeric types can be
converted to another numeric type with larger
range, e.g. - char gt int, int gt long,
- int gt float, float gt double.
- Object reference An object reference of class C
can be converted to a reference of a superclass
of C.
20Type ConversionExplicit Conversion (Cast)
- Numeric variables Any numeric types can be
explicitly cast to any other numeric type. May
lose bits, precision. - Object reference Cast an object reference of a
class to a reference of any other class is - syntactically allowed but
- runtime checked.
21Cast Object References
class Student ... class Undergraduate extends
Student ... class Graduate extends Student
... Student student1, student2 student1 new
Undergraduate() // ok student2 new Graduate()
// ok Graduate student3 student3
student2 // compilation error student3
(Graduate) student2 // explicit cast,
ok student3 (Graduate) student1 //
compilation ok, run-time error
22Polymorphic Reference
- A polymorphic reference is one which can refer to
different types of objects.
23Example Books2.java
class Books2 public static void main
(String args) Book3 books new
Book3("Introduction to Java", 350), new
Dictionary3a("Webster English Dictionary",
1500, 52500), new Dictionary3b("Webst
er English Dictionary", 1500,
52500) for (int i 0 i lt books.length
i) Book3 book booksi
book.info() System.out.println()
24Example Books2.java
Output
C\Examplesgtjava Books2 Title Introduction to
Java Number of pages 350 Dictionary Webster
English Dictionary Number of definitions
52500 Definitions per page 35 Title Webster
English Dictionary Number of pages 1500 Number
of definitions 52500 Definitions per page 35