Object Oriented Programming. Java-Lecture 11 Polymorphism

Similar documents
Inheritance and Polymorphism

CS111: PROGRAMMING LANGUAGE II

Inheritance and object compatibility

Java Object Oriented Design. CSC207 Fall 2014

Chapter 14 Abstract Classes and Interfaces

Polymorphism and Interfaces. CGS 3416 Spring 2018

CS-202 Introduction to Object Oriented Programming

Inheritance -- Introduction

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Practice for Chapter 11

Chapter 10 Classes Continued. Fundamentals of Java

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

JAVA MOCK TEST JAVA MOCK TEST II

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Chapter 5 Object-Oriented Programming

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

PROGRAMMING LANGUAGE 2

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

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

CS 251 Intermediate Programming Inheritance

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

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

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

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

More On inheritance. What you can do in subclass regarding methods:

What is Inheritance?

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

Polymorphism and Inheritance

First IS-A Relationship: Inheritance

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

More on Objects in JAVA TM

Java How to Program, 8/e

Lecture 18 CSE11 Fall 2013 Inheritance

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance, and Polymorphism.

CS313D: ADVANCED PROGRAMMING LANGUAGE

More Relationships Between Classes

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

Inheritance and Polymorphism. CS180 Fall 2007

QUESTIONS FOR AVERAGE BLOOMERS

Programming II (CS300)

25. Generic Programming

CS111: PROGRAMMING LANGUAGE II

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance. Transitivity

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

INHERITANCE. Spring 2019

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

CS111: PROGRAMMING LANGUAGE II

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

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

Programming in C# Inheritance and Polymorphism

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

Self-review Questions

CMSC 132: Object-Oriented Programming II

ECE 122. Engineering Problem Solving with Java

Data Abstraction. Hwansoo Han

Inheritance (continued) Inheritance

Inheritance (cont.) Inheritance. Hierarchy of Classes. Inheritance (cont.)

ITI Introduction to Computing II

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

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Programming II (CS300)

CS Programming I: Inheritance

Classes and Inheritance Extending Classes, Chapter 5.2

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

COMP 110/L Lecture 19. Kyle Dewey

Inheritance, Polymorphism, and Interfaces

More about inheritance

Inheritance Motivation

ITI Introduction to Computing II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CST242 Object-Oriented Programming Page 1

Inheritance & Polymorphism

Full file at Chapter 2 - Inheritance and Exception Handling

Inheritance and Polymorphism

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Inheritance and Polymorphism

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

8. Polymorphism and Inheritance

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

Inheritance and Polymorphism

Lecture Notes on Programming Languages

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

CS111: PROGRAMMING LANGUAGE II

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

Inheritance and Polymorphism

C++ Inheritance and Encapsulation

VIRTUAL FUNCTIONS Chapter 10

Java Magistère BFA

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Transcription:

Object Oriented Programming Java-Lecture 11 Polymorphism

Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class Abstract class purpose is to provide an appropriate super class from which other classes can inherit and thus share the common design Subclasses of an abstract super class has to define the missing pieces in the abstract Superclass Abstract classes are incomplete (they are just a design) so no objects of abstract class can be created Classes for which objects can be created are called concrete classes Concrete classes provide implementation of every method they declare An abstract class contains one or more abstract methods

Abstract Classes and Methods An abstract class contains one or more abstract methods Abstract methods do not provide implementations Each concrete subclass of an abstract superclass must provide concrete implementation of each of the superclass s abstract methods Constructors and static methods can not be declared abstract Constructors are not inherited Static methods can not be overridden

Abstract class and methods Example Implementation in : NumberSystemTest.java

Super class reference Vs Subclass object When we create a abstract superclass reference variable and this variable references to an concrete subclass object, we can use this reference variable to manipulate subclass objects Java run time environment knows which object method should be called. This is one form of polymorphism

Operator instanceof instanceof operator Since we are using abstract super class reference to process subclass objects, we may need to know what is the exact type of the object To check whether an object is an instance of a particular class we use instanceof operator Example: If(obj instanceof OctalNumberSystem) { } System.out.printf( Object is OctalNumberSystem object\n", i);

Allowed assignments between superclass and subclass variables A subclass object is also a superclass object Subclass may have additional members Assigning a superclass reference to a superclass variable Assigning a subclass reference to a subclass variable Assigning subclass reference to a superclass variable Superclass variable can be used to refer only to superclass members If subclass only members are referred compilation error will occur Attempting to assign a superclass reference to a subclass variable is a compilation error. To avoid error you must explicitly type cast At the execution time if the object to which the reference refers is not a subclass object an exception will occur

Final methods A final method in a super class can not be overridden in subclass Methods declared private are implicitly final (it not possible to override) Methods which are declared static are implicitly final A final method declaration can never change. Every subclass use the same method implementation Calls to final methods are resolved at compilation time static binding

Final classes A final class can not be superclass All methods in a final class are implicitly final String class is an example of final class. No subclasses can be made of String class

Java interfaces Interfaces are reference types similar to a class Contains only constants and method signatures (no implementation) One interface can extend none, one or more interfaces. Class can implement many interfaces If a class implements an interface does not implement all the methods then it should be declared abstract

Abstract classes Vs interfaces Abstract Class used to declare common characteristics of subclasses Abstract class can not be instatiated Abstract classes provide template or design for concrete subclasses Abstract class can contain fields and methods. (methods may be abstract) If a class has a abstract method it should be declared abstract Drawback no multiple inheritance Interfaces Can be used to define generic template and one or more abstract classes to define the partial implemetations of the interface Interafce just specifies method declaration (implicitly public abstract) all the fields are public static final Interface can not be instanciated Multiple inheritance is possible. One interface can extend none, one or more interfaces. Class can implement many interfaces If a class implements an interface does not implement all the methods then it should be declared abstract

Interfaces Example Interface Shape TwoDimentionalShape Point ThreeDimentionalShape Quadrilateral Circle Cube Sphere Rectangle Rectangle Implementation in : InterfaceTest.java