Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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).
5
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
6
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
7
11/24/2018
8
11/24/2018
9
11/24/2018
10
Composition 11/24/2018
11
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.