ITEC200 – Week03 Inheritance and Class Hierarchies.

Slides:



Advertisements
Similar presentations
OOP: Inheritance By: Lamiaa Said.
Advertisements

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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
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.
ITEC200 Week01 Introduction to Software Design.
ITEC200 Week02 Program Correctness and Efficiency.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
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.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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,
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
© 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 using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Inheritance in the Java programming language J. W. Rider.
Lists and the Collection Interface Review inheritance & collections.
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.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Introduction to Object-Oriented Programming Lesson 2.
Coming up: Inheritance
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Data Structures and Algorithms
Advanced Java Topics Chapter 9
Chapter 12 Abstract Classes and Interfaces
Creating and Using Classes
Presentation transcript:

ITEC200 – Week03 Inheritance and Class Hierarchies

2 Learning Objectives – Week 03 Inheritance and Class Hierarchies (Ch3) Students can: Interpret and design inheritance class hierarchies facilitating code reuse (including the use of interfaces, abstract classes) Explain and use the OO concepts of method overriding, method overloading and polymorphism “clone” an object and can explain the difference between a true clone (deep copy) and a shallow copy Explain how interfaces and delegation can be used to emulate features of multiple inheritance and use these features appropriately Describe the levels of visability that Java supports and designate these levels appropriately in class design Create and use object factories

3 Introduction to Inheritance and Class Hierarchies Example Class Hierarchy: Exception Class Hierarchy

4 A Superclass and a Subclass Consider two classes: Computer and Laptop A laptop is a kind of computer and is therefore a subclass of computer

5 Method Overriding vs Method Overloading If a derived class has a method found within its base class, that method will override the base class’s method The keyword super can be used to gain access to superclass methods overridden by the base class A subclass method must have the same return type as the corresponding superclass method

6 Polymorphism A variable of a superclass type can reference an object of a subclass type Polymorphism means many forms or many shapes Polymorphism allows the JVM to determine which method to invoke at run time At compile time, the Java compiler can’t determine what type of object a superclass may reference but it is known at run time

7 Polymorphism and Casting A variable can reference an object whose type is a subclass of the variable type The type of reference, not the type of the object referenced, determines what operations can be performed Java is a strongly typed language so the compiler always verifies that the type of the expression being assigned is compatible with the variable type

8 Java 5.0 Reduces Need for Casting Two new features that reduce the need for casting: –Autoboxing/unboxing –Generics Autoboxing/unboxing eases the conversion between a primitive type and its corresponding wrapper type

9 Abstract Classes An abstract class is a class that has abstract methods (defines signature but not implementation) Features: –Abstract classes can contain data fields, and concrete methods –An abstract class cannot be instantiated –An abstract class can declare abstract methods, which must be implemented in its subclasses –An abstract class can have constructors to initialize its data fields when a new subclass is created Subclass uses super(…) to call the constructor –May implement an interface but it doesn’t have to define all of the methods declared in the interface (Implementation is left to its subclasses)

10 Abstract Class Number and the Java Wrapper Classes

11 Summary of Features of Actual Classes, Abstract Classes, and Interfaces

12 Class Object Object is the root of the class hierarchy; every class has Object as a superclass All classes inherit the methods defined in class Object but may be overridden

13 The Object.clone method Java provides the Object.clone method to help solve the shallow copy problem The initial copy is a shallow copy as the current object’s data fields are copied To make a deep copy, you must create cloned copies of all components by invoking their respective clone methods

14 Using Multiple Interfaces to Emulate Multiple Inheritance Multiple inheritance: the ability to extend more than one class Java does not facilitate multiple inheritance directly However, if we define two interfaces, a class can implement both Multiple interfaces emulate multiple inheritance

15 Implementing Reuse Through Delegation You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation In delegation, a method of one class accomplishes an operation by delegating it to a method of another class

16 Packages Package is a bundle of classes that have been logically grouped together The package to which a class belongs is declared by the first statement in the file in which the class is defined using the keyword package followed by the package name All classes in the same package are stored in the same directory or folder Files that do specify a package are considered part of the ‘default’ package

17 Visibility Visability refers to the access control for classes and their data elements (eg, public, protected, package, private) Public visability (keyword ‘public’) allows all classes/methods/data- fields (ie all members) to access the class / data fields / methods Protected visability (keyword ‘protected’) allows all members of the package and any class that wishes to extend the class to access the class / data fields / methods Package visibility is the default (no keyword) only allows members of the package to access the classes / data fields / methods of the package Private visibility (keyword ‘private’) is for members of a class that should not be accessible to anyone but the class, not even the classes that extend it

18 Visibility Supports Encapsulation (continued)

19 A Shape Class Hierarchy

20 Object Factories An object factory is a method that creates instances of other classes Object factories are useful when: –The necessary parameters are not known or must be derived via computation –The appropriate implementation of an interface or abstract class should be selected as the result of some computation

21 Object Factories (continued)

22 Where to from here… Work through Chapter 3 of the Koffman & Wolfgang Text Conceptual Questions and Practical Exercises Submit all preliminary work Be prompt for your online class

23 Acknowledgements These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 3 PowerPoint presentation by Elliot B. Koffman and Paul A. T. Wolfgang