PROGRAMMING LANGUAGE 2

Similar documents
Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Chapter 5 Object-Oriented Programming

Module 10 Inheritance, Virtual Functions, and Polymorphism

JAVA. Lab-9 : Inheritance

Inheritance and Polymorphism

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.

Programming II (CS300)

Object Oriented Programming. Java-Lecture 11 Polymorphism

Hierarchical abstractions & Concept of Inheritance:

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

ECE 122. Engineering Problem Solving with Java

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Java Object Oriented Design. CSC207 Fall 2014

CS313D: ADVANCED PROGRAMMING LANGUAGE

Practice for Chapter 11

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance -- Introduction

CS-202 Introduction to Object Oriented Programming

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CSC 1214: Object-Oriented Programming

Lecture 18 CSE11 Fall 2013 Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 7. Inheritance

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Programming II (CS300)

OBJECT ORİENTATİON ENCAPSULATİON

What is Inheritance?

Chapter 5. Inheritance

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CLASSES AND OBJECTS IN JAVA

Inheritance and Encapsulation. Amit Gupta

Chapter 10 Classes Continued. Fundamentals of Java

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

CMSC 132: Object-Oriented Programming II

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Microsoft Visual Basic 2005: Reloaded

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

C++ Important Questions with Answers

ITI Introduction to Computing II

Arrays Classes & Methods, Inheritance

Inheritance, and Polymorphism.

Data Structures (list, dictionary, tuples, sets, strings)

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS111: PROGRAMMING LANGUAGE II

Data Abstraction. Hwansoo Han

ITI Introduction to Computing II

Chapter 14 Abstract Classes and Interfaces

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

OVERRIDING. 7/11/2015 Budditha Hettige 82

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

CS111: PROGRAMMING LANGUAGE II

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Inheritance, Polymorphism, and Interfaces

15CS45 : OBJECT ORIENTED CONCEPTS

ENCAPSULATION AND POLYMORPHISM

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

What are the characteristics of Object Oriented programming language?

Object-Oriented Concepts

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Lecture 2: Java & Javadoc

CMSC 132: Object-Oriented Programming II

Instance Members and Static Members

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Java Fundamentals (II)

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

The major elements of the object-oriented model

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Inheritance. Transitivity

Object Orientated Analysis and Design. Benjamin Kenwright

INHERITANCE Inheritance Basics extends extends

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CSE 307: Principles of Programming Languages

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

G Programming Languages - Fall 2012

Unit 4 - Inheritance, Packages & Interfaces

Comp 249 Programming Methodology

Class, Variable, Constructor, Object, Method Questions

Object Oriented Issues in VDM++

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Polymorphism and Inheritance

Chapter 2: Java OO II X I A N G Z H A N G

Programming overview

Inheritance CSC 123 Fall 2018 Howard Rosenthal

CompuScholar, Inc. 9th - 12th grades

Inheritance (Part 5) Odds and ends

Inheritance & Polymorphism

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

INHERITANCE: EXTENDING CLASSES

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Transcription:

31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance

31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It allows the creation of hierarchical classifications. The class that is inherited is called a superclass. The class that does the inheriting is called a subclass. A subclass is a specialized version of a superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

31/10/2013 Ebtsam Abd elhakam 3 Inheritance Basics To inherit a class, you simply join the definition of one class into another by using the extends keyword.

31/10/2013 Ebtsam Abd elhakam 4

31/10/2013 Ebtsam Abd elhakam 5 Inheritance As you can see, the subclass B includes all of the members of its superclass, A. This is why subob can access i and j and call showij( ). Also, inside sum( ), i and j can be referred to directly, as if they were part of B.

