Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Similar documents
Java and OOP. Part 2 Classes and objects

Java Programming Lecture 7

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Java Object Oriented Design. CSC207 Fall 2014

INHERITANCE. Spring 2019

Object-Oriented Concepts

Inheritance and Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

CS111: PROGRAMMING LANGUAGE II

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Full file at Chapter 2 - Inheritance and Exception Handling

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

Advanced Placement Computer Science. Inheritance and Polymorphism

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

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

1.00 Lecture 13. Inheritance

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CMSC 132: Object-Oriented Programming II. Inheritance

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

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

More on Objects in JAVA TM

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

ITI Introduction to Computing II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Classes and Inheritance Extending Classes, Chapter 5.2

BBM 102 Introduction to Programming II Spring Abstract Classes and Interfaces

CS111: PROGRAMMING LANGUAGE II

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

CS-202 Introduction to Object Oriented Programming

Inheritance Motivation

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

OVERRIDING. 7/11/2015 Budditha Hettige 82

Polymorphism CSCI 201 Principles of Software Development

ITI Introduction to Computing II

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

The Essence of OOP using Java, Nested Top-Level Classes. Preface

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

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

Chapter 10 Classes Continued. Fundamentals of Java

Programming in C# Inheritance and Polymorphism

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

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

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance, Polymorphism, and Interfaces

CMSC 132: Object-Oriented Programming II

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

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

JAVA Programming Language Homework I - OO concept

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

Chapter 5. Inheritance

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Class, Variable, Constructor, Object, Method Questions

6.170 Recitation #5: Subtypes and Inheritance

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

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

BBM 102 Introduction to Programming II Spring 2017

Introduction to Object-Oriented Programming

8.1 Inheritance. 8.1 Class Diagram for Words. 8.1 Words.java. 8.1 Book.java 1/24/14

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

Inheritance (Part 5) Odds and ends

CS111: PROGRAMMING LANGUAGE II

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

PROGRAMMING LANGUAGE 2

Chapter 14 Abstract Classes and Interfaces

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance and object compatibility

Inheritance -- Introduction

CS 113 PRACTICE FINAL

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming 2015/16. Final Exam June 28, 2016

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Comp 249 Programming Methodology

Rules and syntax for inheritance. The boring stuff

Inheritance 2. Inheritance 2. Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria

CS 11 java track: lecture 3

National University. Faculty of Computer Since and Technology Object Oriented Programming

Fundamentals of Object Oriented Programming

CH. 2 OBJECT-ORIENTED PROGRAMMING

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

What is Inheritance?

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

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Java Fundamentals (II)

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

Questions Answer Key Questions Answer Key Questions Answer Key

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Name: CS 159 Practice Final Fall 2015

Name: CS 159 Practice Final Fall 2015

Reusing Classes. Hendrik Speleers

Transcription:

Java and OOP Part 3 Extending classes OOP in Java : W. Milner 2005 : Slide 1

Inheritance Suppose we want a version of an existing class, which is slightly different from it. We want to avoid starting again from scratch We can define the new class to be a sub-class of the first class. OOP in Java : W. Milner 2005 : Slide 2

Terms used The original class is called the base class, the ancestor class or the super class The process of designing the sub-class from the base class is called 'sub-classing' the base class OOP in Java : W. Milner 2005 : Slide 3

Sub-classing The subclass inherits all members and methods of the first where needed we can write new versions of inherited methods which replace the old method we can add extra members and methods You can t loose a member or method No limit to levels of inheritance OOP in Java : W. Milner 2005 : Slide 4

Example Common type of sub-classing is specialization Suppose we want a type of product which is perishable When we deliver new stock, we throw away old stock not add to it First review the Product class: OOP in Java : W. Milner 2005 : Slide 5

public class Product public Product() lastbarcodeused++; barcode=lastbarcodeused; stocklevel=100; public Product(int initstock) lastbarcodeused++; barcode=lastbarcodeused; stocklevel=initstock; Product class definition public static int count() return lastbarcodeused; public void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); OOP in Java : W. Milner 2005 : Slide 6

Rest of Product public int getstocklevel() return stocklevel; public void deliver(int howmany) if (howmany<0) System.out.println("Invalid delivery"); return; else stocklevel+=howmany; private static int lastbarcodeused=0; private int barcode; protected int stocklevel; OOP in Java : W. Milner 2005 : Slide 7

Implementation of Perishable public class Perishable extends Product public void deliver(int howmany) stocklevel=howmany; OOP in Java : W. Milner 2005 : Slide 8

Protected Problem the deliver method in Perishable references the private field stocklevel not allowed Solution use access control modifier protected Excerpt from modified Product definition.. private static int lastbarcodeused=0; private int barcode; protected int stocklevel; OOP in Java : W. Milner 2005 : Slide 9

