1 Chapter 7 Inheritance, Polymorphism, and Scope.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
Chapter 8 Object-Oriented Software Engineering. 2 Knowledge Goals Understand the concepts of encapsulation and abstraction Know how control and data abstraction.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA BASICS Prepared by The Smartpath Information Systems
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Inheritance and Polymorphism
Packages, Interfaces & Exception Handling
Computer Science II Exam 1 Review.
CSC 205 Java Programming II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Advanced Programming Behnam Hatami Fall 2017.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

1 Chapter 7 Inheritance, Polymorphism, and Scope

2 Chapter 7 Topics l Inheritance l Inheritance and the Object-Oriented Design Process l How to Read a Class Hierarchy l Derived Class Syntax l Scope of Access l Implementing a Derived Class l Copy Constructors l Output and Input of Objects

3 Inheritance l Inheritance A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class. l Superclass The class being inherited from l Derived class The class that inherits l The derived class is specialized by adding properties specific to it

4 Inheritance Hierarchy Example Vehicle WheeledVehicleBoat BicycleCar FourDoor Every Car “is a” WheeledVehicle. TwoDoor

5 Address object hierarchy Object CompanyAddressHomeAddress BoxAddressWorkAddress Address

6 Blank CRC Card Class Name: Superclass: Subclasses: CompanyAddressAddressBoxAddress ResponsibilitiesCollaborations

7 Hierarchy of Component Classes Component Container Object abstract classes JComponent JTextComponen t JTextField

8 More Definitions l Override When an instance method in a derived class has the same form of heading as an instance method in its superclass, the method in the derived class overrides (redefines) the method in the superclass l Hide When a field in a derived class has the same name as one in its superclass or a class method has the same form of heading as a class method in its superclass, the field or class hide the corresponding component in the superclass Say again?

9 An example public class Example { char letter; public static String lineIs(); … } public class ExtExample extends Example { char letter; public static String lineIs(); … } Hiding or overriding?

10 Another Example public class Example { char letter; public String lineIs(); … } public class ExtExample extends Example { String letter; public String lineIs(); … } Hiding or overriding?

11 Overriding vs. Hiding l We override an instance method of a superclass by providing an instance method in a derived class with the same form of heading l We hide a data field of a superclass by providing a field in a derived class with the same name

12 Polymorphism l Polymorphism is the ability of a language to have duplicate method names in an inheritance hierarchy and to decide which method is appropriate to call depending on the class of the object to which the method is applied.

13 OOP Characteristics Characteristic Meaning Benefit New classes created from existing classes Software resusabiltiy Inheritance Write programs to handle variety of existing and future related classes Add new capabilities Polymorphism

14 Class Syntax Derived Class Syntax ClassModifier class Identifier extends ClassName { ClassDeclaration... }

15 Scope of Identifier l The scope of an identifier is the portion of program code where it is legal to use that identifier for any purpose l That is, who knows what, where, and when

16 Shadowing l Shadowing A scope rule specifying that a local identifier declaration blocks access to an identifier declared with the same name outside the block containing the local declaration A shadowed class member can be accessed by using keyword this together with the class member

17 Four Levels of Class Member Access public protected ( no access modifier ) “package access” private public private protected package

18 Member Accessibility External access public protected (default) private package Same package yes yes yes no Derived class in yes yes no no another package (inheritance only) User code yes no no no

19 Subclass Constructor A subclass constructor always calls the superclass constructor first (either as a first statement call to super or automatically when there is no call to super ) to create and initialize members inherited from the superclass.

20 Method Signature l The signature of a method consists of the method’s name, together with the number and type(s) of its parameters in their given order l Method overloading is the use of a method name more than once, each time with different signatures

21 Method Overloading Means l Several methods of the same name are defined with different sets of parameters (based on number of parameters, types of parameters, or order of parameters)

22 Addresses in Memory number ch l When a variable of primitive type is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. The value of the variable is stored at this location double number; char ch; ch = ‘X’; ‘X’

23 Reference Types: A Review title book String title ; String book ; title = “Problem Solving”; book = title ; 2003 “Problem Solving” Memory Location l A variable of reference type stores the address of the location where the object can be found.

24 Class SavingsAccount getNumber getBalance. withdraw getName deposit SavingsAccount Private data: number name 2003 balance activation 3000 “Nell Dale” month day year 13 “June”

25 Shallow Copy. SavingsAccount Private data: number name 2003 balance activation SavingsAccount Private data: number name 2003 balance activation 3000 “Nell Dale” month day year 13 “June” shallow copy

26 Shallow Copy vs. Deep Copy l Shallow copy All class data fields, including references are copied; any objects referred to by data fields are not copied l Deep copy All class data fields are copied, and all objects referred to are copied

27 What’s the difference? l A shallow copy shares nested objects with the original class object l A deep copy makes its own copy of nested objects at different locations than in the original class object

28 Separate deep copy deep copy. SavingsAccount Private data: number name 2003 balance activation 3000 “Nell Dale” month day year 13 “June” SavingsAccount Private data: number name 6000 balance activation 4000 “Nell Dale” month day year 13 “June”

29 Copy Constructor A copy constructor is a constructor that creates a deep copy of an object that can be used for other purposes, such as creating a new instance of an immutable object from an old one public SavingsAccount(SavingsAccount oldAcct, String changeOfAddress) {... // create deep copy of oldAcct } // call account = new Savings Account(oldAcct, newAddress);

30 Output and Input of Objects l Output and Input? Don’t you mean Input and Output? No! l An object can be written out as a stream of bytes l The stream of bytes can be read back in and type cast back into the original object

31 O/I Definitions l Serializing Translating an object into a stream of bytes l Deserializing Translating a serialized stream of bytes back into the original object How? You don’t need to know; Java does it for you when The class implements Serializable

32 How to Write Objects public class Name implements Serializable {...}... public class ObjectFileWrite { private static ObjectOutputStream outObject; public static void main(String[] args) throws IOException { Name person;... // Define the field of person outObject = new ObjectOutputStream(new FileOutputStream("outObject.dat")); outObject.writeObject(person); outObject.close(); }

33 How to Read Objects import Name; public class ObjectFileRead { private static ObjectInputStream inObject; //Input file public static void main(String[] args) throws IOException, ClassNotFoundException { Name person; inObject = new ObjectInputStream(new FileInputStream("outObject.dat")); person = (Name)inObject.readObject();... inObject.close();} }