Data Structures. Subodh Kumar. Dept of Computer Sc. & Engg. IIT Delhi

Similar documents
CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Java Interview Questions and Answers

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Introduction to Object-Oriented Programming

Java CPD (I) Frans Coenen Department of Computer Science

java Features Version April 19, 2013 by Thorsten Kracht

AP Computer Science Java Subset

History OOP languages Year Language 1967 Simula Smalltalk

Part I. Multiple Choice Questions (2 points each):

Chapter 1 Fundamentals of Java Programming

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

CS506 Web Design and Development Solved Online Quiz No. 01

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Crash Course in Java

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Chapter 1 Java Program Design and Development

Java Crash Course Part I

Java Software Development Kit (JDK 5.0 Update 14) Installation Step by Step Instructions

Tutorial: Getting Started

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

LAB4 Making Classes and Objects

CIS 190: C/C++ Programming. Polymorphism

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

CS193j, Stanford Handout #10 OOP 3

CS 106 Introduction to Computer Science I

Chapter 13 - Inheritance

Glossary of Object Oriented Terms

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

Java Programming Fundamentals

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Advanced Data Structures

Introduction to Java

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

Fundamentals of Java Programming

Java Programming Language

J a v a Quiz (Unit 3, Test 0 Practice)

Web Development and Core Java Lab Manual V th Semester

Installing Java (Windows) and Writing your First Program

Java from a C perspective. Plan

Habanero Extreme Scale Software Research Project

Description of Class Mutation Mutation Operators for Java

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Java (12 Weeks) Introduction to Java Programming Language

Object-Oriented Programming: Polymorphism

Inheritance, overloading and overriding

Object-Oriented Programming in Java

Programmation 2. Introduction à la programmation Java

Java Application Developer Certificate Program Competencies

What is Java? Applications and Applets: Result of Sun s efforts to remedy bad software engineering practices

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Programming

The Interface Concept

CS 111 Classes I 1. Software Organization View to this point:

Install Java Development Kit (JDK) 1.8

Web development... the server side (of the force)

Implementation Aspects of OO-Languages

public static void main(string[] args) { System.out.println("hello, world"); } }

C++ INTERVIEW QUESTIONS

Konzepte objektorientierter Programmierung

Using Files as Input/Output in Java 5.0 Applications

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Object Oriented Software Design

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Building Robots with NXT and LEJOS. Introduc<on. What is the NXT Robot Michael Wooldridge liv.ac.uk)

Android Application Development Course Program

PHP Object Oriented Classes and objects

Moving from CS 61A Scheme to CS 61B Java

Java applets. SwIG Jing He

Programming Language Concepts: Lecture Notes

Unit Testing and JUnit

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Object Oriented Software Design

Abstract. a

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Tutorial on Writing Modular Programs in Scala

How to use the Eclipse IDE for Java Application Development

Introduction to Object-Oriented Programming

An Overview of Java. overview-1

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Software Engineering Techniques

More on Objects and Classes

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

DIPLOMADO DE JAVA - OCA

Transcription:

Data Structures Subodh Kumar Dept of Computer Sc. & Engg. IIT Delhi

More instructions! Late policy! 75 hours of slack! Beyond this, no late submissions accepted! Please use your IITD email! Entry number will be taken from address! For Absence message! Mail to: col106mail@iitd.ac.in! Subject format must be Absent:dd/mm/yyyy! e.g., Absent:08/01/2015 2

Objects! Program = collection of cooperating objects, with specified behavior! Objects have properties and methods! Send message to objects to perform a task! Objects can contain other objects! Objects can send message to other objects! Or, create other objects! Objects can be derived from other objects! Object referenced by handles! Generated at creation 3

Object Oriented Programming! Objects abstract how-tos! Interface of invocation clearly defined! Each object knows how to do a few things! Sometimes by taking help from other objects! Does that thing only when someone asks it to! Becomes inactive after that thing is done! Programming =! Decide what classes are needed! What objects should be created to begin with! And sent a message 4

Object Oriented Programming! ObjectClasses abstract how-tos! Interface of invocation clearly defined! Each object knows how to do a few things! Sometimes by taking help from other objects! Does that thing only when someone asks it to! Becomes inactive after that thing is done! Programming =! Decide what classes are needed! What objects should be created to begin with! And sent a message 5

Basics of Running! Compile: javac filename1.java! Output is filename1.class! Not machine code, but byte-code! Platform independence, more security! Executed by Java Virtual Machine! Really interpreted! Run java interpreter: java filename1! Only the main class needs to be called! Other classes must be in the search path

Hello World! import java.util.*; class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!");! Should be in HelloWorld.java?! Executing java HelloWorld! amounts to HelloWorld.main() (args)! main is public so JVM may call it arg1 arg2! static, because it is independent of any instance

