Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
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.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Class and Inheritance.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
The Java Keyword this Marie J. Garlitos. Instance Methods First we introduce instance methods: –any method not declared with a static keyword –operates.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Java Programming, 4E Y. Daniel Liang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Java and C++, The Difference An introduction Unit - 00.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Internet Software Development Classes and Inheritance Paul J Krause.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
Introduction to Java 113 th February 2004Sifei HE © 2004 Introduction to Java for CS381, EE4.Web By Sifei HE Department of Computing.
Objects and Classes Mostafa Abdallah
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
Learners Support Publications Constructors and Destructors.
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
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.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Constructors and Destructors
Object Oriented Programming in
Topic: Classes and Objects
Before You Begin Nahla Abuel-ola /WIT.
Introduction to Java for CS381, EE4.Web
Object-Oriented Programming with Java
Object-Oriented Software Engineering
Constructors and Destructors
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
Presentation transcript:

Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause

Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

Getting Started  Goto:  Download the Java Development Kit (JDK) Its free! Its free!  Download the Documentation Also free! Also free!  Buy JAVA In a Nutshell, by David Flanagan, Publ. O’Rielly It’ll cost you £19.95, sorry! It’ll cost you £19.95, sorry!

File extensions in Java.java Source Byte code.class javac (compiler) JVM Java Virtual Machine Any Hardware (that supports the JVM)

What you get in the JDK appletviewer For running Applets javac Compiles.java .class java Interprets a Java Class classes.zip The system provided classes src.zip Complete source for standard classes javadoc Generates Java HTML documents javapDisassembler …

Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

Defining a Class Account number balance credit_account debit_account { members fields methods public class Account { public int number; public double balance; public void credit(double x) { // do some sums } public void debit(double y) { // do checking then sums }

“Circle” Example Circle radius circumference area public class Circle { } public double radius; public double circumference() { return 2 * PI * radius; } public double area() { return PI * radius * radius; } public static final double PI = ;

The “Circle” class public class Circle { // A class field public static final double PI= ; // A useful constant // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; } // An instance field public double r; // The radius of the circle // Two instance methods: they operate on the instance fields // of an object public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }

The “main” method public class Circle { public double r; // The radius of the circle public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; } public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double result = c.circumference(); System.out.println(result); }

Put “main” in a new class? public class MakeCircle { public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double circum = c.circumference(); System.out.println(circum); double a = c.area(); System.out.println(a); }

An Association MakeCircle main Circle radius circumference area creates instances of

File extensions in Java.java Source Byte code.class javac (compiler) JVM Java Virtual Machine Any Hardware (that supports the JVM)

The Circle example C:\>cd Java C:\Java>javac Circle.java C:\Java>javac MakeCircle.java C:\Java>java MakeCircle C:\Java>java MakeCircle C:\Java contains Circle.java and MakeCircle.java C:\Java now also contains Circle.class and MakeCircle.class

Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

Differences  No Preprocessor No analogues of #define, #include, #ifdef No analogues of #define, #include, #ifdef Constants are replaced by static final fields Constants are replaced by static final fields  No Global Variables Avoids possibility of namespace collisions Avoids possibility of namespace collisions We will see later how you can make a constant or variable globally accessible We will see later how you can make a constant or variable globally accessible

Java vs. C  Well-defined primitive type sizes Removes this as a platform dependency Removes this as a platform dependency  No pointers Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators This is not a handicap and eliminates and important source of bugs This is not a handicap and eliminates and important source of bugs

Java vs. C  Garbage Collection Objects are “tidied away” as soon as there are no further references to them Objects are “tidied away” as soon as there are no further references to them So, no need to explicitly manage memory So, no need to explicitly manage memory Eliminates memory leaks Eliminates memory leaks  No goto statement Adds exception handling and labelled break and continue statements Adds exception handling and labelled break and continue statements

Java vs. C  Variable declarations anywhere Java allows local variable definitions to be made anywhere in a method or block Java allows local variable definitions to be made anywhere in a method or block Good practice to group them, though Good practice to group them, though  Forward references Methods can be invoked before they are defined (we’ll see why it is important to be able to do this) Methods can be invoked before they are defined (we’ll see why it is important to be able to do this)

Java vs. C  Method overloading Multiple methods can be defined with the same name, so long as they have different parameter lists Multiple methods can be defined with the same name, so long as they have different parameter lists  No struct and union types  No enumerated types  No bitfields  No typedef  No method pointers  No variable-length argument lists

Object-Orientation & Java Contents  Getting Started  A Little Bit of Syntax  Differences between C and Java  Object-Oriented Programming in Java

The Members of a Class  Class fields public static final double PI = ; public static final double PI = ;  Class methods public static double radiansToDegrees(double rads) { public static double radiansToDegrees(double rads) { … }  Instance fields public double radius; public double radius;  Instance methods public double circumference() {…} public double circumference() {…}

Class Fields public static final double PI =  A field of type double  Named PI (capitalise constants)  Assigned a value of  The static modifier tags this as a Class Field Associated with the class in which it is defined Associated with the class in which it is defined  The final modifier means it cannot be changed

Class Fields…  There is only one copy of PI  Any instance of Class can refer to this field as PI  PI is essentially a Global Variable BUT  Methods that are not part of Circle access this as Circle.PI No name collisions No name collisions

Class Methods public static double radiansToDegrees(double rads) { return rads * 180 / PI; return rads * 180 / PI; }  Single parameter of type double and returns a value of type double  Is essentially a “global method” // how many degrees is 2.0 radians? double d = Circle.radiansToDegrees(2.0);

Instance Fields public double radius;  Each Circle object can have a have a radius independent of other Circle objects  Outside a class, a reference to an instance field must be prepended by a reference to the object that contains it Circle c = new Circle(); c.radius = 2.0; Circle d = new Circle(); d.radius = c.radius; Are they the same object?

Instance Methods  Instance methods operate on instances of a Class, and not on the Class itself  E.g. area() area() circumference() circumference()  If an instance method is used from outside the Class itself, it must be prepended by a reference to the instance to be operated on: Circle c = new Circle(); Circle c = new Circle(); c.radius = 2.0; c.radius = 2.0; double a = c.area(); double a = c.area();

Creating an Instance  Every Class has at least one constructor  This is used as a default constructor - a method with the same name as the Class Circle c = new Circle();  The new operator creates a new uninitialised instance of the Class  The constructor method is then called, with the new object passed implicitly

Initialising an Instance  A Constructor can use arguments placed between the parentheses to perform initialisation  Define a new Constructor for Circle public Circle(double r) {this.r = r;}  Now two ways: Circle c = new Circle(); c.r = 0.25;  Or Circle c = new Circle(0.25);

Multiple Constructors Public Circle() { r = 1.0; } Public Circle(double r) {this.r = r;}  This is perfectly legal  Each constructor must have a different parameter list  This is a simple example of method overloading

Destroying Objects  Java automatically reclaims the memory occupied by an object when it is no longer needed Garbage Collection Garbage Collection  The Java interpreter can determine when an object is no longer referred to by any other object or variable Also works for cycles Also works for cycles