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

Similar documents
What is Inheritance?

Inheritance -- Introduction

Chapter 5 Object-Oriented Programming

Lecture 18 CSE11 Fall 2013 Inheritance

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

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

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

Polymorphism. return a.doublevalue() + b.doublevalue();

Rules and syntax for inheritance. The boring stuff

Practice for Chapter 11

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

CS-202 Introduction to Object Oriented Programming

25. Generic Programming

ITI Introduction to Computing II

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

ITI Introduction to Computing II

More about inheritance

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

CS260 Intro to Java & Android 03.Java Language Basics

What are the characteristics of Object Oriented programming language?

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

CIS 110: Introduction to computer programming

INHERITANCE. Spring 2019

Class, Variable, Constructor, Object, Method Questions

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

COMP 110/L Lecture 19. Kyle Dewey

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Inheritance Motivation

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

CS/ENGRD 2110 SPRING 2018

Overriding Variables: Shadowing

Programming overview

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

More on Objects in JAVA TM

Programming Exercise 14: Inheritance and Polymorphism

Inheritance. Transitivity

JAVA MOCK TEST JAVA MOCK TEST II

Programming II (CS300)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Inheritance

ASSIGNMENT NO 13. Objectives: To learn and understand concept of Inheritance in Java

Handout 9 OO Inheritance.

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

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

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

CompuScholar, Inc. 9th - 12th grades

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

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

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

Super-Classes and sub-classes

Check out Polymorphism from SVN. Object & Polymorphism

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

INHERITANCE AND EXTENDING CLASSES

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Polymorphism and Interfaces. CGS 3416 Spring 2018

Implements vs. Extends When Defining a Class

Inheritance. Exploring Polymorphism. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Abstract Classes and Interfaces

C++ Important Questions with Answers

C12a: The Object Superclass and Selected Methods

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

COP 3330 Final Exam Review

Programming in C# Inheritance and Polymorphism

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CLASS DESIGN. Objectives MODULE 4

Type Hierarchy. Lecture 6: OOP, autumn 2003

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

Java Magistère BFA

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CISC370: Inheritance

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CS111: PROGRAMMING LANGUAGE II

Arrays Classes & Methods, Inheritance

What about Object-Oriented Languages?

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

Binghamton University. CS-140 Fall Dynamic Types

Motivations. Chapter 13 Abstract Classes and Interfaces

1 Shyam sir JAVA Notes

Inheritance (Outsource: )

9/17/2018 Programming Data Structures. Encapsulation and Inheritance

CS111: PROGRAMMING LANGUAGE II

Chapter 9 Inheritance

A base class (superclass or parent class) defines some generic behavior. A derived class (subclass or child class) can extend the base class.

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

Java: introduction to object-oriented features

Exercise: Singleton 1

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

Section: Inheritance Heck

Declarations and Access Control SCJP tips

Transcription:

More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature as the one in the super class, thus hiding it. You can declare new methods in the subclass that are not in the super class. class SubClass extends SuperClass {..... } All the methods (except private) of Superclass visible to SubClass. Consider the example given below: Objects Methods osub SubClass m3,m4 osuper m1,m2 SuperClass

In this example super / base class has methods m1, m2 and sub/derived class has methods m3 and m4. superclass has object name osuper and subclass has object osub. Now see the following: osub.m3(<args>) osub.m4(<args>) These are fine because osub is an object of subclass and m3 and m4 are also methods of subclass. We can access member data and member function of a class using its object. Similarly: osuper.m2(<args>) osuper.m1(<args>) are correct see the followings: osub.m1(<args>) osub.m2(<args>) osuper.m3(<args>) osuper.m4(<args>) In first two statements, osub is an object of derived/subclass and m1 and m2 are methods of base/super class. As we know that all the methods(except those with the scope private) of base class are also visible to subclass and we can access them by object of subclass. Both the statements are correct.

In third and last statement osuper is an object of super class,while m3 and m4 are methods of sub class, and we cannot access the methods of sub class by object of base class. Both the statement will not work. Type Casting: ((superclass)osub).m1 ((superclass)osub).m2 These will work because the final reference is of super class and we are accessing methods of super class. ((superclass)osub).m3 This won t work because final reference is of super class and we are accessing method of subclass(m3 is a method of subclass) by using reference of super class we cannot access method of subclass. Now consider the example 2 : Person is a base class has a derived class Student because Student is also a Person. Student extends Person Class Student extends Person { };.... Person person1 = new Student(); Student student1 = (Student) person1; // Explicit type casting

Runtime Type Mismatch Exception: Even with explicit casting, you could still end up having a runtime error Example: Let's assume Student class is a child class of Person class Let's assume Teacher class is also a child class of Person class Person person1= new Student (); Person person2 = new Teacher (); Student student1 = (Student) person1; // Explicit type casting // No compile error, but runtime type mismatch exception if Student student2 = (Student) person2 instanceof Operator: it s a binary operator, takes an object and a class as operand and returns true and false. See the following following from first example: (osub instanceof Subclass ) // it will return true. (osub instanceof Superclass) // it will also return true Use instanceof Operator to Prevent Runtime Type Mismatch Error: You can check the type of the object instance using instanceof before the type casting Example: Person person1 = new Student(); Person person2 = new Teacher(); // Do the casting only when the type is verified

if (person2 instanceof Student) { Student student2 = (Student) person2; } Super cosmic class: every class extends a class called Object class. Object class called super cosmic class. Object Class is parent class of all classes In Java language, all classes are sub classed (extended) from the Object super class Object class is the only class that does not have a parent class Defines and implements behavior common to all classes including the ones that you write. Object class has following methods: a) getclass(), b) equals(), c) tostring().. etc. Assume that you have a base class name SuperClass and derived class name SubClass. SubClass SuperClass Object Class

You can override these methods like tostring() etc, that are also in Object class. Call to these methods will traverse the inheritance hierarchy from leaf to root, i.e. from the calling class up to Object class, until an appropriate match is found.