CS 251 Intermediate Programming Inheritance

Similar documents
CS-202 Introduction to Object Oriented Programming

C++ Important Questions with Answers

CS Programming I: Inheritance

Inheritance. Transitivity

Inheritance, Polymorphism, and Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

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

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

Chapter 14 Abstract Classes and Interfaces

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

OVERRIDING. 7/11/2015 Budditha Hettige 82

Practice for Chapter 11

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

Inheritance and Polymorphism. CS180 Fall 2007

Chapter 10 Classes Continued. Fundamentals of Java

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Rules and syntax for inheritance. The boring stuff

CMSC 132: Object-Oriented Programming II

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

CS1150 Principles of Computer Science Objects and Classes

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

What is Inheritance?

Inheritance. Chapter 7. Chapter 7 1

Inheritance and Polymorphism

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

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

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

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

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

Programming Exercise 14: Inheritance and Polymorphism

CS 251 Intermediate Programming Methods and More

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

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

INHERITANCE. Spring 2019

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

Inheritance, polymorphism, interfaces

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

Polymorphism and Inheritance

COP 3330 Final Exam Review

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

Inheritance, and Polymorphism.

Inheritance and Polymorphism

Objects and Iterators

CS 251 Intermediate Programming Methods and Classes

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

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

8. Polymorphism and Inheritance

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

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

Data Abstraction. Hwansoo Han

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

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

Inheritance (continued) Inheritance

More Relationships Between Classes

CMSC 132: Object-Oriented Programming II

Inheritance -- Introduction

Inheritance and Interfaces

Introduction to Inheritance

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

QUESTIONS FOR AVERAGE BLOOMERS

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

Super-Classes and sub-classes

Inheritance and Polymorphism

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

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

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

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

First IS-A Relationship: Inheritance

COMP 250 Fall inheritance Nov. 17, 2017

Chapter 5. Inheritance

Lecture 18 CSE11 Fall 2013 Inheritance

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

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 3: Inheritance and Polymorphism

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

Computer Science II (20073) Week 1: Review and Inheritance

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Exam Duration: 2hrs and 30min Software Design

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

VIRTUAL FUNCTIONS Chapter 10

Inheritance. The Java Platform Class Hierarchy

Computer Science 225 Advanced Programming Siena College Spring Topic Notes: Inheritance

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Polymorphism. Agenda

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object Oriented Programming: Based on slides from Skrien Chapter 2

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

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

+ Abstract Data Types

Inheritance (Outsource: )

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

What are the characteristics of Object Oriented programming language?

Transcription:

CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018

Inheritance We don t inherit the earth from our parents, We only borrow it from our children.

What is inheritance? Just as for humans, sub classes (children) inherit certain properties from their parents. Object Oriented - Doesn t happen if no objects One mother/father (super) object Child inherits all public and protected members

Inheritance It is the same thing as having parents! You get part of what they have, but you can form your own way. If there are parents, there are usually grand-parents too. You get something from them as well...

Everything is an Object The Object class is the Adam/Eve of Java. All classes are subclasses of the Object class Therefore they inherit certain methods (such as tostring()) Object.toString() prints out the address of the object

Subclasses extend parents... In java, all classes extend at least the Object class (implicitly). To extend means to get certain properties from your parent

