SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Object-Oriented Programming: Inheritance Deitel &Deitel Java SE 8.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Design Patterns in Java Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
Chapter 12 – Aggregation, Composition, and Inheritance
Inheritance using Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
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.
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.
Programming Pillars Introduction to Object- Oriented Programming.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
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.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Chapter 12 - Aggregation and Inheritance Composition Aggregation UML Class Diagram for Composition and Aggregation Car Dealership Program Inheritance.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part I
Inheritance and Polymorphism
An Introduction to Inheritance
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction

2 Prior to this chapter, all of our objects have been relatively simple, so we've been able to describe each object with just a single class. This is the natural approach when the attributes are all simple elements.

3 Composition For an object that's more complex (non-trivial attributes), break up the object into its constituent parts and defining one class as the whole and other classes as parts of the whole. When the whole class is the exclusive owner of the parts classes, then that class organization is called a composition. Note the “solid” diamond!

4 A nested composition hierarchy for the human body:

5 Composition semantics In a composition hierarchy, the relationship between a containing class and one of its part classes is known as a has-a relationship. For example, each human body has a brain and has a heart. An automobile has an engine and has wheels. Remember that with a composition relationship, a component part is limited to just one owner at a time. For example, a heart can be in only one body at a time. An engine can only be in one automobile at a time.

6 Composition vs. Aggregation There's another has-a relationship, called aggregation, which is a weaker form of composition. With aggregation, one class is the whole and other classes are parts of the whole (as with composition), but there is no additional constraint that requires parts to be exclusively owned by the whole. An example where the parts (Students) are not exclusively owned by the whole, because the Student can also be part of another aggregation, like a School Club or Athletic Team. Note the “hollow” diamond!

7 UML Class Diagram details for Composition and Aggregation Universal Modeling Language (UML) Class diagrams show the relationships between a program's classes: A solid line between two classes represents an association – a relationship between classes. On an association line, a solid diamond indicates a composition relationship, and a hollow diamond indicates an aggregation relationship. The diamond goes next to the container class. a simple straight line is an unspecified association The labels on the association lines are called multiplicity values. They indicate the number of object instances for each of the two connected classes. The * multiplicity value represents any size number, zero through infinity.

8 UML Class Diagram illustrating “dependency” Suppose the computeInterest() method of BankAccount uses the pow() method of the Math class When a class merely “uses” another class, that relationship is called a dependency and is illustrated with a dotted line A dotted line between two classes represents an dependency A dependency line contains an arrow which points to the class (Math) that the other class (BankAccount) depends upon. It is not always necessary to illustrate dependencies

9 Classes with family ties Sometimes we find that two (or more) classes could be related to each other in the sense that They are not the exactly the same, but they are not completely different either One of the classes is a “special case” of the other, and needs to behave a little differently Examples: Class Automobile is a special case of class Vehicle Class Beagle is a special case of Class Dog This type of class relationship is called Inheritance SE-1020 Dr. Mark L. Hornick

10 Inheritance Example - People in a Department Store Here's a UML class diagram for an inheritance hierarchy that keeps track of people in a department store: The Person class is generic - it contains data and methods that are common to all classes in the hierarchy. As you go down the hierarchy, the classes get more specific. For example, the Customer and Employee classes describe specific types of people in the store.

11 Inheritance Terminology Within an inheritance hierarchy, pairs of classes are linked together. For each pair of linked classes, the more generic class is called the superclass and the more specific class is called the subclass. We say that subclasses are derived from superclasses. That makes sense when you realize that subclasses inherit all of the superclass's data and methods. Unfortunately, the terms superclass and subclass can be misleading. The "super" in superclass could imply that superclasses have more capability and the "sub" in subclass could imply that subclasses have less capability. Actually, it's the other way around - subclasses have more capability. Subclasses can do everything that superclasses can do, plus more. In fact, a subclass must be capable of doing everything a superclass does, and should never violate the Liskov Substitution Principle We'll stick with the terms superclass and subclass since those are the formal terms used by Sun, but be aware of this alternative terminology: Programmers often use the terms parent class or base class when referring to a superclass. Programmers also use the terms child class or derived class when referring to a subclass.

