Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 Object-Oriented Programming (1): Inheritance

Similar presentations


Presentation on theme: "Week 4 Object-Oriented Programming (1): Inheritance"— Presentation transcript:

1 Week 4 Object-Oriented Programming (1): Inheritance
IFS410: Advanced Analysis and Design Week 4 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. 9/12/2018 4.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 9/12/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 9/12/2018

4 9/12/2018

5 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 9/12/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).

6 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. On the other hand, if there are only specific classes, you cannot reuse the code. Programmers realized the benefit of code reuse, and they try to be more generic whenever applicable. This will promote the software re-usability. (Remember: you cannot be just a beneficiary to take advantage of inheritance. There should be someone who programmed the super-class so that someone in the future will create sub-classes out of it). 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. 9/12/2018

7 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). 9/12/2018

8 9/12/2018

9 9/12/2018

10 9/12/2018

11 Composition 9/12/2018

12 Lab activities (Week 4) 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. 9/12/2018


Download ppt "Week 4 Object-Oriented Programming (1): Inheritance"

Similar presentations


Ads by Google