Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
(REALLY SHORT) INTRODUCTION TO OO-PROGRAMMING WITH JAVA Luka Pavlič University of Maribor Faculty of Electrical Engineering and Computer Science Komarno, 16th of Februaryy 2010 OBJECT ORIENTATION ´ Motivation: « « « « « « ´ modularity loose coupling strong cohesion data hiding separation of interests REUSE Approaches Approaches: « « « « encapsulation inheritance message passing polymorphism COUPLING & COHESION ´ ´ ´ ´ ´ Coupling or dependency is the degree to which each program module relies on each one of the other modules. Cohesion is a measure of how strongly-related gy or focused the responsibilities of a single module are. High cohesion often correlates with loose coupling, and vice versa. versa High cohesion and loose coupling are attributes of good design. Given two lines of code, A and B, they are coupled when B must change behavior only because A changed. changed They are cohesive when a change to A allows B to change so that both add new value. OBJECT ORIENTATION Is not SE-only topic! ´ In SE it affects all layers of IS: databases, languages, development environmenst, production environments, environments bussiness processes, processes standards… ´ Built on success of predecessors ´ Enables component component-based based development ´ Today: No.1 paradigm in SE ´ BUT… BUT Fool with a tool… …is still a… FOOL! JAVA Talk is cheap, show me THE CODE! Java: language, development and runtime environment, i tools, l STANDARD! JAVA - HISTORY ´ 1990: Patrick Naughton, Naughton Bill Joy Joy, James Gosling Gosling, Mike Sheradin « How to make development od embeded systems simple? ´ Java: like C++, OO, strongly typed, inovations along… g ´ Java = language + development tools tools+ runtime environment + class library JAVA - HISTORY ´ “Write Write once, once run anywhere! anywhere!” JJanuary 1996, 1996 JRE was intoduced i t d d later l t ´ Internet wave ´ ´ 1996: 100 000 web pages with apples « 200 000 registered domains « ´ Small apps, console apps, applets JAVA - HISTORY ´ JDK 1 1.1: 1 1997 « ´ J2SE 1.2: 1 2 1998 « ´ JNDI, JavaSound, HotSpot… J2SE 1.4: 1 4 2002 « ´ Swing, Java IDL, Collections, strictfp, JIT… J2SE 1.3: 1 3 2000 « ´ new event model, Java Beans, JDBC, RMI, inner classes… XML, IPv6, JCA, JSSA, JAAS, WebStart… Java SE 5.0: 5 0: 2004 « Language changes TODAY: JAVA SE 6 ´ Web services Script languages JDBC Swing JDBC, Management Deployment Security PERFORMANCE Modularity ´ Update 10: Java FX! ´ ´ ´ ´ ´ ´ ´ www jcp org www.jcp.org TODAY: JAVA SE 6 LanguageS, libraries, development and runtime tools, platforms ´ Open Source! ´ Java J ME ´ Java SE 6 ´ Java EE 5 ´ JAVA LANGUAGE PROPERTIES Simple ´ OO ´ Distributed ´ Iterpreted ´ Robust ´ Secure S ´ Architecture c tectu e independent ´ Transferable T f bl ´ High g p performace ´ Multithreaded ´ Dynamic D i ´ OO & JAVA Objects + primitives (boolean, int, short, char, byte, short, float, double) ´ Operation-oriented design ´ Everything E thi g within ithi class… l - no hibrids! hib id ! ´ Packages g ´ TALK IS… IS WHERE IS THE CODE?!? ☺☺☺ LEARNING BY DOING IT Hello H ll World, W ld I can calculate! l l t ! ´ Primitives: ´ byte (8 bits) « short (16 bits) « int (32 bits) « long (64 bits) « float (32 bits) « double (64 bits) « char (16 bits) « boolean « no struct! ´ no typedef! ´ no #define! ´ no pointers! ´ LET LET’S S DO IT! JAVA AS DEVELOPMENT ENVIRONMENT ´ As a development environment, Java technology provides you with a large suite of tools: «A compiler (javac) « An interpreter (java) «A d documentation i generator (j (javadoc) d ) « A class file packaging tool and so on... JAVA AS RUNTIME ENVIRONMENT Java technology applications are typically generalgeneral purpose programs that run on any machine where the Java runtime environment (JRE) is installed. ´ There are two main deployment environments: ´ 1. The JRE supplied by the Java 2 Software 1 Development Kit (SDK) contains the complete set of class files for all the Java technology packages, which includes basic language classes classes, GUI component classes, and so on. « 2. The other main deployment p y environment is on yyour web browser. Most commercial browsers supply a Java technology interpreter and runtime environment. « JAVA AS RUNTIME ENVIRONMENT ´ Java Virtual Machine (JVM) an imaginary machine that is implemented by emulating software on a real machine « provides the hardware platform specifications to which you compile all Java technology code « ´ Bytecode a special machine language that can be understood by th Java the J Virtual Vi t l Machine M hi (JVM) « independent of any particular computer hardware, so any computer with a Java interpreter can execute the compiled Java program, no matter what type of computer the program was compiled on « LET US INTRODUCE MORE CLASSES… CLASSES What is class? ´ What is object? ´ What is method? ´ What about attribute? ´ What about memory management? ´ CLASSES… CLASSES Car Natasha’s Car Type: Mercedes Benz Model: B Color: Black Mike’s Car Type: Toyota Model: Yaris Color: Red p printIt() () getPrice() printIt() getPrice() … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … OBJECT-ORIENTED OBJECT ORIENTED PROGRAMMING= = OBJECTS with PROPERTIES and BEHAVIOUR And some other “technical” stuff (class vs. object vs. reference, null reference, constructor …) GARBAGE COLLECTION THREAD responsible for freeing any memory that can be freed. This happens automatically during the lifetime of the Java program. ´ programmer is freed from the burden of having to deallocate that memory themselves ´ CONTROL STRUCTURES ´ Loops: « While « Do-while « For «& ´ break, continue, return Branches: « If « Switch PACKAGES Similar classes ´ www.komarno.sk ´ ´ sk.komarno.myproject package / import Where are classes? ´ The purpose of CLASSPATH variable? ´ STANDARD PACKAGES? Java standard library ´ Java doc availabe online and for download! ´ ENCAPSULATION? Private? P i t ? ´ Public? ´ Package friendly? ´ ´ What is static?!? ´ Other modifiers: final, synchronized, abstract, native, transient CONSTRUCTORS, CONSTRUCTORS THIS REFERENCE There allways is default constructor ´ Defining our own constructor ´ Triggering constructor? ´ this – reference ´ this() - call ´ CLASS STRING Let us investigate the String ´ toString() method? ´ ´ StringBuffer class? ´ How to compare strings? == ? ARRAYS IN JAVA arg[] ?!?! ´0 0-based based indexes ´ Declaration / instantiation difference ´ length attribute ´ ´ the notion of null – reference! PARAMETER PASSING By reference? ´ By value? ´ GENERALIZATION ´ Main idea: « ´ ´ ´ base class, class superclass derived class, subclass single / multiple inheritance « ´ Abstaction, reuse, similar classes Be aware: multiple inheritance vs. inheritance depth Java implementation: « « « extends new modifier: protected reserved word: super INHERITANCE: NOTE IS-A relationship! ´1 1-1 1 cardinality! ´ ´ Use inheritance correctly! ABSTRACT CLASS ´ Do we allways want (or CAN!) implement all methods? ´ Ab t t methods Abstract th d and d classes l INTERFACES We can implement as many interfaces we want! ´ There is multiple inheritance between interfaces! ´ POLYMORPHISM The ability of a reference variable to change behavior according to what object instance it is holding ´ 3 types: ´ « Inclusion (overriding) « Operation (overloading) « Parametric BASICS EXTENDED ´ Some benefitial info: « System.out y « java.util.Scanner « Managing collections of objects (arrays sux!) « EXCEPTIONS!