Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 10: Introduction to Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Advanced Object-Oriented Programming Features
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
© 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.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
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
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented Programming in Java
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Java Programming Language
Polymorphism and access control
Advanced Java Topics Chapter 9
Polymorphism, Abstract Classes & Interfaces
Java Programming, Second Edition
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Advanced Inheritance Concepts
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Chapter 11: Advanced Inheritance Concepts

Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use the Object class and its methods Use inheritance to achieve good software design Create and use interfaces Create and use packages 2Java Programming, Seventh Edition

Creating and Using Abstract Classes Abstract class – Cannot create any concrete objects – Can inherit – Usually has one or more empty abstract methods When declaring an abstract class: – Use the keyword abstract – Provide the superclass from which other objects can inherit – Example: public abstract class Animal 3Java Programming, Seventh Edition

Creating and Using Abstract Classes (cont’d.) An abstract method does not have: – A body – Curly braces – Method statements To create an abstract method: – Use the keyword abstract – The header must include the method type, name, and parameters – Include a semicolon at the end of the declaration – Example: public abstract void speak(); 4Java Programming, Seventh Edition

Creating and Using Abstract Classes (cont’d.) Subclass of abstract class – Inherits the abstract method from its parent Must provide the implementation for the inherited method or be abstract itself – Code a subclass method to override the empty superclass method 5Java Programming, Seventh Edition

Using Dynamic Method Binding Every subclass object “is a” superclass member – Convert subclass objects to superclass objects – Can create a reference to a superclass object Create a variable name to hold the memory address Store a concrete subclass object Example: Animal animalRef; animalRef = new Cow(); 6Java Programming, Seventh Edition

Using Dynamic Method Binding (cont’d.) Dynamic method binding – Also called late method binding – An application’s ability to select the correct subclass method – Makes programs flexible When an application executes, the correct method is attached (or bound) to the application based on current and changing (dynamic) context 7Java Programming, Seventh Edition

Using Dynamic Method Binding (cont’d.) 8 Figure 11-8 The AnimalReference application Java Programming, Seventh Edition

Using a Superclass as a Method Parameter Type Useful when you want to create a method that has one or more parameters that might be one of several types Use dynamic method binding public static void talkingAnimal (Animal animal) Dog dog = new Dog(); talkingAnimal(dog); 9Java Programming, Seventh Edition

Using a Superclass as a Method Parameter Type (cont’d.) 10 Figure The TalkingAnimalDemo class Java Programming, Seventh Edition

Using a Superclass as a Method Parameter Type (cont’d.) 11 Figure Output of the TalkingAnimalDemo application Java Programming, Seventh Edition

Creating Arrays of Subclass Objects Create a superclass reference – Treat subclass objects as superclass objects Create an array of different objects that share the same ancestry Create an array of three Animal references Animal[] animalRef = new Animal[3]; – Reserve memory for three Animal object references 12Java Programming, Seventh Edition

Using the Object Class and Its Methods Object class – Every Java class is an extension of the Object class – Defined in the java.lang package – Imported automatically – Includes methods to use or override 13Java Programming, Seventh Edition

14Java Programming, Seventh Edition

Using the toString() Method toString() method – Converts an Object into a String – Contains information about the Object – Output : Class sign Hash code 15Java Programming, Seventh Edition

Using the toString() Method (cont’d.) Write an overloaded version of the toString() method – Display some or all data field values for an object – Can be very useful in debugging a program Display the toString() value Examine its contents 16Java Programming, Seventh Edition

Using the toString() Method (cont’d.) 17 Figure The TestBankAccount application Java Programming, Seventh Edition

Using the equals() Method equals() method – Takes a single argument The same type as the invoking object – Returns a boolean value Indicates whether objects are equal – Considers two objects of the same class to be equal only if they have the same hash code 18Java Programming, Seventh Edition

Using the equals() Method (cont’d.) Example of the equals() method: if(someObject.equals (someOtherObjectOfTheSameType)) System.out.println("The objects are equal"); To consider objects to be equal based on contents, you must write your own equals() method 19Java Programming, Seventh Edition

Using the equals() Method (cont’d.) Object method hashCode() – Returns an integer representing the hash code – Whenever you override the equals() method: You should override the hashCode() method as well Equal objects should have equal hash codes 20Java Programming, Seventh Edition

21 Figure The BankAccount class containing its own equals() method Java Programming, Seventh Edition

