Programming Language Concepts: Lecture 2

Similar documents
Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 1

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

CS 11 java track: lecture 1

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

Introduction to Programming Using Java (98-388)

CS105 C++ Lecture 7. More on Classes, Inheritance

An overview of Java, Data types and variables

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Brief Summary of Java

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

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Lecture 02, Fall 2018 Friday September 7

Kickstart Intro to Java Part I

ECE 122. Engineering Problem Solving with Java

C++ Important Questions with Answers

Index COPYRIGHTED MATERIAL

Conversions and Overloading : Overloading

COP 3330 Final Exam Review

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

CS 231 Data Structures and Algorithms, Fall 2016

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Getting started with Java

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Java Fundamentals (II)

Objects and Classes. Objects and Classes

Chapter 6 Introduction to Defining Classes

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lecture 2, September 4

Inheritance and Polymorphism

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

CS260 Intro to Java & Android 03.Java Language Basics

Chapter 7. Inheritance

Programming overview

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

More on Java. Object-Oriented Programming

Pace University. Fundamental Concepts of CS121 1

Class, Variable, Constructor, Object, Method Questions

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Java Bytecode (binary file)

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Values and Variables 1 / 30

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Character Stream : It provides a convenient means for handling input and output of characters.

Array. Prepared By - Rifat Shahriyar

Classes and Inheritance Extending Classes, Chapter 5.2

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Java Primer 1: Types, Classes and Operators

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

Developed in the beginning of the 1990 s by James Gosling from Sun Microsystems. Formally announced in a major conference in 1995.

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 4 Defining Classes I

1 Shyam sir JAVA Notes

BM214E Object Oriented Programming Lecture 8

Making New instances of Classes

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS-202 Introduction to Object Oriented Programming

Java: introduction to object-oriented features

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

COMP 250 Winter 2011 Reading: Java background January 5, 2011

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

STRUCTURING OF PROGRAM

Preview from Notesale.co.uk Page 9 of 108

Assumptions. History

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Lecture 1: Overview of Java

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Super-Classes and sub-classes

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Notes on Chapter Three

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

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

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Short Notes of CS201

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

You must declare all variables before they can be used. Following is the basic form of a variable declaration:

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Software Design and Analysis for Engineers

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

ENCAPSULATION AND POLYMORPHISM

CS212:Data Structure

CS201 - Introduction to Programming Glossary By

9 Working with the Java Class Library

ITI Introduction to Computing II

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

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Inheritance. Transitivity

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture 15: Inheritance II

Transcription:

Programming Language Concepts: Lecture 2 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 2, 19 January 2009

Classes and objects A class is a template for a datatype Instance variables or fields Functions, or methods, to operate on data An object is an instance of a class Private copy of instance variables Methods implicitly attached to objects e.g., s.pop()

Classes and objects A class is a template for a datatype Instance variables or fields Functions, or methods, to operate on data An object is an instance of a class Private copy of instance variables Methods implicitly attached to objects e.g., s.pop() Ideally an object is a black box whose internals are manipulated through a well defined interface

Classes and objects A class is a template for a datatype Instance variables or fields Functions, or methods, to operate on data An object is an instance of a class Private copy of instance variables Methods implicitly attached to objects e.g., s.pop() Ideally an object is a black box whose internals are manipulated through a well defined interface d :: Date getdate setdate

Classes and objects... However, most OO languages do give direct access to internal data Fields and methods can be private or public static fields and methods can be used without creating objects final means a value that cannot be modified

Constructors Can we initialize an object? Analogue of int i = 10;

