MSIS 670 Object-Oriented Software Engineering

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
C++ Inheritance Systems Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance Chapter 9.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Programming Languages and Paradigms Object-Oriented Programming.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Object Oriented Programming: Inheritance Chapter 9.
Topics Inheritance introduction
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Object Oriented Programming: Inheritance Chapter 9.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Object Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Java Inheritance.
Advanced Programming in Java
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Inheritance
Chapter 10 Thinking in Objects
Object-Oriented Programming: Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Object Oriented Programming: Inheritance
Advanced Java Programming
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Fundaments of Game Design
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Presentation transcript:

MSIS 670 Object-Oriented Software Engineering Week 5 Object-Oriented Programming (1): Inheritance In this chapter you will learn: How inheritance promotes software reusability. The notions of superclasses and subclasses. To use keyword extends to create a class that inherits attributes and behaviors from another class. To use access modifier protected to give subclass methods access to superclass members. To access superclass members with super. How constructors are used in inheritance hierarchies. The methods of class Object, the direct or indirect superclass of all classes in Java. 11/24/2018 5.1

Object-Oriented Programming Inheritance Software reusability Classes are created from existing ones Polymorphism Enables developers to write programs in general fashion – handle variety of existing and yet-to-be specified classes Helps add new capabilities to a system Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities Subclass extends superclass Subclass More specialized group of objects Behaviors inherited from superclass Can customize Additional behaviors Class hierarchy Direct superclass Inherited explicitly (one level up hierarchy) Indirect superclass Inherited two or more levels up hierarchy Single inheritance Inherits from one superclass Multiple inheritance Inherits from multiple superclasses Java does not support multiple inheritance 11/24/2018

Superclasses and Subclasses “Is a” Relationship Object “is an” object of another class Rectangle “is a” quadrilateral Class Rectangle inherits from class Quadrilateral Form tree-like hierarchical structures Object of one class “is an” object of another class Superclass typically represents larger set of objects than subclasses Example: superclass: Vehicle (includes cars, trucks, boats, bicycles, …) subclass: Car (smaller, more-specific subset of vehicles) Examples of Superclasses-Subclasses 11/24/2018

Constructors in Subclasses Superclass constructor Initializes superclass instance variables of subclass Not inherited by subclass (only variables and methods) Called by subclass Implicitly or explicitly with super reference 11/24/2018 The Java compiler sets the superclass of a class to Object when the class declaration does not explicitly extend a superclass. Java ensures that even if a constructor does not assign a value to an instance variable, the variable is still initialized to its default value (e.g., 0 for primitive numeric types, false for booleans, null for references).

Software Engineering with Inheritance Create class (subclass) from existing one (superclass) Subclass creation does not affect superclass New class inherits attributes and behaviors Software reuse In object-oriented system, classes are often related. “Factor out” common attributes and behaviors and place these in a superclass. Use inheritance to form subclasses without having to repeat common attributes and behaviors. Changes in subclasses does not affect the superclass, and thus, no other subclasses either. Changes in superclass may not affect all the subclasses unless the modification is done in the public interfaces to the superclass. Why do we extend a class? For code reuse; the structure and behavior already implemented by one class may be used to define other classes. If someone else has already written a class that satisfies 85% of your need, you can just extend the class and save time by implementing only the remaining 15% of functionality. Copying and pasting code from one class to another can spread errors across multiple source code files. To avoid duplicating code (and possibly errors), use inheritance, rather than the “copy-and-paste” approach, in situations where you want one class to “absorb” the instance variables and methods of another class. 11/24/2018

Composition vs. Inheritance “Has a” relationship Employee has a TelephoneNumber Inheritance “Is a” relationship Teacher is an Employee Composition is implemented by having instance variables of that type (composite class). Inheritance is implemented by subclass extending the superclass to inherit the attributes and behaviors of superclass. There may be multiple components in a class; there is only one (direct) superclass for a given class in Java (multi-inheritance is possible in some other languages). 11/24/2018

11/24/2018

11/24/2018

11/24/2018

Composition 11/24/2018

Lab activities (Week 5) Rewrite the Point and Circle program of the examples as a Point and Rectangle program and test them. Let the Point coordinators as one corner of the Rectangle and set a length and a width instead of a radius. If you have time, use composition instead of inheritance; will the test program work as is? For this assignment, use the sample classes. 11/24/2018