Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Evan Korth New York University abstract classes and casting Professor Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 12 Advanced Inheritance
Topic 4 Inheritance.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
CS 5JA Introduction to Java Pop Quiz Define/Explain encapsulation. In object-oriented programming, encapsulation refers to the grouping of data and the.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
abstract classes and casting objects
Object-Oriented Programming: Polymorphism
Extending Classes.
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism, Abstract Classes & Interfaces
Java Programming, Second Edition
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Presentation transcript:

Advanced Inheritance Concepts

In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass objects Using the Object class and its methods Using inheritance to achieve good software design.

Creating and Using Abstract Classes Abstract class –A class from which you cannot create any concrete objects, but from which you can inherit –You can only extend abstract classes –Use the keyword abstract –You cannot use the keyword new

Creating and Using Abstract Classes Non-abstract classes from which objects can be instantiated are called concrete classes In other programming languages, such as C++, abstract classes are known as virtual classes

Abstract Methods Abstract method –A method with no method statements To create an abstract method, you provide –the keyword abstract –the intended method type, name, and arguments –but you do not provide any statements within the method You must code a subclass method to override any inherited abstract superclass method

Using Dynamic Method Binding When you create a superclass and one or more subclasses, each object of the subclass “is a” superclass object –Because every subclass “is a” superclass member, you can convert subclass objects to superclass objects You can create a reference to a superclass –But you do not use the keyword new –You create a variable name to hold the memory address of a subclass concrete object Dynamic method binding –The program’s ability to select the correct subclass method –Is also called late binding

Creating Arrays of Subclass Objects You might want to create a superclass reference and treat subclass objects as superclass objects so you can create an array of different objects that share the same ancestry –Manipulate an array of subclass objects by invoking the appropriate method for each subclass –Elements in a single array must be of the same type –You can then cast the objects in the array back to appropriate subclass. e.g if (shapes[count] instanceof Wheel) ((Wheel) shapes[count]).setSpokes(5);

Using the Object Class and Its Methods Every class in Java is a subclass except for the Object class –The Object class is defined in the java.lang packag java.lang is automatically imported every time you write a program –The Object class includes methods that you can override toString Method –If you do not create a toString() method for a class, then you can use the superclass version of the toString() method –Can be useful for debugging equals() method –Takes a single argument, which must be the same type as the type of the invoking method –Returns a Boolean value

Using Inheritance to Achieve Good Software Design Extended superclass advantages –Subclass creators save development time –Subclass creators save testing time –Programmers who create or use new subclasses already understand how the superclass woks, so the time it takes to learn the new class features is reduced –When you create a new subclass in Java, neither the superclass source code nor the superclass bytecode is changed; the superclass maintains its integrity When you create a number of classes that inherit from each other, you will often find it convenient to place these classes in a package

Design Hints for Inheritance Place common operations and fields in the superclass Don’t overuse protected fields. –Malcious subclasses can access these fields. Use inheritance to model the “is-a” relationship. Don’t use inheritance unless all nherited methods make sense. Use polymorphism, not type information. –if (x is of type 1) action else if (x is of type 2) … think polymorphism. Don’t overuse reflection. –reflection is used for tools, not applications.