import java.util.*; Hello World! class Data { int x; public void print() { System.out.println( x = + x); class HelloWorld { public static void main(string[] args) { Data a = new Data(); a.print(); 8

import java.util.*; Hello World! class Data<T> { T x; public void print() { System.out.println( x = + x); class HelloWorld { public static void main(string[] args) { Data<Integer> a = new Data<Integer>(); a.print(); 9

Generic List v = new ArrayList(); v.add("test"); Integer i = (Integer) v.get(0); // Run time error List<String> v = new ArrayList<String>(); v.add("test"); Integer i = v.get(0); // Type error at compilation 10

Object-oriented Concepts! Class and Object! Visibility: default, protected, private, public! Constructor! Subclass! Inherit: Is A and extend, Override! Polymorphism! Name overloading, Overriding! Use derived object in place of super-class object! Abstract Class, Interface 11

Subclasses! Vehicles! Moves! Carries passengers! Hold luggage! Cars! Have one engine! Have doors! Has one driver! sportscar:! Van:! sleek shape, 2 doors, moves really fast, holds 2 passengers etc.! high top, 5 doors, moderate speed, holds 7 passengers, etc. 12

Class Hierarchy Subclass models is a relationships object of subclass is an object of superclass: it has all methods and attributes of the superclass Inheritance Hierarchy of classes Animal Reptile Mammal Fish Dog Cat Moose Mammal is an Animal: Animal animal = somemammal; Cat is a Mammal => a Cat is an Animal, Reptile, Mammal and Fish inherit from Animal Dog, Cat, and Moose inherit from Mammal 13

Hierarchy Design! superclass too general to define all behaviour! superclass legislates an abstract behaviour and delegates implementation to subclass! superclass specifies behaviour, subclasses inherits the behavior! superclass specifies behaviour, subclasses can choose to override behaviour! It knows about super-class s behaviour 14

Inheritance! A subclass specializes its superclass! adds new methods, overrides existing ones, and implements abstract methods! A superclass factors out capabilities common to its subclasses! A subclass inherits all capabilities of its superclass! Cars can move => SportsCars can move! subclasses:! inherit public methods/variables! inherit private methods/variables! but cannot directly access them! Can access protected methods/variables 15

Abstract Methods! Superclass delegates behaviour! e.g., all Vehicle instances must be able to move! But all Vehicle may move in its own way! Declaration specifies method s signature! name, parameters and return type! vehicle.move() is legal with Vehicle vehicle; is declared! Cannot instantiate an abstract class! A class containing an abstract method must itself be declared abstract public abstract class Vehicle{ // define to provide moving behaviour abstract public void move(); // Now this will not compile. Vehicle mycar = new Vehicle(); 16

JAVA Declaration! To declare a subclass of Vehicle, public class Car extends Vehicle! Constructor for Car: public Car() { super(); engine = new Engine(); (In JAVA all classes automatically are subclasses of in-built Object class) 17

superclass Constructor! Use super to call superclass constructor public SportsCar() { super(); // rest of constructor! Any call to the superclass constructor must be the first line of the subclass constructor! then, initialize any subclass instance variables! Java s default super constructor call is implicit if no explicit call made 18

Overriding Methods! Subclasses can override, i.e., redefine inherited methods: public class SportsCar extends Car { // declarations and constructor elided public void startengine() { // code to start engine with the // gusto of a SportsCar! Vroom!! Definition of startengine() replaces the one in the superclass! this method must be declared identically (in name and parameter list!) 19

Method Resolution! We can use Van in the same way we would use Car! because it is a Car! Car mysterycar = new Van();! How does Java determine which method to call when move() message is sent to an instance?! mysterycar.move();! Depends on what actual object mysterycar refers to: Van in this case 20

Method Resolution Rule! If the instance s class defines the method! Call it! If not, walk up the class inheritance tree from class to its superclass until it either! finds the method: calls that inherited method! doesn t find the method: issues error Object Car startengine() definition found. Call it move startengine No definition of startengine() found in Van, looking to Car Van move 21

class Aclass { void a() { System.out.println("aclass:a"); void b() { System.out.println("aclass:b"); a(); class Bclass extends Aclass { void a() { System.out.println("bclass"); What prints? Why? public class atest { public sta-c void main(string argv[]) { Aclass avar = new Bclass(); avar.a(); avar.b(); 22

OOP Quiz 1! True or False? 1. Subclasses inherit all methods and attributes of their superclasses A:privates not visible 2. Polymorphism means the programmer can create multiple instances of the same class No 3. Polymorphism allows different objects of a class to respond to the same message differently 4. Polymorphism means that depending on the values of parameters, a method will return different values No 23 super or sub can