Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Similar documents
Object Oriented Technology

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Introduction to Programming Using Java (98-388)

Introduction to Programming (Java) 4/12

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

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CHAPTER 7 OBJECTS AND CLASSES

CS 11 java track: lecture 1

Lec 3. Compilers, Debugging, Hello World, and Variables

2. The object-oriented paradigm

Array. Prepared By - Rifat Shahriyar

Getting started with Java

Programming Language Concepts: Lecture 2

CHAPTER 7 OBJECTS AND CLASSES

Java Primer 1: Types, Classes and Operators

Declarations and Access Control SCJP tips

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Types, Values and Variables (Chapter 4, JLS)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Java. Representing Data. Representing data. Primitive data types

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Programming Language Concepts: Lecture 2

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

2. The object-oriented paradigm!

CSC 1214: Object-Oriented Programming

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

1 Shyam sir JAVA Notes

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Selected Questions from by Nageshwara Rao

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

An overview of Java, Data types and variables

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lecture 1: Overview of Java

Many strategies have been developed for this, but they often produce surprising results.

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

COE318 Lecture Notes Week 6 (Oct 10, 2011)

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

( &% class MyClass { }

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

Programming Language Concepts: Lecture 1

Java Fundamentals (II)

Pace University. Fundamental Concepts of CS121 1

Java Bytecode (binary file)

THE CONCEPT OF OBJECT

COP 3330 Final Exam Review

Introduction to Java

Chapter 1: Introduction to Computers, Programs, and Java

Values and Variables 1 / 30

BM214E Object Oriented Programming Lecture 8

Software and Programming 1

CS 231 Data Structures and Algorithms, Fall 2016

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Introduction to Classes and Objects. David Greenstein Monta Vista High School

STRUCTURING OF PROGRAM

Computer Science II Data Structures

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

Computational Expression

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

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

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Class, Variable, Constructor, Object, Method Questions

Java and C# in Depth

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

Lecture 10 Declarations and Scope

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

Lecture Set 4: More About Methods and More About Operators

Introduction to Java

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

CS 335 Lecture 02 Java Programming

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

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

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)

Programming overview

3. Java - Language Constructs I

Lecture 7: Classes and Objects CS2301

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS260 Intro to Java & Android 03.Java Language Basics

Lecture 5: Methods CS2301

A Foundation for Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

McGill University School of Computer Science COMP-202A Introduction to Computing 1

About this exam review

PROGRAMMING FUNDAMENTALS

Object Oriented Modeling

MIDTERM REVIEW. midterminformation.htm

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

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Transcription:

Lecture 1: Object Oriented Programming Muhammad Hafeez Javed www.rmhjaved.com

Procedural vs. Object-Oriented Programming The unit in procedural programming is function, and unit in object-oriented programming is class Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them. Procedural programming separates the data of the program from the operations that manipulate the data, while object-oriented programming focus on both of them figure1: procedural figure2: object-oriented

Concept of Class and Object Class refers to a blueprint. It defines the variables and methods the objects support Object is an instance of a class. Each object has a class which defines its data and behavior

Class Members A class can have three kinds of members: fields: data variables which determine the status of the class or an object methods: executable code of the class built from statements. It allows us to manipulate/change the status of an object or access the value of the data member nested classes and nested interfaces