31/10/2013 Ebtsam Abd elhakam 6 Inheritance general form The general form of a class declaration that inherits a superclass is shown here: class subclass-name extends superclass-name { // body of class } You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass.

31/10/2013 Ebtsam Abd elhakam 7 Member Access and Inheritance Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.

31/10/2013 Ebtsam Abd elhakam 8 Member Access and Inheritance This program will not compile because the reference to j inside the sum( ) method of B causes an access violation. Since j is declared as private, it is only accessible by other members of its own class. Subclasses have no access to it. A class member that has been declared as private will remain private to its class. It is not accessible by any code outside its class, including subclasses.

31/10/2013 Ebtsam Abd elhakam 9 A More Practical Example

31/10/2013 Ebtsam Abd elhakam 10

31/10/2013 Ebtsam Abd elhakam 11 BoxWeight inherits all of the characteristics of Box and adds to them the weight component. It is not necessary for BoxWeight to re-create all of the features found in Box. It can simply extend Box to meet its own purposes.

31/10/2013 Ebtsam Abd elhakam 12 Another Example A major advantage of inheritance is that once you have created a superclass that defines the attributes common to a set of objects, it can be used to create any number of more specific subclasses. Each subclass can precisely tailor its own classification.

A Superclass Variable Can Reference a Subclass Object A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. 13

31/10/2013 Ebtsam Abd elhakam 14 A Superclass Variable Can Reference a Subclass Object Here, weightbox is a reference to BoxWeight objects, and plainbox is a reference to Box objects. Since BoxWeight is a subclass of Box, it is allowed to assign plainbox reference to the weightbox object. It is important to understand that it is the type of the reference variable not the type of the object that it refers to that determines what members can be accessed. That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why plainbox can t access weight even when it refers to a BoxWeight object. If you think about it, this makes sense, because the superclass has no knowledge of what a subclass adds to it. This is why the last line of code in the preceding fragment is commented out. It is not possible for a Box reference to access the weight field, because Box does not define one.

31/10/2013 Ebtsam Abd elhakam 15 Using super In the preceding examples, classes derived from Box were not implemented as efficiently or as robustly as they could have been. For example, the constructor for BoxWeight explicitly initializes the width, height, and depth fields of Box( ). Not only does this duplicate code found in its superclass, which is inefficient, but it implies that a subclass must be granted access to these members. Also, when superclass make its member as private. In this case, there would be no way for a subclass to directly access or initialize these variables on its own. Since encapsulation is a primary attribute of OOP, it is not surprising that Java provides a solution to this problem. Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. super has two general forms. The first calls the superclass constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass (i.e. superclass and subclass members have the same names).

31/10/2013 Ebtsam Abd elhakam 16 Using super to Call Superclass Constructors A subclass can call a constructor defined by its superclass by use of the following form of super: super(arg-list); Here, arg-list specifies any arguments needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor.

31/10/2013 Ebtsam Abd elhakam 17 Example Here, BoxWeight( ) calls super( ) with the arguments w, h, and d. This causes the Box( ) constructor to be called, which initializes width, height, and depth using these values. BoxWeight no longer initializes these values itself. It only needs to initialize the value unique to it: weight. This leaves Box free to make these values private if desired.

31/10/2013 Ebtsam Abd elhakam 18

31/10/2013 Ebtsam Abd elhakam 19

31/10/2013 Ebtsam Abd elhakam 20

31/10/2013 Ebtsam Abd elhakam 21

31/10/2013 Ebtsam Abd elhakam 22 A Second Use for super The second form of super acts some what like this, except that it always refers to the superclass of the subclass in which it is used. The general form: super.member Here, member can be either a method or an instance variable. This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass. Consider this simple class hierarchy: // Using super to overcome name hiding. class A { int i; }

31/10/2013 Ebtsam Abd elhakam 23

31/10/2013 Ebtsam Abd elhakam 24 Part 2

31/10/2013 Ebtsam Abd elhakam 25 Creating a Multilevel Hierarchy However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a subclass as a superclass of another. For example, given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A. When this type of situation occurs, each subclass inherits all of the traits found in all of its super classes. In this case, C inherits all aspects of B and A.

31/10/2013 Ebtsam Abd elhakam 26

31/10/2013 Ebtsam Abd elhakam 27

31/10/2013 Ebtsam Abd elhakam 28

31/10/2013 Ebtsam Abd elhakam 29 // volume method is a member of Box class // weight is a member of BoxWeight

31/10/2013 Ebtsam Abd elhakam 30 Because of inheritance, Shipment can make use of the previously defined classes of Box and BoxWeight, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code. This example illustrates one other important point: super( ) always refers to the constructor in the closest superclass. The super( ) in Shipment calls the constructor in BoxWeight. The super( ) in BoxWeight calls the constructor in Box. In a class hierarchy, if a superclass constructor requires parameters, then all subclasses must pass those parameters up the line. This is true whether or not a subclass needs parameters of its own. NOTE In the preceding program, the entire class hierarchy, including Box, BoxWeight, and Shipment, is shown all in one file. This is for your convenience only. In Java, all three classes could have been placed into their own files and compiled separately.

31/10/2013 Ebtsam Abd elhakam 31 When Constructors Are Called When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy called?

31/10/2013 Ebtsam Abd elhakam 32 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.

31/10/2013 Ebtsam Abd elhakam 33 Method overriding -2

31/10/2013 Ebtsam Abd elhakam 34 If you wish to access the superclass version of an overridden method, you can do so by using super. For example, in this version of B, the superclass version of show( ) is invoked within the subclass version.

31/10/2013 Ebtsam Abd elhakam 35 Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded.

31/10/2013 Ebtsam Abd elhakam 36

31/10/2013 Ebtsam Abd elhakam 37 Dynamic Method Dispatch Method overriding forms the basis for one of Java s most powerful concepts: dynamic method dispatch. Dynamic method dispatch: is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

31/10/2013 Ebtsam Abd elhakam 38 Dynamic Method Dispatch - 2 A superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.

31/10/2013 Ebtsam Abd elhakam 39

31/10/2013 Ebtsam Abd elhakam 40 As the output shows, the version of callme( ) executed is determined by the type of object being referred to at the time of the call.

31/10/2013 Ebtsam Abd elhakam 41 Why Overridden Methods? Overridden methods allow Java to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives, while allowing subclasses to define the specific implementation of some or all of those methods. Overridden methods are another way that Java implements the one interface, multiple methods aspect of polymorphism. Part of the key to successfully applying polymorphism is understanding that the super classes and subclasses form a hierarchy which moves from lesser to greater specialization.

31/10/2013 Ebtsam Abd elhakam 42 Why Overridden Methods? Used correctly, the superclass provides all elements that a subclass can use directly. It also defines those methods that the derived class must implement on its own. This allows the subclass the flexibility to define its own methods, yet still enforces a consistent interface. Thus, by combining inheritance with overridden methods, a superclass can define the general form of the methods that will be used by all of its subclasses. Dynamic, run-time polymorphism is one of the most powerful mechanisms that object oriented design brings to bear on code reuse and robustness. The ability of existing code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool.

31/10/2013 Ebtsam Abd elhakam 43 Applying Method Overriding

31/10/2013 Ebtsam Abd elhakam 44

31/10/2013 Ebtsam Abd elhakam 45