One of these "compartments" is more correctly referred to as an element of the array

Similar documents
In our classes, we've had

INHERITANCE AND EXTENDING CLASSES

Inheritance and Interfaces

Arrays Classes & Methods, Inheritance

Java Object Oriented Design. CSC207 Fall 2014

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

ITI Introduction to Computing II

ITI Introduction to Computing II

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

Instance Members and Static Members

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Programming Exercise 14: Inheritance and Polymorphism

CS-202 Introduction to Object Oriented Programming

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

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Making New instances of Classes

The action of the program depends on the input We can create this program using an if statement

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

QUESTIONS FOR AVERAGE BLOOMERS

JAVA MOCK TEST JAVA MOCK TEST II

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

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

Introduction to Programming Using Java (98-388)

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Lecture 2: Java & Javadoc

CS1150 Principles of Computer Science Objects and Classes

Inheritance, and Polymorphism.

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

Inheritance. Transitivity

CS 251 Intermediate Programming Inheritance

1 Shyam sir JAVA Notes

Collections, Maps and Generics

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

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

Software and Programming 1

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

VIRTUAL FUNCTIONS Chapter 10

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

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

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Another IS-A Relationship

Chapter 5 Object-Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

What happens if someone new wants to join our awesome club?

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

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

CSC 1214: Object-Oriented Programming

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

1.00 Lecture 13. Inheritance

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

C++ Important Questions with Answers

Readings for This Lecture

Programming overview

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

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

Creating Java Programs with Greenfoot

Rules and syntax for inheritance. The boring stuff

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

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

Java Fundamentals (II)

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

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

PROGRAMMING LANGUAGE 2

Example: Count of Points

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Exercise: Singleton 1

CSE 113 A. Announcements - Lab

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CH. 2 OBJECT-ORIENTED PROGRAMMING

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Example: Fibonacci Numbers

M255 Object Oriented Programming with Java

Topic 7: Algebraic Data Types

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

Inheritance (an intuitive description)

Principles of Object Oriented Programming. Lecture 4

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

The Java language has a wide variety of modifiers, including the following:

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Declarations and Access Control SCJP tips

APCS Semester #1 Final Exam Practice Problems

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

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

CSCI 355 Lab #2 Spring 2007

Distributed Systems Recitation 1. Tamim Jabban

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Options for User Input

ENCAPSULATION AND POLYMORPHISM

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

CSE 341, Autumn 2015, Ruby Introduction Summary

CS 116 Week 8 Page 1

Chapter 14 Abstract Classes and Interfaces

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Transcription:

An array is a special type of variable in that it can contain many values If a standard variable is like a box, think of an array as being like a box with compartments: One of these "compartments" is more correctly referred to as an element of the array Each element has a unique number (or index) In most programming languages element indexes start at 0

Arrays store a set of objects in elements Arrays in Java are actual objects Arrays can contain any type of element value (primitive or objects) but a single array must contain elements of the same type (although you could have an array of Object) To declare an array: declare an array variable create an array object and assign it to this variable store things in the array elements 3

To declare an array variable: // int array int[] banana; This declares an array of integers, arr: Note that the number of integers in the array is not specified at this stage. Introduction 4

To Create an Array Object Use the new operator int[] arr; arr = new int[3]; or int[] arr = new int[3]; create array of 3 ints: arr[0], arr[1], arr[2] Can be combined in one statement 5

If a variable is like a box, then an array is like a box with numbered compartments String[] box = new String[5]; 6

Place elements into a compartment of the array by specifying the compartment number: box[3] = xyz ; 7 xyz Until you assign something to an array element, it will contain the default value for that data type or class Primitive data types have default values google "default primitive values in java" Arrays of objects (i.e. that have a type that is a class) contain a default value of null

You can combine declaration, creation and initialisation in one statement: // 3 ints int[] arr = 15,3,56; // 3 strings String[] strs = Paul, Fred, Bill ; 8

You can get the length of an array with.length int[] arr = 1,6,8,24; arr.length would be 4 We could use that in a for loop: for (int i = 0; i < arr.length; i++) System.out.println(arr[i]); 9

Or, we can use an alternative loop construct called a for/each loop: Verbal equivalent: For each element in an array Syntax example (assuming the array arr from the previous slide) for (int single : arr) System.out.println(single); 10 The loop iterates once for each element in arr The element is copied into a variable (single in this case) Then we can do something with single

