1 Inheritance Lecture 9 from Chapter 8. 2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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.
George Blank University Lecturer.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Inheritance and Polymorphism
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Class Inheritance Part I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Lecture 12 Inheritance.
Inheritance-Basics.
Inheritance and Polymorphism
Inheritance in Java.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance Basics Programming with Inheritance
Java Programming Language
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Inheritance.
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

1 Inheritance Lecture 9 from Chapter 8

2 Review Command line arguments Basic Inheritance Member access and inheritance Using super Creating a multi-level hierarchy Calling the constructors Method overriding

3 Command line arguments We can pass the values to a program. This is accomplished by passing command-line arguments to main(). For example, args[0] is 1, args[1] is 2 and args[2] is 3

4 Example

5 Explanation The program will display the arguments (command-line arguments) that it is called with. The output is:  args[0]1  args[1]2  args[2]3

6 Inheritance – an introduction Inheritance enables us to create a class that is similar to a previously defined class, but one that still has some of its own properties. Here, sub class is inherited from super class or super class extends to sub-class Super classSub class

7 Inheritance -characteristics Inheritance is one of the cornerstones of OOP. The other two are: encapsulation and polymorphism. It allows the creation of hierarchical classification. Using inheritance, we can create a general class that defines traits common to a set of related items. This class can then be inherited by other.

8 Example

9 Explanation The sub class B includes all of the members of its superclass A. As a result, class B can access showa(). The sum() can be directly referred. Super classSub class Extend

10 Example – class B access class A

11 Exam ple - more

12 Member Access and Inheritance Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that was declared as private.

13 Example – Explanation with private int j

14 Using super A subclass can call a constructor method defined by its superclass. Super(parameter-list) Here, parameter-list specifies any parameters needed by the constructor in the superclass. Super() must always be the first statement executed inside a subclass’constructor.

15 Example – super(s, i)

16 Explanation In SClass, there are three parameters, s, i and sexcode. Two parameters, namely, s and i are inherited from DClass SClassuses Super(s, i). Here, the parameters are: string s and integer i

17 Super.member, page 202 The second from of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form: super.memebr

18 Example

19 Explanation Here, super.i = a will set the value in Class A this.i will set the value in Class B We could use i instead of this.i

20 Multi-level hierarchy So far, we used two classes, superclass and subclass. We can build hierarchies that contain more than two layers. For example, we could define three classes, A, B and C. C is a subclass of B which is a subclass of A.

21 Example Library item (CityU) Book Video (VCD, DVD) Map Leaflet

22 Example – three classes It displays 1 It displays 2

23 Method Overriding In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. Subclass cannot see the method in the superclass

24 Example

25 Example – more

26 Overload Method overriding occurs only hen the names and the type signatures of the two methods are identical. When the two methods are not identical (the same names and types), it is called overload.

27 Example

28 Dynamic Method Dispatch Method overriding forms the basis of Java’s most powerful concepts. It is called dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method. Dynamic method dispatch is important because this is how Java implements run- time polymorphism.

29 Example – dynamic dispatch

30 Explanation The above program creates one superclass, AClass and two sub- classes, BClass and CClass. A reference of type AClass called r is declared. This program then assigns different reference to different class.

31 Summary Command line arguments – enter values from DOS prompt Basic Inheritance – inherited from superclass Member access and inheritance Using super – similar to this, except for superclass Creating a multi-level hierarchy – supports more than one subclass Method overriding – override the method of same names and type signature