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

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

Inheritance. Transitivity

INHERITANCE. Spring 2019

The class Object. Lecture CS1122 Summer 2008

CS-202 Introduction to Object Oriented Programming

CMSC131. Library Classes

Super-Classes and sub-classes

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

CLASS DESIGN. Objectives MODULE 4

Introduction to Inheritance

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

Practice for Chapter 11

C12a: The Object Superclass and Selected Methods

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance -- Introduction

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

More about inheritance

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Rules and syntax for inheritance. The boring stuff

Java Fundamentals (II)

CMSC 132: Object-Oriented Programming II

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

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

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

Java Magistère BFA

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

COP 3330 Final Exam Review

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

JAVA MOCK TEST JAVA MOCK TEST II

Lecture 10. Overriding & Casting About

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

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

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

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

CS/ENGRD 2110 SPRING 2018

Introduction to Programming Using Java (98-388)

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

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

Building Java Programs. Inheritance and Polymorphism

Implements vs. Extends When Defining a Class

Abstract Classes and Interfaces

CH. 2 OBJECT-ORIENTED PROGRAMMING

What is Inheritance?

Inheritance and Polymorphism

Methods Common to all Classes

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Inheritance (Part 5) Odds and ends

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

More on Objects in JAVA TM

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Java Classes, Inheritance, and Interfaces

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Advanced Placement Computer Science. Inheritance and Polymorphism

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

JAVA MOCK TEST JAVA MOCK TEST III

Programming Exercise 14: Inheritance and Polymorphism

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

Chapter 5 Object-Oriented Programming

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Everything is an object. Almost, but all objects are of type Object!

Big software. code reuse: The practice of writing program code once and using it in many contexts.

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

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

Definition of DJ (Diminished Java)

COE318 Lecture Notes Week 8 (Oct 24, 2011)

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

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

Polymorphism and Inheritance

Overriding Variables: Shadowing

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Lecture Notes Chapter #9_b Inheritance & Polymorphism

The Java Type System (continued)

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Review sheet for Final Exam (List of objectives for this course)

CIS 110: Introduction to computer programming

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

Declarations and Access Control SCJP tips

Chapter 14 Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Programming Languages and Techniques (CIS120)

Inheritance (continued) Inheritance

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

1 Shyam sir JAVA Notes

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Programming II (CS300)

CS 251 Intermediate Programming Inheritance

Class, Variable, Constructor, Object, Method Questions

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

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

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Lecture 4: Extending Classes. Concept

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Inheritance and Polymorphism

Making New instances of Classes

Transcription:

CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object obj); When this came up, I said we'd return to talk about it in more detail towards the end of the semester

Inheritance In OO langs a new class can extend an existing class. Every class that doesn t explicitly extend some other class will still extend Object. The existing class is called the base or super class. The new (typically called the derived or sub) class will inherit the static and instance variables and methods from the base. However, it might not have direct access to some of the things it inherits! Base references can be used to point to derived objects. Syntax Public Class Base { Public Class Derived Extends Base {

protected In addition to public, private, and the default levels, there is an additional one that was briefly mentioned called protected. With this level, a protected variable or method can be accessed by the class or by any class derived from it or by other classes in the same package as the class. Access / Visibility Summary Modifier Within Class Within Package Subclass (more Wed/Fri) Outsiders public YES YES YES YES protected YES YES YES NO (more Wed/Fri) default YES YES NO NO (no modifier) private YES NO NO NO

Example: InheritanceHitsAndMisses What can and can't we access from where? More on Casting You can always cast a derived class back down to its base class. If you try to cast a base object into a derived class it will throw a runtime error. This is important if you have (for example) an array of base references but you think the object at the end of the reference is actually a derived one and you want to access something in that part of the object. We can (and probably should) test before casting so we can handle things ourselves.

Class X extends Class A Class Y extends Class A Will this 1. Not compile. 2. Compile but crash. 3. Compile and run fine. 4. No idea. X var = new A(); 0 of 5 Class X extends Class A Class Y extends Class A Will this 1. Not compile. 2. Compile but crash. 3. Compile and run fine. 4. No idea. A var = new X(); 0 of 5

Class X extends Class A Class Y extends Class A Will this 1. Not compile. 2. Compile but crash. 3. Compile and run fine. 4. No idea. Y var = new X(); 0 of 5 Class X extends Class A Class Y extends Class A Will this 1. Not compile. 2. Compile but crash. 3. Compile and run fine. 4. No idea. X var1 = new X(); A var2 = (A)var1; 0 of 5

Class X extends Class A Class Y extends Class A Will this 1. Not compile. 2. Compile but crash. 3. Compile and run fine. 4. No idea. A var1 = new A(); X var2 = (X)var1; 0 of 5 instanceof Before casting, you might want to test to make sure you are correct about the type. if (objectref instanceof DerivedName) { ((DerivedName)objectRef).funInDer();

Overriding Methods It is possible for a derived class to override a method that has been defined and implemented in the base class. The overriding method needs to have the same name and parameter list and return type as the one in the base class. An interesting issue arises if you have base reference pointing at a derived object if you then use that reference to invoke an overridden method. Which version does it invoke? It depends on the language! In Java it will invoke the derived version. In C++ it will invoke the base version. Some methods Object gives you boolean equals(object); Class getclass(); int hashcode(); String tostring(); Also a variety of methods useful for multithreaded programming

equals The default equals inherited from Object checks to see if the two references are to the same exact object in memory. The following is likely how it should be overridden public boolean equals (Object other) { if (other == null) { return false; else if (this.getclass()!=other.getclass()) { return false; else { //cast other and do real equals tests here "Is A" versus "Has A" Many of the classes we have written have "contained" other types. We could say that a Restaurant "has a" String and "has a" SortedListOfImmutables as "has a" another SortedListOfImmutables. This is referred to as class composition. With inheritance if D is derived from B, we typically say that a D "is a" B.

How to make a Stack? In Java, rather than extend a list structure you might want to just use one (composition). This way your Stack can have push() and pop() but not have the methods "visible" that it would inherit. public class Stack<Type> { private ArrayList<Type> mydata; : : : Some other languages have other options Shadow Variables One thing to be aware of is the names of any variables declared in the base class. If you declare a variable with the same name in the derived class then that is the one that is seen by default (though the one it inherited from the base is still inside the object too). Within a method of the derived class (but not in places in the rest of the project) you can access the base's version by using super. to refer to the variable from the base (also known as the super) class.

Constructors When a derived object's constructor is called, before the body of it is executed, the base class' constructor will be called. If a derived class has a copy constructor, the base class' regular constructor will be called before the body of the derived class' copy constructor is executed unless the derived class' copy constructor explicitly calls the base copy constructor. Multiple Inheritance Some languages that support polymorphism support multiple inheritance. Java is NOT one of those languages. Why? Consider base classes: Helicopter, Jet Consider a new class: HeliJet Could be good use of polymorphism. Could create challenges like naming conflicts if both bases classes have a matching field since they would be at the same "level".

Multiple Interfaces In the previous example, if Helicopter and Jet both defined a String name and HeliJet was built by extending both of them, you would have two different name objects. What if Helicopter and Jet were interfaces and HeliJet implemented both? Is there any potential for conflict or confusion? Copyright 2010-2012 : Evan Golub