Sample class class Pencil { public String color = red ; public int length; public float diameter; public static long nextid = 0; public void setcolor (String newcolor) { color = newcolor;

Fields Declaration Field Declaration a type name followed by the field name, and optionally an initialization clause primitive data type vs. Object reference boolean, char, byte, short, int, long, float, double

More about field modifiers Access control modifiers private: private members are accessible only in the class itself package: package members are accessible in classes in the same package and the class itself protected: protected members are accessible in classes in the same package, in subclasses of the class, and in the class itself public: public members are accessible anywhere the class is accessible

Pencil.java CreatePencil.java public class Pencil { public String color = red ; public int length; public float diameter; private float price; public static long nextid = 0; public void setprice (float newprice) { price = newprice; public class CreatePencil { public static void main (String args[]){ Pencil p1 = new Pencil(); p1.price = 0.5f; %> javac Pencil.java %> javac CreatePencil.java CreatePencil.java:4: price has private access in Pencil p1.price = 0.5f; ^

More about field modifiers static only one copy of the static field exists, shared by all objects of this class can be accessed directly in the class itself access from outside the class must be preceded by the class name as follows System.out.println(Pencil.nextID); or via an object belonging to the class from outside the class, non-static fields must be accessed through an object reference

public class CreatePencil { public static void main (String args[]){ Pencil p1 = new Pencil(); Pencil.nextID++; System.out.println(p1.nextID); //Result? 1 Pencil p2 = new Pencil(); Pencil.nextID++; System.out.println(p2.nextID); //Result? 2 System.out.println(p1.nextID); //Result? still 2! Note: this code is only for the purpose of showing the usage of static fields. It has POOR design!

More about field modifiers (3) final once initialized, the value cannot be changed often be used to define named constants static final fields must be initialized when the class is initialized non-static final fields must be initialized when an object of the class is constructed

Fields Initialization Field initialization not necessary to be constants, as long as with the right type If no initialization, then a default initial value is assigned depending on its type Type Initial Value boolean false char \u0000 byte, short, int, long 0 float +0.0f double +0.0 object reference null

Methods Declaration Method declaration: two parts 1. method header consists of modifiers (optional), return type, method name, parameter list and a throws clause (optional) types of modifiers access control modifiers abstract static final 2. method body the method body is empty. E.g. abstract void samplemethod( ); represent the whole class, no a specific object can only access static fields and other static methods of the same class cannot be overridden in subclasses

Methods Invocation Method invocations invoked as operations on objects/classes using the dot (. ) operator reference.method(arguments) static method: Outside of the class: reference can either be the class name or an object reference belonging to the class Inside the class: reference can be ommitted non-static method: reference must be an object reference

Method - Overloading A class can have more than one method with the same name as long as they have different parameter list. public class Pencil {... public void setprice (float newprice) { price = newprice; public void setprice (Pencil p) { price = p.getprice(); How does the compiler know which method you re invoking? compares the number and type of the parameters and uses the matched one

Methods Parameter Values Parameters are always passed by value. public void method1 (int a) { a = 6; public void method2 ( ) { int b = 3; method1(b); // now b =? // b = 3 When the parameter is an object reference, it is the object reference, not the object itself, getting passed. Haven t you said it s past by value, not reference?

another example: (parameter is an object reference) class PassRef{ public static void main(string[] args) { Pencil plainpencil = new Pencil("PLAIN"); System.out.println("original color: " + plainpencil.color); paintred(plainpencil); plainpencil color: PLAIN plainpencil color: PLAIN p System.out.println("new color: " + plainpencil.color); color: RED plainpencil p public static void paintred(pencil p) { p.color = "RED"; color: RED p = null; - If you change any field of the object which the parameter refers to, the plainpencil object is changed p for every variable which holds a reference to this object - You can change which object a parameter refers to inside a method without affecting the original reference which is passed - What is passed is the object reference, and it s passed in the manner of PASSING BY VALUE! NULL

The Main Method - Concept main method the system locates and runs the main method for a class when you run a program other methods get execution when called by the main method explicitly or implicitly must be public, static and void

The Main Method - Getting Input from the Command Line When running a program through the java command, you can provide a list of strings as the real arguments for the main method. In the main method, you can use args[index] to fetch the corresponding argument class Greetings { public static void main (String args[]){ String name1 = args[0]; String name2 = args[1]; System.out.println("Hello " + name1 + & +name2); java Greetings Jacky Mary Hello Jacky & Mary Note: What you get are strings! You have to convert them into other types when needed.

Modifiers of the classes A class can also has modifiers public publicly accessible without this modifier, a class is only accessible within its own package abstract no objects of abstract classes can be created all of its abstract methods must be implemented by its subclass; otherwise that subclass must be declared abstract also final can not be subclassed Normally, a file can contain multiple classes, but only one public one. The file name and the public class name should be the same