public class First public static void main(string[] args) Product prod = new Product(); prod.deliver(100); Perishable perish1 = new Perishable(); Perishable perish2 = new Perishable(); perish1.deliver(50); perish2.deliver(60); prod.display(); perish1.display(); perish2.display(); Using the subclass All 3 use default constructor =stocklevel 100 Barcode = 1 Stocklevel = 200 ==================== Barcode = 2 Stocklevel = 50 ==================== Barcode = 3 Stocklevel = 60 ==================== OOP in Java : W. Milner 2005 : Slide 10

Constructors of sub-classes Constructors are not methods They are not inherited If you don't define one the no-arg constructor of the base class is called for you see last example OOP in Java : W. Milner 2005 : Slide 11

super() super(); can be used as the first statement in a constructor It means the corresponding superclass constructor is called Further statements can take further action For example..suppose Perishable products have an extra store location code.. OOP in Java : W. Milner 2005 : Slide 12

Using super() public Perishable(int initstock, int initlocationcode) super(initstock); locationcode = initlocationcode; public Product(int initstocklevel) barcode=lastbarcodeused++; stocklevel=initstocklevel; OOP in Java : W. Milner 2005 : Slide 13

More on super() super() cannot be anywhere except the first line of a constructor If you don t use super(), the system executes it anyway IOW a subclass constructor first executes the no-arg constructor of the super class OOP in Java : W. Milner 2005 : Slide 14

Exercise Define an Employee class, with fields payrollnumber and rateofpay Define a Manager class as a sub-class of Employee. They are paid monthly define their pay() method to display their pay Define a Clerical class as a sub-class of Employee. They are hourly paid. Add an hoursworked field, and a pay() method. OOP in Java : W. Milner 2005 : Slide 15

Object All classes descend from the class Object public class MyClass.. Is in effect: public class MyClass extends Object.. While if you say public class MyClass extends MySuperClass Then MySuperClass, or its ancestor, descends from Object Object objects have few useful methods Except tostring(), which converts the object to a descriptive string Which is what System.out.println calls For example.. OOP in Java : W. Milner 2005 : Slide 16

Object example Object someobject= new Object(); System.out.println(someObject); Output: java.lang.object@187c6c7 OOP in Java : W. Milner 2005 : Slide 17

Changing and using tostring In Perishable definition.... public String tostring() return "Perishable ID="+barcode+" Loc="+locationCode+" stock="+stocklevel;.. in use.. Perishable p1 = new Perishable(20,30); Perishable p2 = new Perishable(20,45); System.out.println(p1); System.out.println(p2); Output: Perishable ID=0 Loc=30 stock=20 Perishable ID=1 Loc=45 stock=20 calls tostring() of Perishable objects OOP in Java : W. Milner 2005 : Slide 18

final methods A method declared as final in a superclass cannot be altered in a subclass For example.. OOP in Java : W. Milner 2005 : Slide 19

Defining a method as final In Product.. public final void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); In Perishable.. public void display().... In use: Perishable p1 = new Perishable(20,30); Output on next slide OOP in Java : W. Milner 2005 : Slide 20

Trying to override final - compile time error C:\Walter\JavaProgs\Perishable.java:19: display() in Perishable cannot override display() in Product; overridden method is final public void display() ^ 1 error OOP in Java : W. Milner 2005 : Slide 21

Final classes and why A class declared as final cannot be subclassed Methods and classes are usually declared as final for security Otherwise a subclass of a standard superclass might be defined, with.. Unpleasant overridden methods But at run-time a subclass object would look like the superclass Eg the String class is final for this reason OOP in Java : W. Milner 2005 : Slide 22

Abstract classes Superclasses which are 'general' can be declared abstract Used when subclasses will all implement the same method in different ways, but The superclass is too general to actually implement the method itself For example.. OOP in Java : W. Milner 2005 : Slide 23

Example abstract class Suppose we had a superclass called Shape And subclasses called Triangle, Rectangle, Square and so on. Each would need a draw method But we could not program the draw method of a Shape instance So the draw method of Shape is declared abstract As is the class as a whole This means Shape cannot be instantiated OOP in Java : W. Milner 2005 : Slide 24

Example abstract class public abstract class Shape public Shape(int initheight, int initwidth) width=initwidth; height=initheight; public abstract void draw(); protected int width; protected int height; OOP in Java : W. Milner 2005 : Slide 25

public class Rectangle extends Shape public Rectangle(int h, int w) super(h,w); Subclass of abstract class public void draw() for (int i=0; i<height; i++) for (int j=0; j<width; j++) System.out.print("*"); System.out.println(); OOP in Java : W. Milner 2005 : Slide 26

Using the subclass Rectangle r = new Rectangle(4,5); r.draw(); OOP in Java : W. Milner 2005 : Slide 27

Copy the Shape class Exercise Define a Triangle class by subclassing Shape define the draw method How would you deal with a Square class? OOP in Java : W. Milner 2005 : Slide 28