Distributed Systems Recitation 1. Tamim Jabban

Similar documents
Distributed Systems Recitation 1. Tamim Jabban

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

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

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

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

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

Abstract Classes and Interfaces

Inheritance (Part 5) Odds and ends

Darrell Bethea June 6, 2011

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Java Classes, Inheritance, and Interfaces

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

Data Structures. BSc in Computer Science University of New York, Tirana. Assoc. Prof. Marenglen Biba 1-1

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

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

COE318 Lecture Notes Week 8 (Oct 24, 2011)

C12a: The Object Superclass and Selected Methods

Inheritance. Transitivity

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

Notes on Chapter Three

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Week 5-1: ADT Design

Exploring the Java API, Packages & Collections

Introduction to Programming Using Java (98-388)

Chapter 5 Object-Oriented Programming

Java Fundamentals (II)

Inheritance (Outsource: )

Java Persistence API (JPA) Entities

Java Magistère BFA

Software and Programming 1

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Informatik II (D-ITET) Tutorial 6

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Lecture 2: Java & Javadoc

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

CMSC 341. Nilanjan Banerjee

Inheritance and Polymorphism

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

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

University of Palestine. Mid Exam Total Grade: 100

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

Inf1-OOP. Inheritance and Interfaces. Ewan Klein, Perdita Stevens. January 12, School of Informatics

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

Fundamental Java Methods

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

Programming II (CS300)

The class Object. Lecture CS1122 Summer 2008

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

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

Overview of Eclipse Lectures. Module Road Map

CS 1331 Exam 1 ANSWER KEY

Learn more about our research, discover data science, and find other great resources at:

Programming II (CS300)

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Object Oriented Programming: Based on slides from Skrien Chapter 2

Inheritance and Interfaces

Informatik II Tutorial 6. Subho Shankar Basu

CMSC 132: Object-Oriented Programming II. Inheritance

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

What is Inheritance?

Super-Classes and sub-classes

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

CS 251 Intermediate Programming Inheritance

Create a Java project named week10

What are Generics? e.g. Generics, Generic Programming, Generic Types, Generic Methods

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

Rules and syntax for inheritance. The boring stuff

Sorting. Sorting. Selection sort

AP CS Unit 6: Inheritance Notes

ECE 122. Engineering Problem Solving with Java

Java Generics -- an introduction. Based on

Collections, Maps and Generics

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

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

CO Java SE 8: Fundamentals

type conversion polymorphism (intro only) Class class

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

First IS-A Relationship: Inheritance

More about inheritance

Recitation 3 Class and Objects

Principles of Object Oriented Programming. Lecture 4

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

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

CS 0401 Fall 2017 Quiz 2 Version A SOLUTIONS. logical size of an ArrayList is the number of items being stored in the array.

Questions Answer Key Questions Answer Key Questions Answer Key

Lecture 10. Overriding & Casting About

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

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

Collections Algorithms

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Chapter 5: Classes and Objects in Depth. Information Hiding

Announcements. Final exam. Course evaluations. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA

ITI Introduction to Computing II

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

Inheritance CSC9Y4. Inheritance

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

Transcription:

15-440 Distributed Systems Recitation 1 Tamim Jabban

Office Hours Office 1004 Sunday, Tuesday: 9:30-11:59 AM Appointment: send an e-mail Open door policy

