1 / 35

Advanced Programming in Java

Explore OOP concepts like Abstraction, Encapsulation, and Inheritance, learn to think in terms of objects, messages, and classes, leveraging the power of Java language for advanced programming techniques. Derived from TIJ book with slides containing pseudocode excerpts. Sharif University of Technology course content.

bensond
Download Presentation

Advanced Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Programming in Java SadeghAliakbary Sharif University of Technology Fall 2012

  2. 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 Sharif University of Technology

  3. 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 Sharif University of Technology

  4. 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 Sharif University of Technology

  5. 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… Sharif University of Technology

  6. Library Problem • Suppose you want to write a library program • What are the elements of your program? • We think about functions and variables… Sharif University of Technology

  7. 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” Sharif University of Technology

  8. 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 Sharif University of Technology

  9. 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 Sharif University of Technology

  10. Object Oriented Languages • Smalltalk • The first successful object-oriented language • One of the languages upon which Java is based • Java • C++ • C## Sharif University of Technology

  11. 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 Sharif University of Technology

  12. OOP Characteristics • Alan Kay summarized five basic characteristics of Smalltalk • Everything is an object • A program is a bunch of objects telling each other what to do • by sending messages • Each object has its own memory • made up of other objects • Every object has a type • All objects of a particular type can receive the same messages Sharif University of Technology

  13. 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. Sharif University of Technology

  14. 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.isReserved())… Person p; …. p.setPhoneNumber(66166601) Sharif University of Technology

  15. 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 reservedTo; } Sharif University of Technology

  16. 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; Sharif University of Technology

  17. 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 Sharif University of Technology

  18. 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 Sharif University of Technology

  19. 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 Sharif University of Technology

  20. 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 Sharif University of Technology

  21. 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 Sharif University of Technology

  22. Representation of a light bulb: UML Diagram Sharif University of Technology

  23. Person in an Education System Sharif University of Technology

  24. New names in OOP • Function  Method, Service • Variable  Property, State Sharif University of Technology

  25. Encapsulation • Commercial products are encapsulated • Remote control • TV • Cell phone • They are Black Boxes • Hidden Implementations • Public interface Sharif University of Technology

  26. Why Encapsulation? • Simplified use • Even for the producer • Open implementation  bad use • Hiding the implementation reduces bugs • It is more beautiful! Sharif University of Technology

  27. 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) Sharif University of Technology

  28. 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 Sharif University of Technology

  29. 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 Sharif University of Technology

  30. Class Declaration Private area: hidden implementation publicclass Rectangle { privateintwidth, length; publicvoidsetWidth(int w) { width = w; } publicvoidsetLength(int l) { length = l; } publicintcalculateArea(){ returnwidth*length; } publicintcalculatePerimeter(){ return (width+length)*2; } } Public area :the interface Sharif University of Technology

  31. How to Use Rectangle? Rectangle rect = new Rectangle(); rect.setWidth(2); rect.setLength(7); System.out.println(rect.calculateArea()); System.out.println(rect.calculatePerimeter()); Object creation or instantiation Sharif University of Technology

  32. More Exercises • Encapsulate these concepts: • Student • Footbal-Player • Dog • name, age : properties • bite, bark : messages (actions) Sharif University of Technology

  33. 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 Sharif University of Technology

  34. 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 Sharif University of Technology

  35. Sharif University of Technology

More Related