An example: extends example public class X { public void print () { System. out. println (" Hello "); public class Y extends X { public void tryit () { print (); public static void main ( String [] argv ) { Y test = new Y (); test. tryit (); Y inherited print(); from X!

All classes extend Object If no parent class is given, parent is Object public class Foo { is equivalent to public class Foo extends Object {

this and super There are two keywords that are often used with inheritance. They are: this a reference to the current object instance. Ex: to address an instance variable you may always do: this.variable super a reference to the (parent) object that created this object instance. Use super to access methods that you are overriding in the parent. Note! Calls to super() constructors must always be the first line in your constructor (if you want to)! You can also call other constructors in your object such as this().

Overriding Overloading A class can (as you know) define methods When one class inherits another, the derived class can override a method in the base class. Overriding methods must have the same name, same number of parameters, and same type of all parameters Different from overloading (where types, or number of parameters must differ) If an overriding method is specified as public final void methodname() it can not be overridden.

Overriding Overloading cont... Methods can be both overridden and overloaded at the same time. Fields can not be overridden, only hidden Only accessible methods can be overridden Static members are always hidden (so impossible to override)

Reminder: Access modifiers public Accessible to all private Only this class protected Only this class and its subclasses package-private No modifier. This class and others in same package.

Overriding Annotation The compiler can help check that you are doing what you think you are doing. The @Override annotation tells the compiler that you intend to override a method from the superclass. If you don t, compiler will complain. public class Bar extends Foo { @Override public int foomethod () {

Multiple types A subclass can be assigned to a variable of the supertype class. If you want to check if it s one or the other, you can use the instanceof keyword to check: if ( obj instanceof MyClass )... instanceof works with interfaces, too!

Final classes and methods Classes and methods that are defined as final cannot be extended or overridden respectively. Can be useful if you re sure that you don t want your class extended. Increased security Guarantees specific implementation Generally should only call final helpers in constructors.

Abstract Classes A method header without implementation is called an abstract method Any class containing an abstract method, must be declared as an abstract class Abstract classes can not be instantiated Classes extending the abstract class must provide implementation for the abstract methods.

Abstract class example public abstract class GraphicObject { private int x, y; public void moveto ( int newx, int newy ) { // change x, y here public abstract void draw (); public class Circle extends GraphicObject { public void draw () { // draw a circle

Interfaces An interface is the extreme abstract class, containing only method headers, and no implementations at all. 1 The interface, is a specification of the interface between a programmer and a class Any class that implements an interface, must provide implementations for all methods in the interface (unless it s an abstract class) Interfaces and abstract classes are formal specifications of the interaction between classes (without implementation) 1 Java 8 changes this a bit with default implementations

Example interfaces Comparable Imposes a natural ordering of objects implementing it Collection Common interface for all collections (implementing classes: ArrayList, LinkedList, Vector, etc... Iterator Provides a serialization of collections List Defines methods common for lists etc... Note! You are likely to have to implement some java interfaces in upcoming assignments

Abstract Classes, Interfaces, & Inheritance A class can only extend one (1) class (abstract or concrete) A class can implement any number of interfaces Abstract classes extending abstract classes do not need to implement abstract methods in the superclass. However, concrete classes extending the derived abstract class must implement all abstract methods in both superceding abstract classes.

Interfaces can inherit, too! public interface Foo { void foomethod (); public interface Bar extends Foo { void barmethod (); public class Baz implements Bar { public void foomethod () { public void barmethod () {

Another inheritance example public class Student { public String tostring () { return "I m a student "; public class Undergrad extends Student { public String tostring () { return super. tostring () + " in college!");

I m curious... Think of our two classes Student and Undergrad In the case of: Student mystudent = new Undergrad(); How does Java know how to execute the overridden method tostring() in Undergrad when the programmer calls: mystudent.tostring(); especially in the case where the Student class was compiled before Undergrad?

Dynamic (Late) Binding Compiler puts in a flag saying Use applicable definition for method tostring() when compiling, since it doesn t know what definition it will use later. Method definition is chosen based on the current object s place in the inheritance chain, not by the type naming the current object! Even typecasts will not change this behavior: Student mystudent = (Student) new Undergrad(); mystudent.tostring(); will still call the Undergrad tostring() method.

Polymorphism the quality or state of existing in or assuming different forms In object oriented programming, the term is used to describe a variable that may refer to objects whose class is not known at compile time, and which respond at run time according to the actual class of the object to which they refer. Definitions from dictionary.com

Dynamic Binding vs. Polymorphism Sounds like the same thing... That s because it is because both phrases describe the same process but from different perspectives: Polymorphism is at the object level (for the programmer) Dynamic Binding is the compiler s way of realizing polymorphism Sometimes used interchangably. Note though, original definiton of polymorphism was only referring to type generality, but has been redefined for object oriented programming.

instanceof Can be used to see if instances were created from the same class. Useful when comparing objects - makes for a better comparison Syntax: <object> instanceof <object> Add to equals or compareto method to ensure that there s class correspondence.

Inheritance wrapup... Defines relationships between objects Allows for increasingly specialized implementations without having to reimplement (inherited methods) Enforced by Abstract methods and interfaces that define the allowable use