Advanced Programming in Java Sadegh Aliakbary Sharif University
Advanced Programming in Java Sadegh Aliakbary Sharif University of Technology Spring 2012
Agenda �Object Oriented Programming �Characteristics of objects �Interface �Encapsulation �The notes are mainly extracted from TIJ book �Codes of this slide are pseudocodes �they may have syntactic or logical errors Spring 2012 Sharif University of Technology 2
Abstraction �Machine language �Assembly: an abstraction of the machine language �Many languages are abstraction of assembly language �Fortran, Basic, C �Big improvement �But they still require you to think in terms of the structure of the computer �Rather than the structure of the problem Spring 2012 Sharif University of Technology 3
Different Contexts �Problem Space �the place where the problem exists �such as a business �Solution Space �the place where you’re implementing that solution �such as a computer �The effort required to perform this mapping Spring 2012 Sharif University of Technology 4
Alternatives to model the machine �An alternative is to model the problem �Some early languages chose particular views of the world: �LISP All problems are ultimately lists �APL All problems are algorithmic �Prolog Casts all problems into chains of decisions �Each of these approaches may be a good solution for particular class of problems �But not for all problems… Spring 2012 Sharif University of Technology 5
Library Problem �Suppose you want to write a library program �What are the elements of your program? �We think about functions and variables… Spring 2012 Sharif University of Technology 6
Object Oriented Approach �OO approach goes a step further �Lets the programmer represent problem space elements �This representation is general enough �The programmer is not constrained to any particular type of problem. �The elements in the problem space and their representations in the solution space are referred to as “objects” Spring 2012 Sharif University of Technology 7
OOP �The program is allowed to adapt itself to the lingo of the problem �by adding new types of objects �when you read the code, you’re reading words that also express the problem. �This is a more flexible and powerful language abstraction Spring 2012 Sharif University of Technology 8
OOP (2) �OOP allows you to describe the problem in terms of the problem �Rather than in terms of the computer �Objects in your code are similar to real objects �Recall the sample programs: phonebook and library Spring 2012 Sharif University of Technology 9
Object Oriented Languages �Smalltalk �The first successful object-oriented language �One of the languages upon which Java is based �Java �C++ �C## Spring 2012 Sharif University of Technology 10
OOP vs. Procedural Approach �Elements of OOP �Objects �Message passing between objects �Elements of procedural programming �Functions �Variables �Function invocation �The way of thinking �Thinking about objects and relations �Thinking about functions and computer structure Spring 2012 Sharif University of Technology 11
OOP Characteristics �Alan Kay summarized five basic characteristics of Smalltalk 1. Everything is an object 2. A program is a bunch of objects telling each other what to do � by sending messages 3. Each object has its own memory � made up of other objects 4. Every object has a type 5. All objects of a particular type can receive the same messages Spring 2012 Sharif University of Technology 12
Everything is an object �You can take any conceptual component in the problem �dogs, buildings, books, people, … �And represent it as an object in your program. �Example Person p; Book book; �Think of an object as a variable �It stores data �But you can make requests to that object �asking it to perform operations on itself. Spring 2012 Sharif University of Technology 13
Object Messages �To make a request of an object, you send a message to that object. �Message = invoking a method of an object. �Example Book b; …. if(b. is. Reserved())… Person p; …. p. set. Phone. Number(66166601) Spring 2012 Sharif University of Technology 14
Each object has its own memory �You can create a new kind of object �by making a package containing existing objects �Thus, you can build complexity into a program �while hiding it behind the simplicity of objects Book{ String name; Person reserved. To; } Spring 2012 Sharif University of Technology 15
Every object has a type �Each object is an instance of a class �class is synonymous with type � The most important distinguishing characteristic of a class is What messages can you send to it? Person p; Person q; Person[] people; Spring 2012 Sharif University of Technology 16
Substitutability �All objects of a particular type can receive the same messages �An object of type circle is also an object of type shape �A circle is guaranteed to accept shape messages �You can write code that talks to shapes and automatically handle anything that fits the description of a shape �This substitutability is one of the powerful concepts in OOP. �Inheritance �Polymorphism Spring 2012 Sharif University of Technology 17
Booch’s description of an Object �An object has state, behavior and identity �Booch added identity to the description �An object (may) have internal data �which gives it state �An object (may) have methods �to produce behavior �And each object can be uniquely distinguished from every other object �Each object has a unique address in memory Spring 2012 Sharif University of Technology 18
Abstract Data Types �Each programming language has some predefined data types �int, double, char, … �Creating abstract data types is a fundamental concept in object-oriented programming �Abstract Data Type = Class �Programmer defines a class to fit a problem �You extend the programming language by adding new data types specific to your needs Spring 2012 Sharif University of Technology 19
Messages �E. g. �Assign a book to a person �Set phone number of a person �Calculate area of a shape �complete a transaction �draw something on the screen �turn on a switch Spring 2012 Sharif University of Technology 20
Interface �Each object can satisfy only certain requests �The requests you can make of an object are defined by its interface �The type is what determines the interface Spring 2012 Sharif University of Technology 21
Representation of a light bulb: UML Diagram Spring 2012 Sharif University of Technology 22
Person in an Education System Spring 2012 Sharif University of Technology 23
New names in OOP �Function Method, Service �Variable Property, State Spring 2012 Sharif University of Technology 24
Encapsulation �Commercial products are encapsulated �Remote control �TV �Cell phone �They are Black Boxes �Hidden Implementations �Public interface Spring 2012 Sharif University of Technology 25
Why Encapsulation? �Simplified use �Even for the producer �Open implementation bad use �Hiding the implementation reduces bugs �It is more beautiful! Spring 2012 Sharif University of Technology 26
Object Encapsulation �Encapsulation of a problem-space concept into a class of objects �Define interface �Hide the implementation �Black box �The client may see the implementation �But can not use it directly �This is better even for the producer (programmer) Spring 2012 Sharif University of Technology 27
Access Control �Access to some parts of the class is restricted �Public and Private area of the class �The client of a class can use only public area �Public area = class interface �Public methods �Public variables Spring 2012 Sharif University of Technology 28
Example: Rectangle �Lets encapsulate a rectangle �What is a rectangle? �An object �Which has length and width (properties) �Lets you specify its length and width �Can calculate its area and perimeter Spring 2012 Sharif University of Technology 29
Class Declaration public class Rectangle { private int width, length; Private area: hidden implementation public void set. Width(int w) { width = w; } public void set. Length(int l) { length = l; Public area : } the interface public int calculate. Area(){ return width*length; } public int calculate. Perimeter(){ return (width+length)*2; } } Spring 2012 Sharif University of Technology 30
How to Use Rectangle? Rectangle rect = new Rectangle(); Object creation or instantiation rect. set. Width(2); rect. set. Length(7); System. out. println(rect. calculate. Area()); System. out. println(rect. calculate. Perimeter()); Spring 2012 Sharif University of Technology 31
More Exercises �Encapsulate these concepts: �Student �Footbal-Player �Dog � name, age : properties � bite, bark : messages (actions) Spring 2012 Sharif University of Technology 32
Conclusion �OOP brings us a new abstraction �OOP allows you to describe the problem in terms of the problem �Think in terms of the structure of the problem �Rather than the structure of the computer �Object has state, behavior and identity �Object has data and interface Spring 2012 Sharif University of Technology 33
Further Reading �Google these queries and read some pages (Wikipedia is preferred) �Object Oriented Programming �History of Object Oriented Programming �Smalltalk �UML �Encapsulation �Inheritance �Polymorphism Spring 2012 Sharif University of Technology 34
Spring 2012 Sharif University of Technology 35
- Slides: 35