We can have an array of objects Ball[] ballsonasnookertable = new Ball[22]; ballsonasnookertable[0] = new Ball(); ballsonasnookertable[0].setcolour( white ); and so on! 11

Remember the House has a Garden exercise a couple of weeks ago? What if a house could have many gardens? We could make the garden attribute in house an array of type Garden A house could then have several gardens 12

public class House public int bedrooms; public Garden[] garden; // rest of class 13

1. There is no relationship between Foo and Bar 2. Foo has one single Bar 3. Bar has one single Foo 4. Foo has one or multiple Bars 5. Bar has one or multiple Foos 6. Both 4 and 5 7. Paul has found yet another innovative way to cock up an orange slide public class Foo private int bar; private Bar[] moo; // getters and setters assumed public class Bar private String[] foo; private int moo; // getters and setters assumed 8% 85% 5% 0% 0% 1% 1% 1. 2. 3. 4. 5. 6. 7.

1. 95% 2. 3. 4. 4% 0% 1% 1. 2. 3. 4. public class Wibble 1 private int bibble; private Flibble[] nibble; public class Flibble 3 private int bar; private Wibble[] nibble; // getters and setters assumed // getters and setters assumed public class Wibble 2 private int bar; private Flibble nibble; public class Flibble private int bar; private Wibble nibble; 4 // getters and setters assumed // getters and setters assumed

Inheritance is an OO technique that lets you use an existing class as the basis for a new class The existing class is called the base class, superclass or parent class The new class is called the subclass or child class The subclass inherits all the methods and attributes of the superclass 17

We can have different instances of ball They all have a colour They all have one diameter But what about if they aren't quite the same as every other ball? 18

A rugby ball has the same attributes as a "normal" ball It has a colour It has a diameter However, it has a SECOND, additional diameter attribute which we ll call seconddiameter Colour: white Diameter: 27cm Second Diameter: 19cm Second Diameter Diameter 19

We can use inheritance to create a new class, OvalBall OvalBall will extend Ball This means it will inherit all of its existing attributes and methods We can then add additional attributes and methods to the subclass OvalBall that get added to the ones that have been inherited 20

public class Ball private double diameter; private String colour; public void setdiameter(double d) this.diameter = d; // assume other methods follow // including constructors // and getter/setters... 21 public class OvalBall extends Ball private double seconddiameter; public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here public class Main public static void main(string[] a) OvalBall rugby = new OvalBall(); rugby.setdiameter(27); rugby.setseconddiameter(19);

public class Ball private double diameter; private String colour; public void setdiameter(double d) this.diameter = d; // assume other methods follow // including constructors // and getter/setters... OvalBall inherits setdiameter from Ball public class OvalBall extends Ball private double seconddiameter; public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here public class Main public static void main(string[] a) OvalBall rugby = new OvalBall(); rugby.setdiameter(27); rugby.setseconddiameter(19); 22

public class Ball private double diameter; private String colour; public void setdiameter(double d) this.diameter = d; // assume other methods follow // including constructors // and getter/setters... OvalBall s own attributes and methods are combined with those from the superclass, Ball public class OvalBall extends Ball private double seconddiameter; public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here public class Main public static void main(string[] a) OvalBall rugby = new OvalBall(); rugby.setdiameter(27); rugby.setseconddiameter(19); 23

We can replace an existing method from the superclass with a new or altered version in the subclass Consider our OvalBall Ball has a method bounce that returns the height a ball bounces based on its diameter when an OvalBall bounces, would it bounce the same way as a normal ball would? 24

Let s say for the sake of argument that an OvalBall has a 50/50 chance of landing on the flat or the pointy end 25

if it lands on the pointy end, it should use diameter to calculate the bounce height if it lands on the flat end, it should use seconddiameter to calculate the bounce height 26

public double bounce() We can supply a version of bounce in OvalBall that will override the one from the superclass if (Math.random() > 0.5) else return this.getdiameter() * 2; return this.getseconddiameter() * 2;

You can use super to refer to the superclass from the subclass So, we might write our bounce method in OvalBall like this public double bounce if (Math.random() > 0.5) return super.bounce(); else return this.getseconddiameter() * 2; 28

You can use super to refer to the superclass from the subclass So, we might write our bounce method in OvalBall like this public double bounce if (Math.random() > 0.5) return super.bounce(); else return this.getseconddiameter() * 2; 29

A superclass will fit into a subclass, e.g. Ball rugbyball = new OvalBall(); but not the other way round OvalBall rugbyball = new Ball(); 30