Using Inheritance to Achieve Good Software Design You can create powerful computer programs more easily if components are used either “as is” or with slight modifications Make programming large systems more manageable 22Java Programming, Seventh Edition

Using Inheritance to Achieve Good Software Design (cont’d.) Advantages of extendable superclasses – Save development time Much code is already written – Save testing time Superclass code is already tested – Programmers understand how a superclass works – A superclass maintains its integrity The bytecode is not changed 23Java Programming, Seventh Edition

Creating and Using Interfaces Multiple inheritance – Inherit from more than one class – Prohibited in Java – Variables and methods in parent classes might have identical names Creates conflict Which class should super refer to when a child class has multiple parents? 24Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) Interface – An alternative to multiple inheritance – Looks like a class except all of its methods are implicitly public and abstract, and all of its data items are implicitly public, abstract, and final – A description of what a class does – Declares method headers 25Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) 26 Figure The Animal and Dog classes Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) 27 Figure The Worker interface Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) 28 Figure The WorkingDog class Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) Create an interface – Example: public interface Worker Implement an interface – Use the keyword implements Requires the subclass to implement its own version of each method – Use the interface name in the class header Requires class objects to include code public class WorkingDog extends Dog implements Worker 29Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) Abstract classes versus interfaces – You cannot instantiate concrete objects of either – Abstract classes Can contain nonabstract methods Provide data or methods that subclasses can inherit – Subclasses maintain the ability to override inherited methods Can include methods that contain the actual behavior the object performs 30Java Programming, Seventh Edition

Creating and Using Interfaces (cont’d.) Abstract classes versus interfaces (cont’d.) – Interfaces Methods must be abstract Programmers know what actions to include Every implementing class defines the behavior that must occur when the method executes A class can implement behavior from more than one parent 31Java Programming, Seventh Edition

Creating Interfaces to Store Related Constants Interfaces can contain data fields – Data fields must be public, static, and final Interfaces contain constants – Provide a set of data that many classes can use without having to redeclare values 32Java Programming, Seventh Edition

Creating Interfaces to Store Related Constants (cont’d.) 33 Figure The PizzaConstants interface Java Programming, Seventh Edition

Creating and Using Packages Package – A named collection of classes – Easily imports related classes into new programs – Encourages other programmers to reuse software – Helps avoid naming conflicts or collisions – Gives every package a unique name 34Java Programming, Seventh Edition

Creating and Using Packages (cont’d.) Create classes for others to use – Protect your work Do not provide users with source code in files with.java extensions Provide users with compiled files with.class extensions – Include the package statement at the beginning of the class file Place compiled code into the indicated folder 35Java Programming, Seventh Edition

Creating and Using Packages (cont’d.) Compile the file to place in a package – Use a compiler option with the javac command The -d option places the generated.class file in a folder Package-naming convention – Use your Internet domain name in reverse order Collisions – Class naming conflicts 36Java Programming, Seventh Edition

Creating and Using Packages (cont’d.) Java ARchive (JAR) file – A package or class library is delivered to users as a JAR file – Compresses and stores data Reduces the size of archived class files – Based on the Zip file format 37Java Programming, Seventh Edition

You Do It Creating an Abstract Class Extending an Abstract Class Extending an Abstract Class with a Second Subclass Instantiating Objects from Subclasses Using Object References Using an Interface Creating a Package 38Java Programming, Seventh Edition

Don’t Do It Don’t write a body for an abstract method Don’t forget to end an abstract method header with a semicolon Don’t forget to override any abstract methods in any subclasses you derive Don’t mistakenly overload an abstract method instead of overriding it Don’t try to instantiate an abstract class object Don’t forget to override all the methods in an interface that you implement Don’t use the wildcard format to import multiple classes when creating your own packages 39Java Programming, Seventh Edition

Summary Abstract class – A class that you create only to extend from, but not to instantiate from – Usually contains abstract methods Methods with no method statements Can convert subclass objects to superclass objects Dynamic method binding – Create a method that has one or more parameters that might be one of several types – Create an array of superclass object references but store subclass instances in it 40Java Programming, Seventh Edition

Summary (cont’d.) Interface – Similar to a class – All methods are implicitly public and abstract – All of its data fields are implicitly public, static, and final – To create a class that uses an interface, include the keyword implements and the interface name in the class header Place classes in packages – Convention uses Internet domain names in reverse order 41Java Programming, Seventh Edition