1 / 24

Inheritance

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.

marciagreen
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

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

  3. 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); } }

  4. 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

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

  6. 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); } }

  7. 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); } }

  8. 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

  9. 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); } }

  10. 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); } }

  11. 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); } }

  12. 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(); } }

  13. 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

  14. 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.

  15. 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.

  16. 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

  17. 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

  18. 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

  19. 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.

  20. 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.

  21. 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

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

  23. 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(); } } }

  24. 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

More Related