But we could do Ball firstball = new Ball(); Ball secondball = new Ball(); Ball thirdball = new OvalBall(); // below will call bounce from Ball secondball.bounce(); // below will call bounce from OvalBall thirdball.bounce(); This is called dynamic polymorphism 31

If your superclass does not have the default (parameterless) constructor, then you MUST have a constructor in your subclass that uses super 32

public class Ball private double diameter; private String colour; public Ball(double d, String c) this.diameter = d; this.colour = c; public class OvalBall extends Ball private double seconddiameter; public void setseconddiameter(double sd) this.seconddiameter = sd; // assume other methods follow // including constructors // and getter/setters... // rest of class methods etc here 33

public class Ball private double diameter; private String colour; public Ball(Double d, String c) this.diameter = d; this.colour = c; public class OvalBall extends Ball private double seconddiameter; public void setseconddiameter(double sd) this.seconddiameter = sd; // assume other methods follow // including constructors // and getter/setters... // rest of class methods etc here 34

public class Ball private double diameter; private String colour; public Ball(double d, String c) this.diameter = d; this.colour = c; // assume other methods follow // including constructors // and getter/setters... public class OvalBall extends Ball private double seconddiameter; public OvalBall(double d, String c, double sd) super(d,c); this.seconddiameter = sd; public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here 35

public class Ball private double diameter; private String colour; public Ball(double d, String c) this.diameter = d; this.colour = c; // assume other methods follow // including constructors // and getter/setters... public class OvalBall extends Ball private double seconddiameter; public OvalBall(double d, String c, double sd) Call the superclass s constructor super(d,c); with these parameters this.seconddiameter = sd; public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here 36

public class Ball private double diameter; this.colour = c; // assume other methods follow // including constructors // and getter/setters... public class OvalBall extends Ball private double seconddiameter; private String colour; public OvalBall(double d, String c, double sd) public Ball(double d, String c) super(d,c); this.seconddiameter = sd; this.diameter = d; The superclass s constructor only knows about and sets diameter and colour public void setseconddiameter(double sd) this.seconddiameter = sd; // rest of class methods etc here 37

public class Ball private double diameter; private String colour; public Ball(double d, String c) this.diameter = d; this.colour = c; // assume other methods follow // including constructors // and getter/setters... public class OvalBall extends Ball private double seconddiameter; public OvalBall(double d, String c, double sd) super(d,c); this.seconddiameter = sd; so after the call to the superclass s constructor, we public void setseconddiameter(double sd) return to the subclass s constructor and set the parameter(s) that are specific to the subclass this.seconddiameter = sd; // rest of class methods etc here 38

A triangular arrow signifies that the relationship between two classes involves inheritance Ball -diameter -colour +bounce() +roll() The triangle goes at the superclass end We also refer to this as an is a relationship i.e. OvallBall is a Ball OvalBall -seconddiameter +bounce() +roll() 39

Consider a university system Lecturer Administrator

1. There is no relationship between Googoo and Gaga 2. Googoo is a Gaga 3. Googoo has one single Gaga 4. Googoo has many Gagas 5. Gaga has one single Googoo 6. Gaga has many Googoos 7. Gaga is a Googoo 8. Paul has found yet another innovative way to cock up an orange slide public class Googoo extends Gaga private int meep; private String[] gaga; // getters and setters assumed public class Gaga private String mope; private int googoo; // getters and setters assumed 4% 63% 3% 16% 0% 5% 10% 0% 1. 2. 3. 4. 5. 6. 7. 8.

If a variable is like a box, then an array is like a box with compartments Declare an array with int[] arr = new int[3]; or int[] arr = 1,7,9,2,5,20; Get the length of an array with.length You can iterate (loop) through an array with the for/each loop Attributes in a class can be of type array 42..this is one way to represent the a has-a relationship when there are several of the same objects involved in the attribute, e.g. A university has many students Student[] students A house has several gardens Garden[] gardens

Inheritance lets us create new classes by using an existing one as a base The existing class that we use is called the superclass The new class we create (using the superclass as a base) is called the subclass We use the Java keyword extends to specify that a new class uses an existing one as its superclass The subclass inherits all of the methods and attributes of the superclass If we add any new methods or attributes in the subclass, these are added alongside to the inherited ones from the superclass 43

You can replace an existing superclass method in the subclass This is called overriding a method You can call methods in the superclass from the subclass with super If you have constructors in your superclass, and one of them is not the default (parameterless) constructor.. any subclasses MUST include a constructor that calls the superclass s constructor via super 44