240 likes | 251 Vues
Learn about inheritance in object-oriented programming, the concepts of superclass and subclass, and how to extend classes to create specialized functionality. Explore examples and understand the difference between overloading and overriding.
E N D
Inheritance • 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-arelationship. • 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 pageMessage() { System.out.println("Number of pages: " + pages); } }
Example: Dictionary.java class Dictionary extends Book { private int definitions = 52500; public void definitionMessage() { 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 webster = new Dictionary(); webster.pageMessage(); webster.definitionMessage(); } } 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 • A class may have only one superclass
Example: Book2.java class Book2 { protected int pages; public Book2(int pages) { this.pages = pages; } public void pageMessage() { System.out.println("Number of pages: " + pages); } }
Example: 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); } }
Example: Words2.java class Words2 { public static void main (String[] args) { Dictionary2 webster = new Dictionary2(1500, 52500); webster.pageMessage(); webster.definitionMessage(); } } Output: C:\Examples>java Words2 Number of pages: 1500 Number of definitions: 52500 Definitions per page: 35
Example: 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); } }
Example: 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); } }
Example: 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); } }
Example: 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(); } }
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 • 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.
The 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.
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
Interface • 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
Inheritance 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
Type ConversionImplicit 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 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.
Cast 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
Polymorphic Reference • A polymorphic reference is one which can refer to different types of objects.
Example: 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("Webster English Dictionary", 1500, 52500)}; for (int i = 0; i < books.length; i++) { Book3 book = books[i]; book.info(); System.out.println(); } } }
Example: Books2.java Output: C:\Examples>java 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