Inheritance CT1513.

Slides:



Advertisements
Similar presentations
1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.
Advertisements

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.
Chapter 1 Inheritance University Of Ha’il.
Reusable Classes.  Motivation: Write less code!
OOP: Inheritance By: Lamiaa Said.
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Inheritance CT Introduction 2  Inheritance  Software reusability  Create new class from existing class  Absorb existing class’s data and behaviors.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
 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.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
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.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
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
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Topics Inheritance introduction
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Inheritance CT Introduction Inheritance Software reusability Create new class from existing class Absorb existing class’s data and behaviors Enhance.
Inheritance 1.  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Advanced Programming in Java
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Java Inheritance.
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Java Inheritance.
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Object-Oriented Programming: Inheritance
Inheritance and Polymorphism
Inheritance CT1513.
Inheritance CT1513.
Object-Oriented Programming: Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Java Inheritance.
Presentation transcript:

Inheritance CT1513

Introduction 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

Introduction (Cont.) 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

Superclasses and subclasses Object of one class “is an” object of another class Example: mustang is car. Class mustang inherits from class car car: superclass mustang : subclass Superclass typically represents larger set of objects than subclasses Example: superclass: Vehicle Cars, trucks, boats, bicycles, … subclass: Car Smaller, more-specific subset of vehicles

Examples

Fig. 9.2 | Inheritance hierarchy for university CommunityMembers

Fig. 9.3 | Inheritance hierarchy for Shapes.

Modifiers in inheritance Protected (#) makes a data member or method visible and accessible to the instances of the class and the descendant classes (subclasses). Public (+) data members and methods are accessible to everyone. Private(-) data members and methods are accessible only to instances of the class.

Inheritance and Member Accessibility We use the following visual representation of inheritance to illustrate data member accessibility.

The Effect of Three Visibility Modifiers

Accessibility of Super from Sub Everything except the private members of the Super class is visible from a method of the Sub class.

Accessibility from Unrelated Class class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super () { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub () { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } class Client { public void main (String [] args) { Super mySuper = new Super(); Sub mySub = new Sub(); int i = mySuper.pub_Super_Field; int j = mySub.pub_Super_Field; // inherited int k = mySub.pub_Sub_Field; } :Super :Sub :Client  VALID

Accessibility from Unrelated Class class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } class Client { public void main (String [] args) { Super mySuper = new Super(); Sub mySub = new Sub(); int l = mySuper.pri_Super_Field; int m = mySub.pri_Sub_Field; int n = mySub.pri_Super_Field; } :Super :Sub :Client  NOT VALID

Accessibility from Unrelated Class class Super { public int pub_Super_Field; protected int pro_Super_Field; private int pri_Super_Field; public Super() { pub_Super_Field = 10; pro_Super_Field = 20; pri_Super_Field = 30; } class Sub extends Super { public int pub_Sub_Field; protected int pro_Sub_Field; private int pri_Sub_Field; public Sub() { pub_Sub_Field = 100; pro_Sub_Field = 200; pri_Sub_Field = 300; } class Client { public void main (String [] args) { Super mySuper = new Super(); Sub mySub = new Sub(); int o = mySuper.pro_Super_Field; int p = mySub.pro_Sub_Field; int q = mySub.pro_Super_Field; } :Super :Sub :Client  NOT VALID

Accessibility of Super from Super class Super { public void superToSuper(Super anotherSuper) { int i = anotherSuper.pub_Super_Field; int j = anotherSuper.pro_Super_Field; int k = anotherSuper.pri_Super_Field; }  VALID :Super anotherSuper

Accessibility of Sub from Sub class Sub extends Super { public void subToSub(Sub anotherSub) { int i = anotherSub.pub_Sub_Field; int j = anotherSub.pro_Sub_Field; int k = anotherSub.pri_Sub_Field; int l = anotherSub.pub_Super_Field; //inherited int m = anotherSub.pro_Super_Field; int n = anotherSub.pri_Super_Field; }  VALID  VALID  NOT VALID :Sub anotherSub

Accessibility of Sub from Super class Super { public void superToSub(Sub sub) { int i = sub.pub_Sub_Field; int j = sub.pro_Sub_Field; int k = sub.pri_Sub_Field; }  VALID  NOT VALID :Super :Sub sub

Accessibility of Super from Sub class Sub extends Super { public void subToSuper(Super mySuper) { int i = mySuper.pub_Super_Field; int j = mySuper.pro_Super_Field; int k = mySuper.pri_Super_Field; }  VALID  NOT VALID :Sub :Super mySuper

Inheritance and Constructors Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses!! You must define a constructor for a class or use the default constructor added by the compiler. A subclass uses a constructor from the base class to initialize all the data inherited from the base class

Inheritance and Constructors In order to invoke a constructor from the base class, it uses a special syntax: **the call of the super constructor must be the first statement.

Using super methods In subclass you can override (or redefine) a method That is already existed in super class. If there is no override you can call a public method of a superclass by its name and appropriate parameter list. If the sub override the super method you should call it by using the reserved key word super.methodsName()

Animal Example

Animal Example

Example person

Example person

Example person

Example person