Lecture 2: Object Oriented Programming I

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Object Oriented Programming. Problem Description “ …customers are allowed to have different types of bank accounts, deposit money, withdraw money and.
Object Oriented Programming Teguh Sutanto Si | STIKOM Surabaya
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Enhancing classes Visibility modifiers and encapsulation revisited
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Programming (OOP) Mohamed Ezz. Lecture 1 History and Concept.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
JAVA MULTIPLE CHOICE QUESTION.
Examples of Classes & Objects
Java Primer 1: Types, Classes and Operators
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Introduction to Java Programming
The Building Blocks Classes: Java class library, over 1,800 classes:
Group Status Project Status.
Interfaces.
Programs and Classes A program is made up from classes
Object Oriented Programming in java
Java Programming Language
Chapter 7 Objects and Classes
Chapter 5 Classes.
Presentation transcript:

Lecture 2: Object Oriented Programming I

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 field declarations can be preceded by different modifiers access control modifiers static final

More about field modifiers (1) 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

%> javac Pencil.java %> javac 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; } Pencil.java public class CreatePencil { public static void main (String args[]){ Pencil p1 = new Pencil(); p1.price = 0.5f; } CreatePencil.java %> javac Pencil.java %> javac CreatePencil.java CreatePencil.java:4: price has private access in Pencil p1.price = 0.5f; ^

More about field modifiers (2) 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? Pencil p2 = new Pencil(); System.out.println(p2.nextID); } 1 2 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 method header consists of modifiers (optional), return type, method name, parameter list and a throws clause (optional) types of modifiers access control modifiers abstract the method body is empty. E.g. abstract void sampleMethod( ); static represent the whole class, no a specific object can only access static fields and other static methods of the same class final cannot be overridden in subclasses method body

Methods – Invocation reference.method(arguments) 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) plainPencil plainPencil p class PassRef{ public static void main(String[] args) { Pencil plainPencil = new Pencil("PLAIN"); System.out.println("original color: " + plainPencil.color); paintRed(plainPencil); System.out.println("new color: " + plainPencil.color); } public static void paintRed(Pencil p) { p.color = "RED"; p = null; color: PLAIN p color: PLAIN color: RED color: RED NULL - If you change any field of the object which the parameter refers to, the object is changed 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”!

The Main Method - Concept 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