12 The Java mechanism for defining a inheritance relationship between two classes is called extension: In Dog.java: public class Dog{…} In Beagle.java: public class Beagle extends Dog{…} The extends keyword establishes the inheritance relationship extends means “is a kind of” SE-1020 Dr. Mark L. Hornick

13 Usually, UML class diagrams show superclasses above subclasses. That's a common practice, but not a requirement. The following is a requirement…. UML class diagrams use an arrow for inheritance relationships, with a hollow arrowhead pointing to the superclass. Warning: The direction of arrows in UML class diagrams is opposite to the direction in which inheritance flows. In the diagram the Beagle class inherits the name variable from the Dog class. And yet the arrow does not go from Dog to Beagle ; it goes from Beagle to Dog. That's because the arrow points to the superclass, and Dog is the superclass. UML class diagram review: What are the class boxes' minus signs for? What are the class boxes' third compartments for?

14 Benefits of Inheritance It helps with code reusability - A superclass's code can be used for multiple subclasses. That eliminates code redundancy and makes debugging and upgrading easier. A programmer can use an existing class to easily create a new subclass (no need to "reinvent the wheel.") Smaller modules (because classes are split into superclasses and subclasses) - That makes debugging and upgrading easier. 1 2

SE-1020 Dr. Mark L. Hornick 15 Rules of inheritance All attributes and methods defined in a superclass are inherited by all subclasses Except for constructors!!! But: Only protected and public attributes and methods defined in the superclass are accessible in the subclasses meaning only those superclass attributes are visible to the subclasses And only those superclass methods are callable by the subclasses Package attributes and methods are accessible only if the subclass is in the same package as the superclass methodA() can only access these attributes and methods of the superclass Everything except the private members of the superclass is visible from a method of the subclass Private methods and attributes defined in a superclass are only accessible within the superclass itself

SE-1020 Dr. Mark L. Hornick 16 Visibility from other classes SomeOtherClass’s doSomething() method can only access the public attributes and methods of Superclass: publicAttr publicMethod() and methodA() of Subclass Package attributes and methods are accessible if SomeOtherClass is in the same package as Superclass and Subclass

SE-1020 Dr. Mark L. Hornick 17 Inheritance and Constructors Unlike attributes and regular methods of a superclass, constructors of a superclass are not inherited by its subclasses You must define a constructor for a subclass or let the compiler add a default subclass constructor

SE-1020 Dr. Mark L. Hornick 18 There are many cases in which a subclass may want to utilize the behavior implemented in a superclass constructor So that the subclass does not have to completely reimplement the same behavior …which, remember, is not inherited For example, if the Dog class has the following constructor: public void Dog(String name, int age) { // Dog initialization goes here } Within the Beagle class: public void Beagle(String name, int age) { super(name, age); // invoke Dog ctor // other statements can go here, but the FIRST statement // must be the invocation of the superclass ctor }

Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick 19 If a superclass has only a default constructor public void BaseClass(){ // initialization code here } Within the derived class: public void DerivedClass() { super(); // invoke BaseClass default ctor }

Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick 20 Actually, the invocation of a superclass’s default constructor is performed automatically when… 1. A subclass does not explicitly invoke it’s superclass constructor 2. If the subclass does not contain a constructor method at all

Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick 21 A subclass constructor must invoke a superclass constructor whenever: 1. The superclass does not contain a default constructor 2. Private inherited attributes of the superclass need to be initialized

A subclass method can redefine a superclass method This is called overriding a method Do not confuse with method overloading (which is where a class implements multiple methods having the same name but different parameters) A subclass overrides a superclass method to provide a specialized behavior SE-1020 Dr. Mark L. Hornick 22 Beagle provides it’s own custom implementation of speak()

23 Method overriding is when a subclass has a method with the same name and the same parameter types as a method in its superclass. If a subclass contains an overriding method: By default, an object of the subclass will use the subclass's overriding method (and not the superclass's overridden method). Sometimes, an object of the subclass may need to call the superclass's overridden method. To do that, preface the method call with "super." (don't forget the dot). If a subclass and a superclass have methods with the same name, same parameter types, and different return types, that generates a compilation error.

Methods modified with the final keyword cannot be overridden We declare a method final if we want to prevent subclasses from changing the behavior of the method via an override. Similarly, classes modified with the final keyword cannot be extended. We declare a class to be final if we want to prevent subclasses from extending that class. Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 24