Computer Science 210: Data Structures

Similar documents
(SE Tutorials: Learning the Java Language Trail : Interfaces & Inheritance Lesson )

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Universiti Malaysia Perlis

Inheritance. The Java Platform Class Hierarchy

Inheritance, part 2: Subclassing. COMP 401, Spring 2015 Lecture 8 2/3/2015

Introduction and Review of Java: Part 2

Tony Valderrama, SIPB IAP 2009

ITI Introduction to Computing II

ITI Introduction to Computing II

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

Data Types. Lecture2: Java Basics. Wrapper Class. Primitive data types. Bohyung Han CSE, POSTECH

Lesson: Classes and Objects

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

Java Object Oriented Design. CSC207 Fall 2014

Defining an interface is similar to creating a new class: // constant declarations, if any

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

CS260 Intro to Java & Android 03.Java Language Basics

What is Inheritance?

C++ Important Questions with Answers

Chapter 5 Object-Oriented Programming

Java Programming Tutorial 1

Chapter 6: Inheritance

Inheritance and Polymorphism

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Inheritance -- Introduction

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

CS-202 Introduction to Object Oriented Programming

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

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

Chapter 10 Classes Continued. Fundamentals of Java

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

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

Programming overview

Lecture 2: Java & Javadoc

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

INHERITANCE. Spring 2019

Lecture 18 CSE11 Fall 2013 Inheritance

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS1150 Principles of Computer Science Objects and Classes

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

Instance Members and Static Members

Inheritance. Transitivity

Java OOP (SE Tutorials: Learning the Java Language Trail : Object-Oriented Programming Concepts Lesson )

Java Fundamentals (II)

Chapter 14 Abstract Classes and Interfaces

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

Inheritance and Polymorphism

OBJECT ORİENTATİON ENCAPSULATİON

OVERRIDING. 7/11/2015 Budditha Hettige 82

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

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

PROGRAMMING LANGUAGE 2

Advanced Placement Computer Science. Inheritance and Polymorphism

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

Inheritance (Part 2) Notes Chapter 6

Introductory Programming Inheritance, sections

What is the output of the following code segment:

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Sorting. Sorting. Selection sort

Object-Oriented Programming

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

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

Chapter 5. Inheritance

Exercise: Singleton 1

Example: Count of Points

Programming II (CS300)

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS111: PROGRAMMING LANGUAGE II

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

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Practice for Chapter 11

Example: Count of Points

CS111: PROGRAMMING LANGUAGE II

Programming II (CS300)

CMSC 132: Object-Oriented Programming II

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

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

1 Shyam sir JAVA Notes

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance, Polymorphism, and Interfaces

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

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

Chapter 9 Inheritance

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

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 6 Reflection

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

EECS 1001 and EECS 1030M, lab 01 conflict

Chapter 6 Introduction to Defining Classes

Inheritance (continued) Inheritance

Programming Languages and Techniques (CIS120)

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Subclasses, Superclasses, and Inheritance

Transcription:

Computer Science 210: Data Structures

Summary Today writing a Java program guidelines on clear code object-oriented design inheritance polymorphism this exceptions interfaces READING: GT chapter 2

Object-Oriented Design In an object-oriented language you model/design the world using classes. To create the world you instantiate classes thus creating objects. Objects respond to events and this determines how your world behaves. Each class models one part of the world. Usually in a project there is one class that creates the world---it creates the objects and starts the initial events (e.g. timer events); after that the world evolves. You model and create your project s world. Your design goals are: Robustness your world is capable of handling unexpected inputs without crashing your world recovers gracefully from errors Adaptability your world can be changed/adapted to new requirements Reusability your world is general/simple enough so that it can be re-used Note: Code sharing is good. avoids re-inventing the wheel reliable (code is debugged many times)

Design Principles To achieve the design goals, you follow a couple of principles: Abstraction distill a complicated system down to its most fundamental parts and describe it simply Encapsulation different components should NOT reveal internal details of their implementation e.g. data of an object is private (not public) one should be able to use a class by reading its interface interface of a class: the set of methods it supports e.g. read Java online docs and use the class; no need to know implementation Modularity divide the code into separate functional units

