1 / 26

JAVA Introduction

JAVA Introduction. One of the main JAVA design goal is reducing complexity for programmer Development time is half or less comparing to equivalent C++ programs Language support for multi-threading and network programming Cross-platform programs, dynamic code change, security

willhite
Télécharger la présentation

JAVA Introduction

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. JAVA Introduction • One of the main JAVA design goal is reducing complexity for programmer • Development time is half or less comparing to equivalent C++ programs • Language support for multi-threading and network programming • Cross-platform programs, dynamic code change, security • Java programs run in a Virtual Machine environment • Programs are compiled to an intermediate form • Intermediate code is run by VM

  2. Introduction to Object Orientation • Why OO? • What is wrong with non OO programming languages like C? • Is it really needed to learn OO? • What are the differences of making a program using OO than non OO way?

  3. Progress of abstraction • Programming languages makes abstractions • Assembly language is abstraction of the machine language • Fortran, Basic, C and many others were abstractions for assembly language • The above languages makes abstractions for machines • They don't provide abstraction of problem space • Object orientation • Provides abstractions of problem space • Elements of problem space are represented as Objects

  4. Object? • Characteristic of Smalltalk by Alan Kay • 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 (what messages can send to it?) • All object of a particular type can receive the same messages • Booch: • An object has state, behavior and identity

  5. An object has interface • A Type or Class of an object • Describes a set of objects with identical characteristics (data elements) and behaviors (functionality) Light lt = new Light(); lt.on();

  6. Objects as service providers • A program is a set of objects that provide services to other objects • Program design then is a process of finding or creating objects that solve the problem • Service provider view helps making High Cohesive objects • This view also helps others understand program

  7. Hidden implementation • Regarding an object we can distinguish two roles: • Creator of the object: wants to expose only what is necessary for the client programmer • Users of the object (client programmer): Only wants to know what an object does for him/her • Enforce client programmers to not access those parts of an object that they should not • Makes it possible to change internal structure of an object without worrying of effects on clients • Java uses public, private and protected for these access controls

  8. Reusing the implementation • Code reuse is one of the main advantages that OO languages provide • One way of reusing an object is to place it inside another object: Creating a member object (composition)

  9. Inheritance: Reusing the interface • Creating a class based on a similar already implemented class

  10. Inheritance: Reusing the interface • With inheritance you create a new type • The new type includes all the members of base type • It also has same interface of base type • If we inherit a new class from a base class and don't implement methods, then methods have same behavior as base class • Differentiating between derived class and base class: • Adding new methods to the derived class • Overriding existing methods

  11. Inheritance: Reusing the interface • Overriding existing methods IS-A relationship

  12. Inheritance: Reusing the interface • Adding new methods IS-LIKE-A relationship

  13. Interchangeable objects with polymorphism • Sometimes it is much convenient to treat objects in a type hierarchy all as base class objects • It allows to write code that does not depend on the specific type of objects: For all shape object O { o.draw(); } • But what happens when one shape object is actually a circle or a triangle or a square? • The key is late binding: runtime binding

  14. Interchangeable objects with polymorphism • Suppose we have: void doStuff(Shape s) { s.erase(); // ... s.draw(); } • and we have: Circle circle = new Circle(); Triangle t = new Triangle(); dostuff(c); dostuff(t);

  15. Interchangeable objects with polymorphism • Calls to dostuff works correctly. At runtime draw() and erase() methods of correct object is called. • The process of treating a derived type as a base type is called upcasting

  16. Abstract base class and interfaces • Often it is needed that base class only represent a common interface for its derived classes. One main usage is for upcasting. • This is done by making the class abstract. An abstract class can't be instantiated • It is also possible to have an abstract method (which means it is not implemented yet) • Interface is like abstract class, but in interface there is no method definition at all • One of the big differences between C++ and Java is that C++ doesn't have interface concept, and Java does not have multiple inheritance

  17. Object creation, use and lifetimes • Technically OOP is just about abstract data typing, inheritance and polymorphism. But some other issues are also important in writing OO programs. • One important issue is object lifetime: • C++ approach: Programmer controls memory allocation and disposal • Memory leaks is a major headache for programmers • Java approach: System controls memory allocation and disposal. Garbage collector takes care of removing objects that are no longer in use. • Programmers are not worrying about memory leaks

  18. Containers • In many useful programs it is needed to store a collection of objects. The number of objects are not known at the program development time • Every OO language has facilities to store collection of objects • Java has several different type of Lists, Maps and Sets used as containers • Containers provides methods to: • Put elements in • Get elements out

  19. Iterators • One of the standard ways to traverse all elements of a container is by the use of iterators • An iterator hides the details of a container from the code that is accessing that container • An iterator provides a SEQUENCE view of container • It does not matter if the container is a ArrayList, LinkedList or Stack. An iterator provides a sequence access to the elements of the container • So it is possible to change a data structure (if needed) without disturbing the client's code

  20. The singly rooted hierarchy • Java has a single rooted base class which is named Object • All objects inherits from Object class • Therefore all the Objects share a common interface • One usage is toString() method, which all the objects inherits from Object class • Another usage is for garbage collection

  21. Downcasting vs. templates/generics • Containers in Java stores and retreive objects of type Objects.When adding a object to a container it is upcasted to Object. When retrieving an object from container it is of type Object and normally is casted again but down the hierarchy to a specific type. This is named downcasting • But you should know the exact type of the object for downcasting • Another approach is what is in C++. It is named templates. When creating a container we specify the type of objects that is stored there. Next version of Java supports this with the name generics

  22. Object clean up and garbage collector • Java keeps track of object references and if all references to an objects is deleted, garbage collector delete the object from memory • In C++ there is no garbage collector. • The Java way is much convenient for programmers • But for some application areas (like real time systems), CPU time wasted by garbage collector is not acceptable

  23. Exception handling • Exceptions are thrown when an error occurs • Exceptions can be caught by an exception handler • It simplifies writing programs by enabling to treat exception as alternative path to normal program execution • Exceptions provides some mechanism to reliably recover from a bad situation • Java provides a solid exception handling that forces the programmers to write code to handle exceptions • Exceptions are a kind of object in Java

  24. Concurrency (multi-threading) • Java simplifies writing multi-threading programs • It shares CPU time between threads • However, shared resources should be managed by programmers • Synchronized keyword is used for controlling access shared resources

  25. Persistence • Normally when a program terminates, all objects are destroyed • Sometime it is needed to have objects even after program terminates • Java provides a way to store and retrieve objects on non-volatile memory. • This is named Serialization in Java

  26. Java and the internet • Web? Client-Server programming. • Client-side programming: HTML, Javascript, Plug-ins, Java applets • Server-side programming: CGI, Java web applications, .Net and C# • Java applications: Standalone applications, Server-side applications, client-side applets

More Related