QAFQAZ UNIVERSITY

Computer Engineering Department

Object Oriented Programming Introduction to OOP Concept Dr. Abzetdin ADAMOV Chair of Computer Engineering Department [email protected]

http://ce.qu.edu.az/~aadamov

EVALUATION • Midterm – 30% • Compulsory Project – 20% (quiz – 70%; attendance – 30%) • Final – 50% • Total – 100 points • Optional Project – 10 points (max) (Referat – in accordance to requirements, min 7 pages , English + software implementation)

• Note: If you have requested Optional Project, it must be completed

References ftp://store.qafqaz.local/student/AAdamov-ComputerSciences/Object-OrientedProgramming/ 1. Algorithms in Java: 1-4. Parts Robert Sedgewick 2. Algorithms in Java: Graph Algorithms: 5.Parts Robert Sedgewick 3. Data structures and other objects using JAVA Micheal Main 4. Enterprise J2ME: Devoliping mobile Java applications Michael Juntao Yuan 5. HTML-JAVA-CGI-VRML-SGML-Unleashed William Robert Stanek 6. JAVA ile Proglamlama: Sınıflar ve Arabirimler Mine Keskinkılıç 7. Java : How to program H.M. Deitel 8. Java Network Programming and Distributed Computing David Reilly 9. Java ile Temel Programlama Bora Güngören 10. Java kullanım kılavuzu 11. Murach's Java servlets and JSP Andrea Steelman Rafta 12. Understanding Object Oriented Programming with JAVA 12. Google Web Toolkit: GWT Java Ajax Programming 13. Core Servlets and Javaserver Pages: Core Technologies, Vol. 2 (2nd Edition) 14. Advanced JavaScript, 3rd Edition 15. Graphic Java 2, Volume 2, Swing (3rd Edition) (Sun Microsystems Press Java Series) 16. Java Examples in a Nutshell, 3rd Edition [ILLUSTRATED] 17. Java In A Nutshell, 5th Edition (Paperback) 18. Sams Teach Yourself Programming with Java in 24 Hours (4th Edition)

Computer History • 1822 Charles Babbage Defference machine. • 1942 ENIAC – the first electric machine • 1945 John Von Neumann – – 1. shared programm technique – 2. condition control transfer • 1949 John Von Neumann – fist computer lang. Short Code • 1951 Grace Hopper – first compiler • 1957 – IBM FORTRAN (FORmula TRANslate) • 1959 – COBOL – (Common Business Oriented Language) language for Businessmen. • 1958 – Algol – the root of Pascal, C and Java • 1968 – Niklaus Wirth – Pascal – invented for education purposes • 1972 – Dennis Ritchie - Bell Labs – C for new UNIX system • 1983 – Bjarne Stroustroup – C++ OOP Concept • 1990 – Sun Microsystems - Java

BİLGİSAYAR TARİHİ

Programming Approaches and Technologies • Imperative programming

• Declarative programming • Procedural programming • Logical programming • Functional programming • Object Oriented Programming • Agent Oriented Programming (Shoham, APSLA) • Agent-Oriented Programming, Systems, Languages and Applications

• Service Oriented Programming (Architecture) (WSDL, SSCL) • Process Oriented Programming (SysML, PSL, BPEL, BPEL4WS) • Process Specification Language

A WAY OF VIEWING THE WORLD Gardeners Friend Me Grower Flora

Delivery Person

Flower Arranger

WholeSaler

Friend’s Florist

AGENT and COMMUNITIES An object-oriented program is structured as a community of interacting agents, called Objects. Each object has a role to play. Each object provides a service, or perform an action, that is used by other members of the community.

MESSAGES and METHODS Action is initiated in object-oriented programming by the transmission of a message to an agent (an object) responsible for the action. If the receiver accepts the message, it accepts the responsibility to carry out the indicated action.

INDEPENDENCE and RESPOSIBILITIES By discussing a problem in term of Responsibilities, we increase the level of abstraction. This permits greater Independence between objects. The entire collection of responsibilities associated with an object is often termed the Protocol.

CLASSES and INSTANCES All objects are instances of a class. All objects of a given class use the same method in response to similar messages.