Inheritance The capability of a class to use the properties and methods of another class while adding its own functionality. A mechanism for sharing/reusing code captures similarities between classes Bike base-class/super-class MountainBike TandemBike sub-classes RoadBike A sub-class inherits all public and protected members of its parent

Example public class Bicycle { public int gear; public int speed; public Bicycle(int startspeed, int startgear) {.. public void setgear(int newvalue) {.. public void applybrake(int decrement) {.. public void speedup(int increment) {.. public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public int seatheight; // the MountainBike subclass has one constructor public MountainBike(int startheight, int startspeed, int startgear) { super(startspeed, startgear); seatheight = startheight; // the MountainBike subclass adds one method public void setheight(int newvalue) {...

Inheritance in Java Object is the highest superclass (ie. root class) of Java all other classes are subclasses (children or descendants) of Object Object class defined defined in the java.lang package; includes methods such as: hashcode() tostring() getclass() when your class does not extend any specific class, it extends Object by default

Inheritance Using inheritance When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself. Definitions A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class). Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object. Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

What You Can Do in a Subclass The inherited fields and method can be used directly You can declare new fields in the subclass that are not in the superclass You can declare new methods in the subclass that are not in the superclass You can override a method write a new method in the subclass that has the same signature as the one in the superclass you can invoke superclass method using keyword super You can write a subclass constructor invokes the constructor of the superclass by using super

Calling super in a constructor public MountainBike(int startheight, int startspeed, int startgear) { //call superclass constructor to create a Bike super(startcadence, startspeed, startgear); seatheight = startheight; Calling super in an overridden method public class Superclass { public void printmethod() { System.out.println("Printed in Superclass."); public class Subclass extends Superclass { public void printmethod() { //overrides printmethod in Superclass super.printmethod(); System.out.println("Printed in Subclass"); public static void main(string[] args) { Subclass s = new Subclass(); s.printmethod();

this within a method this refers to the current object Used when a field is shadowed by a method or constructor parameter. public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; but it could have been written like this: public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y;

this Using this with a Constructor From within a constructor, you can use this keyword to call another constructor in the same class (doing so is called an explicit constructor invocation) public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 0, 0); public Rectangle(int width, int height) { this(0, 0, width, height); public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height;...

Casting objects a MountainBike is a Bike a MountainBike is also an Object a Bike is not (necessarily) a MountainBike Bike MountainBike TandemBike RoadBike In Java: A variable of type T can be of type T or of any subclass of T Example Object bike; //bike is allowed to be any subclass of Object bike = new MountainBike(); We ll use this by defining data structures that work generically with Objects; we ll create the data structure on any type of objects. this is called casting: changing the type of an object Implicit casting in an inheritance hierarchy: a subclass can be used in place of a superclass

Casting examples Bike b; MountainBike mb; mb = new MountainBike(..); //implicit casting of a MountainBike to a Bike b = mb; class Person { //any person has a bike Bike b; void Person(Bike b) { this.b = b; a person that owns a bike... MountainBike mb = new MountainBike(); Person p = new Person(mb); a mountainbike is a bike

Interfaces An interface is a collection of method signatures (with no bodies) similar to a class public interface OperateCar { // method signatures int turn(direction direction, double radius,); int changelanes(direction direction, double startspeed, double endspeed); int signalturn(direction direction, boolean signalon);... When a class implements an interface it must implement all methods in that interface public class OperateBMW760i implements OperateCar { int signalturn(direction direction, boolean signalon) { //code to turn BMW's LEFT turn indicator lights on //code to turn BMW's LEFT turn indicator lights off //code to turn BMW's RIGHT turn indicator lights on //code to turn BMW's RIGHT turn indicator lights off // other members, as needed

Interfaces Interfaces are used to describe the functionality of a software in an abstract way (since methods have no bodies) Advantage: the implementation can change while interface remains the same multiple implementations E.g., a digital image processing library writes its classes to implement an interface, and publishes its interface (API-application programming interface) the implementation of the methods is usually not disclosed moreover, it can change a graphics package may decide to use this library only needs to know the API Interfaces in Java a class can inherit from a SINGLE class a class can implement many interfaces