Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Chapter 10: Introduction to Inheritance
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Programming in Java CSCI-2220 Object Oriented Programming.
C# G 1 CSC 298 Object Oriented Programming Part 2.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Coming up: Inheritance
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CITA 342 Section 1 Object Oriented Programming (OOP)
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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Final and Abstract Classes
Inheritance and Polymorphism
Packages, Interfaces & Exception Handling
Java Programming Language
Interfaces.
Java – Inheritance.
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Inheritance Lakshmish Ramaswamy.
Computer Science II for Majors
Presentation transcript:

Inheritance Extending Class Functionality

Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we consider multiple classes to be of the same “role” or “category,” with certain properties or methods shared in common.

Polymorphism Review Step 1: creating an abstract base class that declares the common methods. – One or more methods are declared (but not defined). Use the virtual keyword! – These methods have specifications that should be followed whenever they are implemented.

Polymorphism Review Step 2: declaring our classes to be extensions of that abstract base class. – This allows instances of our classes to be considered as instances of that abstract base type.

Polymorphism Review Step 3: implementing the declared methods of the base class. – All undefined methods must be implemented for the class to be instantiated. – These implementations should follow the base class’s specifications.

Questions?

Toward Inheritance Note that in our original base classes for polymorphism, every declared method was “pure” virtual. – This reflects interfaces in Java. – This allows a base class to ensure it remains abstract and to force a base class to implement the method manually.

Toward Inheritance class Shape { public: virtual double area() = 0; virtual double perimeter() = 0; } For this example, there is no one “right” way to implement area; it depends on each specific type of Shape.

Toward Inheritance There may be some cases in coding, however, when the base class could provide some functionality for its derived classes. – There may be some specifics better left to each of the derived classes, but with some core features held in common.

Toward Inheritance Previously, we had an example themed off of bank accounts and automatic transactions. Let’s re-examine the base class from that example.

Toward Inheritance public class Account { public: // Throws exception if deposit is too great. virtual void deposit(double amt) = 0; virtual double getMaxDeposit() = 0; // Exception if withdrawal is too great. // (or illegal - loans) virtual void withdraw(double amt) = 0; virtual double getMaxWithdrawal() = 0; } // You can’t overpay a loan, and checking/savings accts // tend to have overdraft limits, hence the Max methods.

Toward Inheritance Without being specific to any particular bank account type, what if we could implement one or more methods to support possible derived types?

Inheritance In C++, entire class specifications can be inherited from a base class to a derived class. – This includes fields and method definitions.

Inheritance Inheriting from a class means that the derived class should be considered a “more specific” version of the base class. – It inherits all the original specifications and adds more of its own. – In C++, any ( public ly) derived class is automatically polymorphic to its base class.

Inheritance All this being said, what functionality could we have our base ( Account ) class provide to future derived classes?

Inheritance In-class example.

Questions?

Access Specifiers Thus far, we have seen two access modifiers: – public : declares a field or method is fully visible from any object – private : places fields or methods on “lockdown,” making them invisible outside of the class.

Access Specifiers There exists another access modifier: – protected : declares a field or method is invisible outside of the class, except to those inheriting from the class. This can be very useful to extend core functionality in derived classes. One example: providing an empty protected method called by the base class in certain situations.

Access Specifiers Note that when extending a class in C++, an access specifier is used. – This allows the derived class to restrict access to the base class’s members. – All base class fields and methods will have their access be at least as strict as the given modifier. protected inheritance will cause public base class methods to become protected within the derived class.

Access Specifiers Note that when extending a class in C++, an access specifier is used. – The implied polymorphism of the derived class to its base will be similiarly restricted.

The friend keyword It may be desirable for one class to permit another to have special access rights to its members. – The solution: the friend keyword.

The friend keyword Declaring another class as a friend grants it private-level access to all fields and methods. – This only applies for the exact friend - granting class (not its super- or sub- classes) – Likewise, only the exact friend stated is granted special access. friend ship is not inherited.

Questions?

Odds and Ends To make sure that a derived class is properly overriding (or implementing) a base class method, the override keyword may be appended at the end of the declaration’s signature. – If the override n method does not exist, or is not virtual, a compiler error will result.

Odds and Ends To make sure that a derived class cannot possibly override a method, the base class may declare a method final. – Any attempt to override it in a derived class will be marked as a compiler error. – Both final and override appear at the absolute end of a method declaration signature.