Inheritance allows the derivation of a new class

  • Slides: 24
Download presentation
Inheritance allows the derivation of a new class from an existing one, for the

Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. n n 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.

Example: Book. java class Book { protected int pages = 1500; public void page.

Example: Book. java class Book { protected int pages = 1500; public void page. Message() { System. out. println("Number of pages: " + pages); } }

Example: Dictionary. java class Dictionary extends Book { private int definitions = 52500; public

Example: Dictionary. java class Dictionary extends Book { private int definitions = 52500; public void definition. Message() { System. out. println("Number of definitions: " + definitions); System. out. println("Definitions per page: " + definitions/pages); } }

Example: Words. java class Words { public static void main (String[] args) { Dictionary

Example: Words. java class Words { public static void main (String[] args) { Dictionary webster = new Dictionary(); webster. page. Message(); webster. definition. Message(); } } Output: C: Examples>java Words Number of pages: 1500 Number of definitions: 52500 Definitions per page: 35

Extending Classes Protected visibility Super constructor Overriding Super reference Single vs. multiple inheritance n

Extending Classes Protected visibility Super constructor Overriding Super reference Single vs. multiple inheritance n A class may have only one superclass

Example: Book 2. java class Book 2 { protected int pages; public Book 2(int

Example: Book 2. java class Book 2 { protected int pages; public Book 2(int pages) { this. pages = pages; } public void page. Message() { System. out. println("Number of pages: " + pages); } }

Example: Dictionary 2. java class Dictionary 2 extends Book 2 { private int definitions;

Example: Dictionary 2. java class Dictionary 2 extends Book 2 { private int definitions; public Dictionary 2(int pages, int definitions) { super (pages); this. definitions = definitions; } public void definition. Message () { System. out. println("Number of definitions: " + definitions); System. out. println("Definitions per page: " + definitions/pages); } }

Example: Words 2. java class Words 2 { public static void main (String[] args)

Example: Words 2. java class Words 2 { public static void main (String[] args) { Dictionary 2 webster = new Dictionary 2(1500, 52500); webster. page. Message(); webster. definition. Message(); } } Output: C: Examples>java Words 2 Number of pages: 1500 Number of definitions: 52500 Definitions per page: 35

Example: Book 3. java class Book 3 { protected String title; protected int pages;

Example: Book 3. java class Book 3 { protected String title; protected int pages; public Book 3(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); } }

Example: Dictionary 3 a. java class Dictionary 3 a extends Book 3 { private

Example: Dictionary 3 a. java class Dictionary 3 a extends Book 3 { private int definitions; public Dictionary 3 a(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); } }

Example: Dictionary 3 b. java class Dictionary 3 b extends Book 3 { private

Example: Dictionary 3 b. java class Dictionary 3 b extends Book 3 { private int definitions; public Dictionary 3 b(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); } }

Example: Books. java class Books { public static void main (String[] args) { Book

Example: Books. java class Books { public static void main (String[] args) { Book 3 java = new Book 3("Introduction to Java", 350); java. info(); System. out. println(); Dictionary 3 a webster 1 = new Dictionary 3 a("Webster English Dictionary", 1500, 52500); webster 1. info(); System. out. println(); Dictionary 3 b webster 2 = new Dictionary 3 b("Webster English Dictionary", 1500, 52500); webster 2. info(); } }

Example: Books. java Output: C: Examples>java Books Title: Introduction to Java Number of pages:

Example: Books. java Output: C: Examples>java 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

Overloading vs. Overriding Overloading n More than one methods have the same name but

Overloading vs. Overriding Overloading n More than one methods have the same name but different signatures Overriding n n n 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.

The Object Class The root of Java class hierarchy Defines some common methods: n

The Object Class The root of Java class hierarchy Defines some common methods: n n public String to. String() public boolean equals(Object other) If a class does not explicitly declare a superclass, its superclass is Object.

Abstract Class An abstract class is a class with partial implementation. It implements behaviors

Abstract 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

Interfaces are classes with no implementation. n n Interfaces represent pure design. Abstract classes

Interfaces are classes with no implementation. n n 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

Inheritance on Interface Inheritance relation also applies to interfaces n superinterface and subinterface Effects

Inheritance on Interface Inheritance relation also applies to interfaces n superinterface and subinterface Effects of Multiple inheritance effected by n n An interface may extend multiple interfaces A class my implement multiple interfaces

Type Conversion Implicit Conversion Numeric variables: Any numeric types can be converted to another

Type Conversion Implicit Conversion Numeric variables: Any numeric types can be converted to another numeric type with larger range, e. g. char ==> int, int ==> long, int ==> float, float ==> double. Object reference: An object reference of class C can be converted to a reference of a superclass of C.

Type Conversion Explicit Conversion (Cast) Numeric variables: Any numeric types can be explicitly cast

Type Conversion Explicit 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: n n syntactically allowed; but runtime checked.

Cast Object References class Student {. . . } class Undergraduate extends Student {.

Cast Object References class Student {. . . } class Undergraduate extends Student {. . . } class Graduate extends Student {. . . } Student student 1, student 2; student 1 = new Undergraduate(); // ok student 2 = new Graduate(); // ok Graduate student 3; student 3 = student 2; // compilation error student 3 = (Graduate) student 2; // explicit cast, ok student 3 = (Graduate) student 1; // compilation ok, run-time error

Polymorphic Reference A polymorphic reference is one which can refer to different types of

Polymorphic Reference A polymorphic reference is one which can refer to different types of objects.

Example: Books 2. java class Books 2 { public static void main (String[] args)

Example: Books 2. java class Books 2 { public static void main (String[] args) { Book 3[] books = { new Book 3("Introduction to Java", 350), new Dictionary 3 a("Webster English Dictionary", 1500, 52500), new Dictionary 3 b("Webster English Dictionary", 1500, 52500)}; for (int i = 0; i < books. length; i++) { Book 3 book = books[i]; book. info(); System. out. println(); } } }

Example: Books 2. java Output: C: Examples>java Books 2 Title: Introduction to Java Number

Example: Books 2. java Output: C: Examples>java Books 2 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