Java: Object Oriented Programming A programming paradigm based on objects An example of an Object template: public class Animal {

Java: Object Oriented Programming A programming paradigm based on objects An Object can contain data/attributes/fields: public class Animal { String name; Integer age;

Java: Object Oriented Programming A programming paradigm based on objects An Object can contain methods (behavior): public class Animal { String name; public String getname() { return name;

Java: Object Oriented Programming A programming paradigm based on objects To create an Animal Object: Animal bear = new Animal();

Constructors Constructors take in zero or more variables to create an Object: public class Animal { String name; int age; public Animal () { Constructor Animal bear = new Animal();

Constructors Constructors take in zero or more variables to create an Object: public class Animal { String name; int age; public Animal(String name, int sage) { name = name; age = sage; Problem!

Constructors Constructors take in zero or more variables to create an Object: public class Animal { String name; int age; public Animal(String name, int sage) { this.name = name; age = sage; Animal bear = new Animal( Bear, 21 );

Inheritance Enables one object to inherit methods (behavior) and attributes from another object. For example, a Cat class can extend an Animal class: public class Cat extends Animal { int numofmugspushed; String favoritehuman; Cat inherits name, age & getname from Animal.

Inheritance : Class Hierarchy This introduces subclasses and superclasses. A class that inherits from another class is called a subclass: Cat inherits from Animal, and therefore Cat is a subclass. The class that is inherited is called a superclass: Animal is inherited, and is the superclass. Animal Cat Snake

Inheritance Organizes related classes in a hierarchy: This allows reusability and extensibility of common code Subclasses extend the functionality of a superclass Subclasses inherit all the methods of the superclass (excluding constructors and privates) Subclasses can override methods from the superclass (more on this later)

Java Workspace Hierarchy Project name Package name Class name

Access Control Access modifiers describe the accessibility (scope) of data like: Attributes: public String name; Methods: public String getname() { Constructers: private Student(String name, int sage) {

Access Control Access modifiers include: Public Protected Private

Access Control Access modifiers include: Public Protected Private

Access Control Access modifiers include: Public: Allows the access of the object/attributes/methods from any other program that is using this object: public class Animal { public void setname(string newname) { this.name = newname; public class Test { public static void main(string[] args) { Animal foobar = new Animal(); foobar.setname( Foo Bar );

Access Control Access modifiers include: Public Protected Private

Access Control Access modifiers include: Protected: You can use this only in the following Same class as the variable, Any subclasses of that class, Or classes in the same package. A package is a group of related classes that serve a common purpose.

Access Control Access modifiers include: Public Protected Private

Access Control Access modifiers include: Private: Restricted even further than a protected variable: you can use it only in the same class: public class Animal { private void setname(string newname) { this.name = newname; public Student(String name) { setname(name); public class Test { public static void main(string[] args) { Animal foobar = new Animal(); foobar.setname( Foo Bar ); // Not accessible anymore!

Object & Class Variables Each Animal object has its own name, age, etc name and age are examples of Object Variables. When an attribute should describe an entire class of objects instead of a specific object, we use Class Variables (or Static Variables).

Object & Class Variables A Class Variable Example: public class Animal { public static final String currentplanet = EARTH ; public class Test() { public static void main(string[] args) { Animal foobar = new Animal(); String planet = foobar.currentplanet;

Object & Class Variables A Class Variable Example: public class Animal { public static final String currentplanet = EARTH ; public class Test() { public static void main(string[] args) { String planet = Animal.currentPlanet;

Encapsulation Encapsulation is restricting access to an object s components. How can we change or access name now?: public class Animal { private public String name; private public int age; Animal foobar = new Animal();

Encapsulation Encapsulation is restricting access to an object s components. Using getters and setters: public class Animal { private String name; private int age; public void setname(string newname) { this.name = newname; public String getname() { return name; Animal foobar = new Animal(); foobar.setname( Foo Bar );

Overloading Methods Methods overload one another when they have the same method name but: The number of parameters is different for the methods The parameter types are different Example: public void changedate(int year) { // do cool stuff here public void changedate(int year, int month) { // do cool stuff here

Overloading Methods Methods overload one another when they have the same method name but: The number of parameters is different for the methods The parameter types are different Another Example: public void addsemestergpa(float newgpa) { // process newgpa public void addsemestergpa(double newgpa) { // process newgpa

Overloading Methods Methods overload one another when they have the same method name but: The number of parameters is different for the methods The parameter types are different Another Example: public void changedate(int year) { // do cool stuff here public void changedate(int month) { // do cool stuff here

Overloading Methods Methods overload one another when they have the same method name but: The number of parameters is different for the methods The parameter types are different Another Example: public void changedate(int year) { // do cool stuff here public void changedate(int month) { // do cool stuff here We can t overload methods by just changing the parameter name!

Overriding Methods Example: public class ClassA { public Integer somemethod() { return 3; public class ClassB extends ClassA { // this is method overriding: public Integer somemethod() { return 4;

Object Overriding Methods Any class extends the Java superclass Object. The Java Object class has 3 important methods: public boolean equals(object obj); public int hashcode(); public String tostring(); Animal Car The hashcode is just a number that is generated by any object: It shouldn t be used to compare two objects! Instead, override the equals, hashcode, and tostring methods.

Overriding Methods Example: Overriding the tostring and equals methods in our Animal class: public class Animal { public String tostring() { return this.name;

Overriding Methods Example: Overriding the tostring and equals methods in our Animal class: public class Animal { public boolean equals(object obj) { if (obj.getclass()!= this.getclass())) return false; else { Animal s = (Animal) obj; return (s.getname().equals(this.name));

Abstract Classes A class that is not completely implemented. Contains one or more abstract methods (methods with no bodies; only signatures) that subclasses must implement Cannot be used to instantiate objects Abstract class header: accessmodifier abstract class classname public abstract class Car Abstract method signature: accessmodifier abstract returntype methodname ( args ); public abstract int max_speed (); Subclass signature: accessmodifier class subclassname extends public class Mercedes extends Car classname

Interfaces A special abstract class in which all the methods are abstract Contains only abstract methods that subclasses must implement Interface header: accessmodifier public Abstract method signature: interface interfacename interface Car accessmodifier abstract returntype methodname ( args ); public abstract String CarType ( args ); Subclass signature: accessmodifier class subclassname implements someinterface public class BMW implements Car

Generic Methods Generic or parameterized methods receive the data-type of elements as a parameter E.g.: a generic method for sorting elements in an array (be it Integers, Doubles, Objects etc.)

A Simple Box Class Consider this non-generic Box class: public class Box { private Object object; public void set(object object) { this.object = object; public Object get() { return object;

A Simple Box Class A generic class is defined with the following format: class my_generic_class <T1, T2,..., Tn> { /*... */ Type parameters

A Simple Box Class Now to make our Box class generic: public class Box<T> { // T stands for "Type" private T t; public void set(t t) { this.t = t; public T get() { return t; To create, for example, an Integer Box : Box<Integer> integerbox;

Java Generic Collections Classes that represent data-structures Generic or parameterized since the elements data-type is given as a parameter E.g.: LinkedList, Queue, ArrayList, HashMap, Tree They provide methods for: Iteration Bulk operations Conversion to/from arrays