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

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Inheritance (Part 5) Odds and ends

CLASS DESIGN. Objectives MODULE 4

INHERITANCE. Spring 2019

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

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

Distributed Systems Recitation 1. Tamim Jabban

Java Magistère BFA

More about inheritance

Chapter 5 Object-Oriented Programming

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

The class Object. Lecture CS1122 Summer 2008

Super-Classes and sub-classes

Methods Common to all Classes

Distributed Systems Recitation 1. Tamim Jabban

Practice for Chapter 11

Abstract Classes and Interfaces

C12a: The Object Superclass and Selected Methods

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

Introduction to Inheritance

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

Inheritance. Transitivity

CMSC 132: Object-Oriented Programming II

CS-202 Introduction to Object Oriented Programming

Inheritance -- Introduction

Check out Polymorphism from SVN. Object & Polymorphism

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Java Fundamentals (II)

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

COE318 Lecture Notes Week 8 (Oct 24, 2011)

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

Java Classes, Inheritance, and Interfaces

Language Features. 1. The primitive types int, double, and boolean are part of the AP

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

More on Objects in JAVA TM

Lecture 4: Extending Classes. Concept

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Chapter 11: Collections and Maps

Rules and syntax for inheritance. The boring stuff

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Making New instances of Classes

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

What is Inheritance?

type conversion polymorphism (intro only) Class class

Programming II (CS300)

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Objects and Iterators

CO Java SE 8: Fundamentals

CS260 Intro to Java & Android 03.Java Language Basics

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

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

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

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

TeenCoder : Java Programming (ISBN )

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

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

Introduction to Programming Using Java (98-388)

CIS 110: Introduction to computer programming

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

Programming II (CS300)

Generics method and class definitions which involve type parameters.

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

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

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

C++ Important Questions with Answers

CompuScholar, Inc. 9th - 12th grades

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

25. Generic Programming

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

Inheritance. The Java Platform Class Hierarchy

Lecture 18 CSE11 Fall 2013 Inheritance

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

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Class, Variable, Constructor, Object, Method Questions

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

Inheritance and Polymorphism

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Polymorphism and Inheritance

JAVA MOCK TEST JAVA MOCK TEST II

C09: Interface and Abstract Class and Method

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

Full file at Chapter 2 - Inheritance and Exception Handling

Overriding Variables: Shadowing

Implements vs. Extends When Defining a Class

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

ITI Introduction to Computing II

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

Declarations and Access Control SCJP tips

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

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

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Announcements/Follow-ups

Transcription:

Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy

Some Reminders Interfaces can require classes to provide specific methods a constructor initializes new objects of the given class tostring method is invoked automatically by Java when needed this and super fields are defined in every class

Method overriding a class overrides a method when it provides another implementation of a method that its superclass already provides examples: public String tostring() public boolean equals(object obj)

Method overloading a class overloads a method when it provides another method of the same name but with a different signature examples: public string tostring() public String tostring(int numberofelements) public String tostring(double maxvalue)

Polymorphism polymorphism means ``many types polymorphism allows operations to be implemented on the most general possible type (the highest superclass) that supports the operation For example: private double sum(number a, Number b) { } return a.doublevalue() + b.doublevalue(); double x = sum(new Double(1.3), new Integer(2)); with autoboxing, the last line can be written as: double x = sum(1.3, 2);

Polymorphism and Type conversion A variable of a superclass type can always refer to an object of a subclass type: Object o = new String("hello world"); assigning an object of a superclass type to a variable of a subclass type requires a cast, and may throw a ClassCastException: String s = (String) o;

Abstract Classes Reminder: an interface can be used to specify that a class provides certain methods a class can implement any number of interfaces a class can have only one superclass if that superclass is abstract, that means that the superclass itself cannot be instantiated if an abstract class has abstract methods, those methods must be implemented by the subclass Number is an abstract class

Abstract Class Example public abstract class AgriculturalPlant { protected double plantsize; // cannot be instantiated, so no constructor // non-abstract (i.e., implemented) methods may appear in an abstract class public void grow(double fraction) { } plantsize = plantsize * (1.0 + fraction); // abstract methods must be implemented by the subclass(es) } public abstract double harvest(); // returns weight public abstract void fertilize(double fertilizeramount);

Class Object class Object is the superclass of every object class in Java, even if not extended explicitly useful methods include: boolean equals(object obj); String tostring(); int hashcode(); Class<?> getclass(); hashcode() will be used later in this course, when hash tables are discussed

Class Accessor Methods getclass() can be used wih equals to test whether two objects are in the same class. For example, public class Foo { public boolean equals(object obj) { if (this.getclass()!= obj.getclass()) { }... return false; instanceof can also be used to test class membership, e.g. Integer x = new Integer (99); if (x instanceof Number) {...

Exception Class Hierarchy Throwable is a subclass of Object Exception is a subclass of Throwable many exceptions are subclasses of Exception RuntimeException is also a subclass of Exception many common exceptions are subclasses of RuntimeException

RuntimeException exceptions that are subclasses of Exception (but not of RuntimeException) are checked exceptions: if a method may throw such an exception, the method's declaration must say so runtime exceptions need not be declared, e.g. public String readfoo() throws java.io.filenotfoundexception { // this constructor may throw java.io.filenotfoundexception java.io.fileinputstream foo = new java.io.fileinputstream("foo"); java.util.scanner s = new java.util.scanner (foo); // s.nextline may throw NoSuchElementException or IllegalStateException, // both of which are subclasses of RuntimeException, so need not be declared } return s.nextline();