Constructors Can we initialize an object? Analogue of int i = 10; Special methods called constructors Invoked once, when an object is created Usually have the same name as the class class Date{ private int day, month, year; public Date(int d, int m, int y){ day = d; month = m; year = y;

Constructors Can we initialize an object? Analogue of int i = 10; Special methods called constructors Invoked once, when an object is created Usually have the same name as the class class Date{ private int day, month, year; public Date(int d, int m, int y){ day = d; month = m; year = y; Date d = new Date(27,1,2009);

Constructors... Can have more than one constructor public Date(int d, int m){ day = d; month = m; year = 2009;

Constructors... Can have more than one constructor public Date(int d, int m){ day = d; month = m; year = 2009; Invoke appropriate constructor by context Date d1 = new Date(27,1,2008); Date d2 = new Date(27,1);

Constructors... Can have more than one constructor public Date(int d, int m){ day = d; month = m; year = 2009; Invoke appropriate constructor by context Date d1 = new Date(27,1,2008); Date d2 = new Date(27,1); Two functions can have the same name, different signatures Overloading

Constructors... A later constructor can call an earlier one class Date{ private int day, month, year; public Date(int d, int m, int y){ day = d; month = m; year = y; public Date(int d, int m){ this(d,m,2009); this refers to the object to which method is associated Objects have a notion of self!

Constructors... Can reverse the order class Date{ private int day, month, year; public Date(int d, int m){ day = d; month = m; year = 2009; public Date(int d, int m, int y){ this(d,m); year = y; Call to other constructor must be first instruction

Constructors... If no constructors are defined, default constructor initializes methods to default values Date d = new Date(); Note the brackets after Date Default constructor is available only if no constructors are defined

Back to Java Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments

Back to Java Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine

Back to Java Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class

Back to Java Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class java xyz interprets and runs bytecode in class file

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean Size of int etc are fixed, operational semantics is wrt JVM

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean Size of int etc are fixed, operational semantics is wrt JVM char is 2 bytes, Unicode, but otherwise behaves as usual

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean Size of int etc are fixed, operational semantics is wrt JVM char is 2 bytes, Unicode, but otherwise behaves as usual char c = a ; c = X ; if (c!= ) {...

Java syntax Basic datatypes are similar to other programming languages int (4 bytes), long (8 bytes), short (2 bytes) float (4 bytes), double (8 bytes) char (2 bytes) boolean Size of int etc are fixed, operational semantics is wrt JVM char is 2 bytes, Unicode, but otherwise behaves as usual char c = a ; c = X ; if (c!= ) {... Explicit boolean type boolean b, c = false; b = true; b = (i == 7);

Java syntax... Expressions are similar to C x = 7 returns value 7 flag = (x = 0) is caught as a syntax error Compound statements are familiar if (condition)... else... while (condition)... do... while (condition) for (i = 0; i < n; i++)... No goto, but labelled break and continue outer_loop: // this is a loop label for (i = 0; i < n; i++){ for (j = 0; j < n; j++){ if (a[i][j] == k){ break outer_loop; // exits the outer for loop

Java syntax, strings String is a built in class String s,t; String constants enclosed in double quotes String s = "Hello", t = "world"; Strings are not arrays of characters Cannot write s[3] = p ; s[4] =! ; Instead, invoke method substring in class String s = s.substring(0,3) + "p!"; + is overloaded for string concatenation If we change a String, we get a new object After the update, s points to a new String Java does automatic garbage collection

Java syntax, arrays Arrays are also objects Typical declaration int[] a; a = new int[100]; Can write int a[] instead of int[] a Can combine as int[] a = new int[100]; Aside: Why the seemingly redundant reference to int in new? Can create new arrays at run time a.length gives size of a Note, for String, it is a method s.length()!

Java arrays,... public class arraycheck{ public static void main(string[] argv){ int[] a; int i, n; n = 10; a = new int[n]; for (i = 0; i < n; i++){ a[i] = i; n = 20; a = new int[n]; for (i = 0; i < n; i++){ a[i] = -i;

Java syntax class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); args is an array of String argv in C Don t explicitly need the number of arguments (argc in C) Use args.length to get this

More about private fields Should private fields be visible to other objects of the same type? How do we check if two objects are equal? Date s,t;.. if (s == t) {... ;

More about private fields Should private fields be visible to other objects of the same type? How do we check if two objects are equal? Date s,t;.. if (s == t) {... ; == checks whether s and t are the same object Date s = new Date(27,1,2009); Date t = s; s == t Date s = new Date(27,1,2009); s == t Date t = new Date(27,1,2009);

More about private fields Should private fields be visible to other objects of the same type? How do we check if two objects are equal? Date s,t;.. if (s == t) {... ; == checks whether s and t are the same object Date s = new Date(27,1,2009); Date t = s; s == t Date s = new Date(27,1,2009); s == t Date t = new Date(27,1,2009); We want to check if the contents of s and t are the same

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year)

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year) Invoke as s.isequal(t) (or t.isequal(s))

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year) Invoke as s.isequal(t) (or t.isequal(s)) The object that executes isequal needs access to private information of the other object...

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year) Invoke as s.isequal(t) (or t.isequal(s)) The object that executes isequal needs access to private information of the other object......but both are objects of the same type, so internal structure is not a secret!

More about private fields Add a function isequal to the class Date class Date { private int day, month, year; public boolean isequal(date d){ return (this.day == d.day) && (this.month == d.month) && (this.year == d.year) Invoke as s.isequal(t) (or t.isequal(s)) The object that executes isequal needs access to private information of the other object......but both are objects of the same type, so internal structure is not a secret! Note the use of this to refer to the parent object this can be omitted if context is clear

Subclasses A class Employee for employee data class Employee{ private String name; private double salary; // Some Constructors... // "mutator" methods public boolean setname(string s){... public boolean setsalary(double x){... // "accessor" methods public String getname(){... public double getsalary(){... // other methods double bonus(float percent){ return (percent/100.0)*salary;

Subclasses Managers are special types of employees with extra features class Manager extends Employee{ private String secretary; public boolean setsecretary(name s){... public String getsecretary(){... Manager objects inherit other fields and methods from Employee Every Manager has a name, salary and methods to access and manipulate these.

Subclasses Managers are special types of employees with extra features class Manager extends Employee{ private String secretary; public boolean setsecretary(name s){... public String getsecretary(){... Manager objects inherit other fields and methods from Employee Every Manager has a name, salary and methods to access and manipulate these. Manager is a subclass of Employee Think of subset

Subclasses Manager objects do not automatically have access to private data of parent class.

Subclasses Manager objects do not automatically have access to private data of parent class. Common to extend a parent class written by someone else

Subclasses Can use parent class s constructor using super class Employee{... public Employee(String n, double s){ name = n; salary = s; public Employee(String n){ this(n,500.00);

Subclasses Can use parent class s constructor using super class Employee{... public Employee(String n, double s){ name = n; salary = s; public Employee(String n){ this(n,500.00); In Manager public Manager(String n, double s, String sn){ super(n,s); /* super calls Employee constructor */ secretary = sn;

Subclasses Subclass can override methods of super class

Subclasses Subclass can override methods of super class double bonus(float percent){ return 1.5*super.bonus(percent);

Subclasses Subclass can override methods of super class double bonus(float percent){ return 1.5*super.bonus(percent); In general, subclass has more features than parent class

Subclasses Subclass can override methods of super class double bonus(float percent){ return 1.5*super.bonus(percent); In general, subclass has more features than parent class Can use a subclass in place of a superclass Employee e = new Manager(...) Every Manager is an Employee, but not vice versa! Recall int[] a = new int[100]; Aside: Why the seemingly redundant reference to int in new? One can now presumably write Employee[] e = new Manager(...)[100]