Extending Classes Through Inheritance

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance using Java
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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 Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-Oriented Design
Abstract classes and interfaces
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance and Polymorphism
One class is an extension of another.
03/10/14 Inheritance-2.
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Object Oriented Analysis and Design
One class is an extension of another.
Abstract classes and interfaces
Domain Class Diagram Chapter 4 Part 2 pp
Object-Oriented Programming
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
Inheritance and Polymorphism
Object-Oriented Programming
Java Programming, Second Edition
Java Inheritance.
Fundaments of Game Design
Object-Oriented Programming
Inheritance and Polymorphism
Abstract classes and interfaces
Object Oriented Analysis and Design
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Computer Science II for Majors
Presentation transcript:

Extending Classes Through Inheritance B.Ramamurthy 7/3/2019 B.Ramamurthy

Inheritance Inheritance is the act of deriving a new class from an existing one. A primary purpose of inheritance is to reuse existing software. Original class used to derive a new class is called “super” class and the derived class is called “sub” class. Inheritance represents “is a” relationship between the superclass and the subclass. 7/3/2019 B.Ramamurthy 4

Syntax class subclass extends superclass { class definition } Example: class Windstar extends FordCar // meaning it inherits from class Fordcar{ ....} Windstar MyCar; In this example, FordCar is the super-class and Windstar is a sub-class and MyCar is an object reference to Windstar class. MyCar = new Windstar(green); // object instantiated 7/3/2019 B.Ramamurthy

Representing the Relationship BankClass has a has a has a Account [ ] MortgageSVC BrokerageSVC is a is a Savings Checking is a : use inheritance has a : use aggregation, or membership 7/3/2019 B.Ramamurthy 6

Example: Animal Hierarchy is a Animal type Dog constructor name breed show In the code, note the call to super-class constructor “super” constructor (name) constructor (name , breed) 7/3/2019 B.Ramamurthy

Example : Words Book class super class Main class uses is a Dictionary class super class subclass uses is a 7/3/2019 B.Ramamurthy

Modifers Modifiers are special words that are added in front of methods, data fields, classes to specify their nature. Visibility modifiers: private, public, protected Protected modifier is used when the entity needs to be available to the subclass but not to the public. 7/3/2019 B.Ramamurthy

Need for “protected” modifier Employee public: name private: wage_rate and hours uses super class Application uses Employee class, Paid_Employee class Paid_Employee uses public compute_wages(); sub class 7/3/2019 B.Ramamurthy

Need for “protected” modifier Employee is a super class, Paid_Employee is a sub class derived from it. If we make wage_rate and hours public they are available to the whole world. If you make them private then they are not accessible to the sub class unless there are set and get functions. Protected access control provides a mechanism for hiding details from the world but making it available to sub classes. 7/3/2019 B.Ramamurthy

Attributes (Data) Modifiers Data with no modifier is visible to sub-class within the same package but not to sub-class outside the package. Private data is available only within the class in which it is defined. Protected is available to the class and all its sub-classes. Public is available to all class. 7/3/2019 B.Ramamurthy

Polymorphism We already discussed polymorphism in lecture 2 with Food as example. (Given in next slide) Polymorphism is ability to assume several different forms and automatic resolution of one of the forms depending on the (run time) type of the object. 7/3/2019 B.Ramamurthy

Abstract Class (ABC) and methods An abstract class is a class in which one or more methods are declared but not defined. Declaration of the methods can be omitted since it make no sense to implement them in the superclass. Am abstract method has “abstract” modifier in front of it. ABC cannot be instantiated. Sole purpose of ABC is to provide an appropriate super class from which classes may inherit. Classes from which objects can be instantiated are called concrete classes. 7/3/2019 B.Ramamurthy

Example Food Abstract super class Pizza Hamburger HotDog subclasses 7/3/2019 B.Ramamurthy

Example for Superclass Framework public abstract class EPA { \\ different data public abstract void emission_control();} class CA_EPA extends EPA { void emission_control () { //implementation } class NY_EPA extends EPA { void emission_control () { // implementation } 7/3/2019 B.Ramamurthy

Interface An interface essentially takes the abstract class concept to the extreme. When a class contains all abstract methods and/or constants, it can be implemented using an interface. It contains just constants, abstract methods or both. Many different implementations can be realized from a single interface. The concept of interface-implementation is the core of the JDK1.1 event model. 7/3/2019 B.Ramamurthy

Animal interface and implementations dog spaniels show(){…} sound(){….} Special details added on to the general dog characteristics interface others animal Other implementations show(); sound(); 7/3/2019 B.Ramamurthy

Example GUI and mouse listerners EVENT LISTERNERS UPDATE GUI GUI EVENT HANDLERS 7/3/2019 B.Ramamurthy

Summary Inheritance implements a very useful relationship between classes. Inheritance lets you extend and reuse existing classes / library of classes. Interface provides a framework for a system of classes. Interfaces are implemented by other classes. A class may implement more than one interface. 7/3/2019 B.Ramamurthy