CLASS HIERARCHIES INHERITANCE Human Female Shopkeeper Florist Flora

A child class (or subclass) will inherit attributes from a parent class higher in the tree. An Abstract Parent Class is a class for which there are no direct instances.

Summary of Object-Oriented Concepts • Everything is an Object. • Objects communicate by sending and receiving messages. • Each object has its own memory, which consists of other objects. • Every object is an instance of a class. A class simply represents a grouping of similar objects, such us integers or lists. • The class is the repository for behavior associated with an object. • Classes are organized into a singly rooted tree structure, called the inheritance hierarchy.

Introduction to Object Oriented Programming

• Abstraction • Encapsulation • Inheritance

Introduction to Object Oriented Programming

Abstraction

• Radio is an object that was designed to hide its complexity. • In object-oriented software, complexity is managed by using abstraction. • Abstraction is a process that involves identifying the crucial behavior of an object and eliminating irrelevant and tedious details. • Remember that the abstraction process is context sensitive.

Introduction to Object Oriented Programming

Abstraction

People come in all shapes, sizes, and colors. They have different backgrounds, enjoy different hobbies, and have a multitude of beliefs. But perhaps, in terms of the payroll application, an employee is just a name, a rank, and a serial number, while the other qualities are not relevant to the application.

Introduction to Object Oriented Programming

Encapsulation struct Kayitlar{ char name[15]; char addres[15]; int course; char comment[30]; }stud_info[5]; Struct in C

type LibRec =record flag: char; name: string[20]; price: real; numb: integer; end; var f: file of LibReci; Record in Pascal

In both languages, a function can operate on more than one data type and more than one function can operate on a single data type. Because these languages do not explicitly tie together data and the functions that operate on that data. In contrast, object-oriented programming is based on encapsulation. When an object’s state and behavior are kept together, they are encapsulated.

Introduction to Object Oriented Programming

Encapsulation Information Hiding

• Encapsulation is often referred to as information hiding (―black box‖). • Information hiding is really the result of encapsulation,not a synonym for it. • Encapsulation is also frequently confused with abstraction. • Encapsulation is the mechanism by which the abstraction is implemented.(The radio, for instance, is an object that encapsulates many technologies that might not be understood clearly by most people who benefit from it.)

Introduction to Object Oriented Programming

Inheritance Inheritance is the ability to define a new class that inherits the behaviors (and code) of an existing class. The new class is called a child or derived class, while the original class is often referred to as the parent or base class. Inheritance is used to express ―is-a‖ or ―kind-of‖ relationships. A car is a vehicle. A boat is a vehicle. A submarine is a vehicle. The main concepts behind inheritance are extensibility and code reuse. Composition In contrast to inheritance, there is also the notion of a ―has-a‖ relationship. Naturally, a car has an engine, but it is not a kind of engine.

Multiple Inheritance In this scenario, one class inherits from more than one base class.

Introduction to Object Oriented Programming

Inheritance class Motorcycle { class MotorVehicle class Car { String make; { String model; String make; String make; int year; String model; String model; int max_speed; int year; int year; int weight int max_speed; int max_speed; float price; int weight int weight int num_passengers; float price; float price; int num_wheels = 4; int num_passengers; int int num_doors; int num_wheels = 2; num_passengers; int num_wheels; } } }

Introduction to Object Oriented Programming

Inheritance class Motorcycle extends MotorVehicle { int num_wheels = 2; }

class coupe extends Car { int num_doors = 2; }

class Car extends MotorVehicle { int num_wheels = 4; int num_doors; }

class wagon extends Car { int num_doors = 5; }

Object Oriented Programming Concept - Object Real-world objects share two characteristics: They all have state and behavior. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Object Oriented Programming Concept – Object Consider a bicycle, for example: A bicycle modeled as a software object. By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6. OOP provides a number of benefits, including:

1. 2. 3. 4.

Modularity Information-hiding Code re-use Pluggability and debugging ease

Object Oriented Programming Concept – Class In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

Object Oriented Programming Concept – Class The Bicycle class:

BicycleDemo class creates two separate Bicycle objects:

class Bicycle { int cadence = 0; int speed = 0; int gear = 1;

class BicycleDemo { public static void main(String[] args) { // Create two different // Bicycle objects Bicycle bike1 = new Bicycle(); Bicycle bike2 = new Bicycle();

void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }

// Invoke methods on // those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates(); bike2.changeCadence(50); bike2.speedUp(10); bike2.changeGear(2); bike2.changeCadence(40); bike2.speedUp(10); bike2.changeGear(3); bike2.printStates(); } }

Object Oriented Programming Concept – Inheritance Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses: The syntax for creating a subclass is simple: use extend keyword class MountainBike extends Bicycle { // new fields and methods defining // a mountain bike would go here }

Why JAVA? 1. 2. 3. 4. 5. 6. 7. 8. 9.

Small Code General Interfaces Simpler than… Object-Oriented Network Savvy Interpreted Robust Multithreaded Dynamic

10. 11. 12. 13. 14. 15.

Platform Independent Secure Fast Encapsulated in JVM Different Technologies Distributed Properties

How JAVA Works? In the Java programming language, 1. Source code is plain text files ending with the .java extension. 2. Source files are compiled into .class files javac compiler. 3. A .class file does not contain code that is native to your processor; it instead contains bytecodes — the machine language of the Java Virtual Machine1 (Java VM). 4. JVM then runs your application and communicate to system.

First JAVA Program Because the Java VM is available on many different operating systems, the same .class files are capable of running on Microsoft Windows, the Solaris™ Operating System (Solaris OS), Linux, or Mac OS.

What Java Can Do? • •



• •

Development Tools: The development tools provide everything you'll need for compiling, running, monitoring, debugging, and documenting your applications. Application Programming Interface (API): The API provides a wide array of useful classes ready for use in your own applications. It spans everything from basic objects, to networking and security, to XML generation and database access, and more. Deployment Technologies: The JDK software provides standard mechanisms such as the Java EE software for deploying your applications to end users. User Interface Toolkits: The Swing and Java 2D toolkits make it possible to create sophisticated Graphical User Interfaces (GUIs). Integration Libraries: Integration libraries such as the Java IDL API, JDBC™ API, Java Naming and Directory Interface™ (JNDI) API, Java RMI, and Java Remote Method Invocation over Internet Inter-ORB Protocol Technology (Java RMI-IIOP Technology) enable database access and manipulation of remote objects.

What Java Can Do? • •



• •

Development Tools: The development tools provide everything you'll need for compiling, running, monitoring, debugging, and documenting your applications. Application Programming Interface (API): The API provides a wide array of useful classes ready for use in your own applications. It spans everything from basic objects, to networking and security, to XML generation and database access, and more. Deployment Technologies: The JDK software provides standard mechanisms such as the Java EE software for deploying your applications to end users. User Interface Toolkits: The Swing and Java 2D toolkits make it possible to create sophisticated Graphical User Interfaces (GUIs). Integration Libraries: Integration libraries such as the Java IDL API, JDBC™ API, Java Naming and Directory Interface™ (JNDI) API, Java RMI, and Java Remote Method Invocation over Internet Inter-ORB Protocol Technology (Java RMI-IIOP Technology) enable database access and manipulation of remote objects.

How Java Changes World? • • •



• •



Get started quickly: Especially for programmers already familiar with C or C++. Write less code: Program written in the Java programming language can be four times smaller than the same program written in C++. Write better code: The Java programming language encourages good coding practices, wide-ranging, easily extendible API let you reuse existing, tested code and introduce fewer bugs. Develop programs more quickly: The Java programming language is simpler than C++, and as such, your development time could be up to twice as fast when writing in it. Avoid platform dependencies: You can keep your program portable by avoiding the use of libraries written in other languages. Write once, run anywhere: Because applications written in the Java programming language are compiled into machine-independent bytecodes, they run consistently on any Java platform. Distribute software more easily: With Java Web Start software, users will be able to launch your applications with a single click of the mouse.

First JAVA Program //Simple Stand-alone application class HelloWorld { public static void main() { system.out.println(―Hello World\n‖); } }