440 likes | 566 Vues
In this lecture, Rina Zviel-Girshin provides an overview of Object-Oriented Programming (OOP) with a focus on Java. Key concepts such as classes, objects, and the role of class libraries are discussed. OOP is contrasted with other programming paradigms, highlighting how Java uniquely enforces OOP principles. The lecture explores real-world object representation in programming and emphasizes the importance of Java's class libraries, packages, and methods for building applications. Students learn about communication between objects and the structure of Java's standard libraries.
E N D
System development with Java Instructors: Rina Zviel-Girshin Lecture 3 Rina Zviel-Girshin @ARC
Overview • OOP • Class libraries and Packages • Classes and Objects Rina Zviel-Girshin @ARC
Java and OOP • Some languages do not support OOP – e.g. C, Pascal. • Some languages support OOP but not as the only method – e.g. C++. • Java not only supports but compels OOP. • OOP in Java has its own special flavor. Rina Zviel-Girshin @ARC
The central idea - objects • The central new idea in object-oriented programming is that of an object. • Objects tend to map the real-world to the set of entities. • Basic OO idea: • Look at the problem, decompose into objects. Rina Zviel-Girshin @ARC
Real-life objects The real-world consist of objects: • University, students, instructors, classroom, … • Bus, driver, passengers, bus station, tickets, … • Computer, programmer, CD ROM, monitor,… • Cinema, movie, actor, director, … Rina Zviel-Girshin @ARC
Objects Objects include both: • data (fields, variables, state) - attributes • processing (behavior, methods, functions) – what it can do or can be done to it Objects have two kinds of behavior: • outer – I/O, messages, relations with others • inner – processing and computing the messages Rina Zviel-Girshin @ARC
Communicating objects Relations between objects are implemented using communication (messages), you have to ‘send a message’ to ask an object to perform some action. Rina Zviel-Girshin @ARC
Object example Example: • Student has name, id, age, address, library card, grades. - All these called data or state. • Address of the student can be changed. • Library card can be lost. • New grade can be added to the grades. - All these called processing or behavior. Rina Zviel-Girshin @ARC
Example Rina Zviel-Girshin @ARC
Example Rina Zviel-Girshin @ARC
Class Libraries • A class library is a collection of classes that we can use when developing programs. • There is a Java standard class library that is part of any Java development environment. • These classes are not part of the Java language per se, but we rely on them heavily. • The Systemclass and the String class are part of the Java standard class library. • Other class libraries can be obtained through third party vendors, or you can create them yourself. Rina Zviel-Girshin @ARC
Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities Package java.lang java.applet java.awt javax.swing java.net java.util Packages • The classes of the Java standard class library are organized into packages. • Some of the packages in the standard class library are: Rina Zviel-Girshin @ARC
Java API (Packages) • Java comes with 4,000+ pre-designed components. • The Java API is the library of classes supplied by Java. • The classes in the Java API is separated into packages. Each package contains a set of classes that are related in some way. Rina Zviel-Girshin @ARC
Documentation: Packages.html List of Packages Rina Zviel-Girshin @ARC
java.lang List of Classes Rina Zviel-Girshin @ARC
String Class Class Hierarchy Class Documentation Rina Zviel-Girshin @ARC
String Methods Methods List Rina Zviel-Girshin @ARC
The import Declaration • When you want to use a class from a package, you could use its fully qualified name java.util.Random • Or you can import the class, then just use the class name import java.util.Random; • To import all classes in a particular package, you can use the * wildcard character import java.util.*; Rina Zviel-Girshin @ARC
The import Declaration • All classes of the java.lang package are automatically imported into all programs. • That's why we didn't have to explicitly import the System or String classes in earlier programs. • TheRandom class is part of the java.util package. • It provides methods that generate pseudo-random numbers. • We often have to scale and shift a number into an appropriate range for a particular purpose. Rina Zviel-Girshin @ARC
Example import java.util.Random; public class RandomNumbers { public static void main (String[] args) { Random generator = new Random(); int num; num = generator.nextInt(); System.out.println ("A random int: " + num); System.out.print(”Two Dice throws:”); Rina Zviel-Girshin @ARC
Example num = Math.abs(generator.nextInt()) % 6; System.out.print(num + “ and “); num = Math.abs(generator.nextInt()) % 6; System.out.println(num); } } Rina Zviel-Girshin @ARC
Class Methods • Some methods can be invoked through the class name, instead of through an object of the class • These methods are called class methods or static methods • The Mathclass contains many static methods, providing various mathematical functions, such as absolute value, trigonometry functions, square root, etc. temp = Math.cos(90) + Math.sqrt(delta); Rina Zviel-Girshin @ARC
Classes As New Types • The class declaration is a way of defining new types for your program – extending your language “vocabulary”. • Once a class is declared you can use it to declare object variables. • All those objects will be of the same type, they will have the same data and the same methods. Rina Zviel-Girshin @ARC
Objects & Classes Rina Zviel-Girshin @ARC
Objects & Classes • Class is a data type which describes the pattern of data and behavior. • Class is the type of object. • Class is the set of all objects of same type. • Class is the set of all objects of same type, including also data and behavior of the class and not of an object in it. Rina Zviel-Girshin @ARC
Objects & Classes • Objects of a class are called also instances of the class. • Class type, in contrast to its instance type, is the features of “all” of its objects (together) as opposite to the features of “each” of its objects (individually). Rina Zviel-Girshin @ARC
OO vocabulary • Class – a data type which describes the model or pattern of data and behavior. • Object – an instance of a class. • Messaging – request to perform an action. • Method – the code body that implements a message. Rina Zviel-Girshin @ARC
Creating Objects • To create a new object you need: • allocate a memory for the object • assign the reference to the object the address of the allocated memory • The newoperator has to be used. • Objects always initialized when they are created. • The creation of some objects require additional information about their initial state. Rina Zviel-Girshin @ARC
Creating Objects Example: new Student(“Avi”,1234); new Point(3,4); • The keyword new followed by class name and a pair of parenthesis creates an object in Java. • If creation requires additional information it is written inside the parenthesis. Rina Zviel-Girshin @ARC
Creating Objects Point mypoint; mypoint = new Point(235,456"); This calls the Point classconstructor, which is a special method that sets up the object • Creating an object is calledinstantiation. • An object is an instance of a particular class. • Objects are always initialized when they are created. Rina Zviel-Girshin @ARC
Object References • Object are created in the main memory but have no name and cannot be found. • In order to interact and use them we have to assign a reference to the address of the allocated memory. • We refer to an object by means of an object reference. • An object reference holds the data about memory location of the object. Rina Zviel-Girshin @ARC
x = 3 y = 4 p This variable has a type “reference to the object of type point” Object Reference Variables • Usually object references are stored in variables: Student avi = new Student(“Avi”,1234); Point p = new Point(3,4); Rina Zviel-Girshin @ARC
Memory Allocation • Why is it important to allocate a specific place in memory for each object? • Preserve the object state • Refer to it from different places • Not destroy it by overriding it with other data in that memory cells Rina Zviel-Girshin @ARC
Garbage Collection • Garbage collection takes care of de-allocating the memory storage. • The basic idea is storage reuse: • When an object is created the memory space is allocated for this object. • Later if the object or data are not used the memory is returned to the system for future reuse. • Java language performs the garbage collection periodically automatically. Rina Zviel-Girshin @ARC
new new, new… empty references x …new? …new Allocation Example Rina Zviel-Girshin @ARC
Class declaration public class Rectangle { // data int width,height; // methods public Rectangle() { width=0; height=0; } int perimeter() { return (width+height)*2;} int area() { return width*height;} } Rina Zviel-Girshin @ARC
Methods • Object has data and processing. • Data is not much use unless we can manipulate it. • For this purpose classes have methods. • Methods are implemented as part of class definition. • We interact with objects by method invocation. Rina Zviel-Girshin @ARC
Method invocation • We invocate a method by using • object name (object reference), • “.” , • method name and a pair of parenthesis. • If method invocation requires additional information it is written inside the parenthesis. Example: Point p = new Point(3,4); p.setx(0); p.sety(5); Rina Zviel-Girshin @ARC
Method declaration • Method name and parameter list called method signature. • The return type is not part of the signature. Example (methods of Rectangle class): int perimeter() { return (width+height)*2;} public void set(int width, int height) { this.width = width; this.height = height;} Rina Zviel-Girshin @ARC
Method declaration • Some methods have return value. int newX = p.getx(); • Each method has an access modifier. Method modifier is one of public, protected, private, static. (We will talk about them later.) • An additional information, written inside the parenthesis, called method parameters. Rina Zviel-Girshin @ARC
Constructors • Objects must be initialized before they can be used (similar to primitive data types). • We must specify what is the initial state of the object before we can use it. • We specify the way an object is initialized using a constructor, which is a special method which is invoked every time we create a new object. Rina Zviel-Girshin @ARC
Rectangle constructor public class Rectangle { … // constructor public Rectangle() { width=0; height=0; } // … other methods } Rina Zviel-Girshin @ARC
Constructor • Constructor has no declared return type and cannot return anything. • Constructor must have exactly the same name as class. public Rectangle() • Constructors can have parameters. Rina Zviel-Girshin @ARC
More about objects and constructors next time. Any Questions? Rina Zviel-Girshin @ARC