Distributed Systems Recitation 1. Tamim Jabban

Similar documents
Distributed Systems Recitation 1. Tamim Jabban

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

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

Abstract Classes and Interfaces

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

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

Inheritance (Part 5) Odds and ends

Java Classes, Inheritance, and Interfaces

C12a: The Object Superclass and Selected Methods

Inheritance. Transitivity

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

Programming II (CS300)

Introduction to Programming Using Java (98-388)

Programming II (CS300)

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Darrell Bethea June 6, 2011

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

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

Software and Programming 1

Java Persistence API (JPA) Entities

Super-Classes and sub-classes

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Java Fundamentals (II)

Informatik II Tutorial 6. Subho Shankar Basu

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

ECE 122. Engineering Problem Solving with Java

Java Magistère BFA

Chapter 5 Object-Oriented Programming

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

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

CO Java SE 8: Fundamentals

CMSC 132: Object-Oriented Programming II. Inheritance

Week 5-1: ADT Design

Java Object Oriented Design. CSC207 Fall 2014

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

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Objects as a programming concept

Collections, Maps and Generics

Object Oriented Programming

Informatik II (D-ITET) Tutorial 6

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

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

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.

Practical Session 3 Java Collections

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

What is Inheritance?

Fundamental Java Methods

Introduction to Inheritance

Notes on Chapter Three

CS 251 Intermediate Programming Inheritance

ITI Introduction to Computing II

Object Oriented Programming: Based on slides from Skrien Chapter 2

CS-202 Introduction to Object Oriented Programming

Java Generics -- an introduction. Based on

Inheritance and Polymorphism

CS 1302 Chapter 9 (Review) Object & Classes

More about inheritance

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

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

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Abstract class & Interface

CS 2530 INTERMEDIATE COMPUTING

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

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

CMSC 341. Nilanjan Banerjee

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

C08: Inheritance and Polymorphism

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Introduction to Computing II (ITI 1121) Final Examination

CS1150 Principles of Computer Science Objects and Classes

Lecture 2: Java & Javadoc

Collections Algorithms

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

ITI Introduction to Computing II

Exploring the Java API, Packages & Collections

CIS 110: Introduction to computer programming

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

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

Inheritance (Part 2) Notes Chapter 6

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

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

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

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

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

JAVA MOCK TEST JAVA MOCK TEST II

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

Recitation 3 Class and Objects

Index COPYRIGHTED MATERIAL

5/23/2015. Core Java Syllabus. VikRam ShaRma

Principles of Object Oriented Programming. Lecture 4

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Arrays Classes & Methods, Inheritance

COP 3330 Final Exam Review

Transcription:

15-440 Distributed Systems Recitation 1 Tamim Jabban

Office Hours Office 1004 Tuesday: 9:30-11:59 AM Thursday: 10: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 Student {

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

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

Java: Object Oriented Programming A programming paradigm based on objects To create a Student Object: Student Sameer = new Student();

Constructors Constructors take in zero or more variables to create an Object: public class Student { String name; int age; public Student() {

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

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

Class Hierarchy This introduces subclasses and superclasses. A class that inherits from another class is called a subclass: Alumni inherits from Student, and therefore Alumni is a subclass. The class that is inherited is called a superclass: Student is inherited, and is the superclass. Student Alumni Exchange Student

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

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 Student { public void setname(string newname) { this.name = newname; public class Test() { public static void main(string[] args) { Student Sameer = new Student(); Sameer.setName( Sameer );

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 (more on this later).

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 Student { private void setname(string newname) { this.name = newname; public Student(String name) { setname(name); public class Test() { public static void main(string[] args) { Student Sameer = new Student(); Sameer.setName( Sameer ); // Not accessible anymore!

Object & Class Variables Each Student 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 Student { public static String University= CMU ; public class Test() { public static void main(string[] args) { Student Sameer = new Student(); String uni = Sameer.University;

Object & Class Variables A Class Variable Example: public class Student { public static String University= CMU ; public class Test() { public static void main(string[] args) { String uni = Student.University;

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

Encapsulation Encapsulation is restricting access to an object s components. Using getters and setters: public class Student { private String name; private int age; public void setname(string newname) { this.name = newname; Student Sameer = new Student(); Sameer.setName( Sameer );

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) { // process date change public void changedate(int year, int month) { // process date change

Overloading Methods Methods overload one another when they have 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 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) { // process date change public void changedate(int month) { // process date change

Overloading Methods Methods overload one another when they have 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) { // process date change public void changedate(int month) { // process date change We can t overload methods by just changing the parameter name!

Overriding Methods Example: public class Parent { public int somemethod() { return 3; public class Child extends Parent { // this is method overriding: public int 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(); Student 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 Student class: public class Student { public String tostring() { return this.name;

Overriding Methods Example: Overriding the tostring and equals methods in our Student class: public class Student { public boolean equals(object obj) { if (obj.getclass()!= this.getclass())) return false; else { Student s = (Student) obj; return (s.name == 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 speed ( args ); Subclass signature: accessmodifier class subclassname extends classname public class Mercedes extends Car

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

Generic Methods Generic or parameterized methods receive the datatype 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 name<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 Provide methods for: Iteration Bulk operations Conversion to/from arrays *The data-type passed as parameter to a collection s constructor can not be of the type Object, the unknown type?, or a primitive data-type. The data-type must be a Class.

Why Generic Functions? Consider writing a method that takes an array of objects, a collection, and puts all objects in the array into the collection Generic Method

ArrayList Class Is a subclass of Collection Implements a resizable array Provides methods for array manipulation Generic or parameterized Declaration and Instantiation: ArrayList<ClassName> arraylistname = new ArrayList<ClassName>(); ArrayList<Student> students = new ArrayList<Student>();

ArrayList Methods Add, Get, Set, Clear, Remove, Size, IsEmpty, Contains, IndexOf, LastIndexOf, AsList etc. Basic Iterator: for (int i = 0; i < arraylistname.size(); i++) { // Get object at index i ClassName obj = arraylistname.get(i) // Process obj Advanced Iterator: for (ClassName obj : arraylistname) { // Process obj

Generic Classes with Wildcards Wildcards <?> denote unknown or any type (resembles <T>)