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.

Slides:



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

CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 6 Class Hierarchies, Inheritance, and Interfaces  This is a very good chapter for us at this point in time.  We have been programming hard this.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
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.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
OOP Languages: Java vs C++
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
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.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
 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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Inheritance and Access Control CS 162 (Summer 2009)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Polymorphism in Methods
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Inheritance Chapter 7 Chapter 7.
Java Programming Language
Advanced Java Topics Chapter 9
Java Inheritance.
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

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 of the superclass? Answer: for data members of the superclass, also. NOTE: They can be accessed directly by the subclass only if they have the scope modifier protected in the super class. 2. DEF: protected is the scope modifier that permits access to a data field in a superclass by a subclass but no other class.

3. Can the same identifier be used for a local variable in a method, a data member in a subclass, and a data member in a superclass? Answer: yes a. Which will be accessed from within the method? b. How can the subclass data member be accessed then from within that method? Answer: the local variable c. How can the superclass data member be accessed? Answer: super.identifier Answer: this.identifier

4. Can an object of a subclass be referenced by a variable declared to be a reference to the superclass? Answer: yes NOTE: when this happens only methods with names in the superclass identical to those in the subclass can be used BUT it will be the subclass method that is executed!! Casting can be used to improve this. See ex. 6.4 p.384

5. Can an object of a superclass be referenced by a variable that was declared to be a reference to the subclass? Answer: NO (see ex. 6.2 p.385) Casting can be used to do this in some cases: see p Can a subclass variable reference an object of another subclass of the same superclass on the same level? Answer: NO

7. What does it mean when we say primitives are passed to parameters in method headings by value? Answer: a copy of the value in the argument (that is in the method call) is made in the parameter (that’s in the method heading). Therefore, any changes made to the parameter do not change the argument (the original data).

8. Since objects are always passed by reference, that is, the parameter receives the address of the object only, changes to the parameter do change the original object. 9. RULE: An argument of a subclass type can be passed to a parameter which is its superclass, BUT not the other way around (that is the converse is false). CAUTION: It can appear that this is not the case when methods in subclasses and superclasses have the same name.

Examples: 1. equals() 2. toString() see p.718, 720, 722 for classes: Object & String Fig p.386 shows an equals() method defined in the class NewEmployee, a subclass of the class Object, receiving an instance of the class Object as a parameter! (emp) This will only work if emp had previously received the address of an object of the NewEmployee class. (see ex. P.381)

In Fig 6.16 checking with the operator instanceOf and type casting, allows this method to work, thereby, overriding the equals() method in class Object. A call such as clerk.equals(obj) is not breaking the above rule. It is calling the equals() method in class Object (the superclass), which can be done by any subclass. Why is clerk not calling the equals() method in the subclass? (answer: different argument!)

10. Review: Arrays must contain only 1 type component. EXCEPTION: An array of objects may contain subclass objects also, since a superclass variable can reference objects of its own type OR objects of its subclass types. Ex: employees [ i ]. toString() See NewCompany class p. 390.

NOTE: With a method such as toString( ) whose name is the same in different classes, Java cannot determine at compile time which toString( ) mthod to call. It is not known at that time whether employee[i] references an object of the superclass or one of its subclasses. However, at run-time, the Java interpreter determines this based on the address then stored in employee[i].

11. Def: dynamic binding - the programming language feature that permits selection of a code fragment at execution time. NOTE: This is another example of polymorphism. What are other? method overloading method overriding

12. Def: abstract class - a class that is never instantiated. It has the purpose of being a superclass (parent) of a group of classes. It is a place to store data fields and methods common to all subclasses. NOTE: an abstract class must contain at least 1 abstract method (its header only) which must be implemented by each subclass. See p abstract class: GeoFigure hierarchy top p abstract class code p test class showing array containing different subclasses.

13. Does Java support multiple inheritance? No. (a class can extend only one superclass.) However, Java provides a mechanism called an interface. An interface is similar to a class but the only members of an interface are abstract methods and final class data fields. (See p.766 ActionListener) A class implements an interface (not extend it). Such classes are guaranteed to have certain functionality (behavior) and constants. Often used in GUI and graphic programs

14. What is a GUI? A Graphical User Interface is a collection of instances (objects) of certain classes called components. Examples of components are: buttons, check boxes, text fields and labels. Each is able to access data fields and methods defined somewhere in a hierarchy. Each handles an action performed by a user called an event which is usually a click of a mouse.

Note: packages that include GUI’s are AWT (Abstract Window Toolkit) and Swing (which an improved alternate to AWT). See example DistanceGUI p.448 and method actionPerformed() p On website “boxProjects” see boxApplet.java

15. Java provides a collection of graphics methods that enable you to draw pictures on a drawing surface (computer screen). The class Applet has an associated drawing surface accessed through its paint() method. This method requires one argument which is an object of the Graphics class defined in the AWT package. Therefore, we must import both packages. See ex. Intersection p.159, House p.162 drawBox (website).