Fundamentals of oop lecture 2


Published on

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Fundamentals of oop lecture 2

  1. 1. Fundamentals of OOP IT 2 Instructor: Paul Kisambira
  2. 2. Some History <ul><li>Developed and maintained by Sun Microsystems </li></ul><ul><ul><li>Originally called Oak </li></ul></ul><ul><ul><li>Aimed at producing an operating environment for networked devices and embedded systems </li></ul></ul><ul><ul><li>… but has been much more successful </li></ul></ul><ul><li>Design objectives for the language </li></ul><ul><ul><li>Simple, object-oriented, </li></ul></ul><ul><ul><li>Distributed, multi-threaded, and platform neutral </li></ul></ul><ul><ul><li>Robust, secure, scaleable </li></ul></ul>
  3. 3. The Virtual Machine <ul><li>Java is both compiled and interpreted </li></ul><ul><ul><li>Source code is compiled into Java bytecode </li></ul></ul><ul><ul><li>Which is then interpreted by the Java Virtual Machine (JVM) </li></ul></ul><ul><ul><li>Therefore bytecode is machine code for the JVM </li></ul></ul><ul><li>Java bytecode can run on any JVM, on any platform </li></ul><ul><ul><li>… including mobile phones and other hand-held devices </li></ul></ul><ul><li>Networking and distribution are core features </li></ul><ul><ul><li>In other languages these are additional APIs </li></ul></ul><ul><ul><li>Makes Java very good for building networked applications, server side components, etc. </li></ul></ul>
  4. 4. Features of the JVM <ul><li>Security </li></ul><ul><ul><li>Java offers very fine control over what an application is allowed to do </li></ul></ul><ul><ul><li>E.g. Read/write files, open sockets to remote machines, discover information about the users environment, etc </li></ul></ul><ul><ul><li>Used in Java Applets to create a “sandbox”. Stops a rogue applet attacking your machine. </li></ul></ul><ul><ul><li>Makes Java very safe, an important feature in distributed systems </li></ul></ul><ul><li>Class Loading </li></ul><ul><ul><li>Loading of bytecode into the virtual machine for execution </li></ul></ul><ul><ul><li>Code can be read from a local disk, over a network, or the Internet </li></ul></ul><ul><ul><li>Allows downloading of applications and applets on the fly </li></ul></ul><ul><ul><li>… and even ‘mobile code’ </li></ul></ul>
  5. 5. Versions of Java <ul><li>Java Language vs Java Platform </li></ul><ul><ul><li>Current version of the language is 1.4.1 </li></ul></ul><ul><ul><li>Core language plus additional APIs is called the Java 2 platform </li></ul></ul><ul><ul><li>Three versions of the Java 2 Platform, targetted at different uses </li></ul></ul><ul><li>Java 2 Micro Edition (J2ME) </li></ul><ul><ul><li>Very small Java environment for smart cards, pages, phones, and set-top boxes </li></ul></ul><ul><ul><li>Subset of the standard Java libraries aimed at limited size and processing power </li></ul></ul><ul><li>Java 2 Standard Edition (J2SE) </li></ul><ul><ul><li>The basic platform, which this course will cover </li></ul></ul><ul><li>Java 2 Enterprise Edition (J2EE) </li></ul><ul><ul><li>For business applications, web services, mission-critical systems </li></ul></ul><ul><ul><li>Transaction processing, databases, distribution, replication </li></ul></ul>
  6. 6. Some Salient Characteristics of Java <ul><li>Java is platform independent: the same program can run on any correctly implemented Java system </li></ul><ul><li>Java is object-oriented: </li></ul><ul><ul><li>Structured in terms of classes , which group data with operations on that data </li></ul></ul><ul><ul><li>Can construct new classes by extending existing ones </li></ul></ul>Appendix A: Introduction to Java
  7. 7. <ul><li>Java designed as </li></ul><ul><ul><li>A core language plus </li></ul></ul><ul><ul><li>A rich collection of commonly available packages </li></ul></ul><ul><li>Java can be embedded in Web pages </li></ul>
  8. 8. Java Processing and Execution <ul><li>Begin with Java source code in text files: </li></ul><ul><li>A Java source code compiler produces Java byte code </li></ul><ul><ul><li>Outputs one file per class: Model.class </li></ul></ul><ul><ul><li>May be standalone or part of an IDE </li></ul></ul><ul><li>A Java Virtual Machine loads and executes class files </li></ul><ul><ul><li>May compile them to native code (e.g., x86) internally </li></ul></ul>Appendix A: Introduction to Java
  9. 9. Compiling and Executing a Java Program Appendix A: Introduction to Java
  10. 10. Classes and Objects <ul><li>The class is the unit of programming </li></ul><ul><li>A Java program is a collection of classes </li></ul><ul><ul><li>Each class definition (usually) in its own .java file </li></ul></ul><ul><ul><li>The file name must match the class name </li></ul></ul><ul><li>A class describes objects (instances) </li></ul><ul><ul><li>Describes their common characteristics: is a blueprint </li></ul></ul><ul><ul><li>Thus all the instances have these same characteristics </li></ul></ul><ul><li>These characteristics are: </li></ul><ul><ul><li>Data fields for each object </li></ul></ul><ul><ul><li>Methods (operations) that do work on the objects </li></ul></ul>Appendix A: Introduction to Java
  11. 11. Grouping Classes: The Java API <ul><li>API = Application Programming Interface </li></ul><ul><li>Java = small core + extensive collection of packages </li></ul><ul><li>A package consists of some related Java classes: </li></ul><ul><ul><li>Swing: a GUI (graphical user interface) package </li></ul></ul><ul><ul><li>AWT: Application Window Toolkit (more GUI) </li></ul></ul><ul><ul><li>util: utility data structures </li></ul></ul><ul><li>The import statement tells the compiler to make available classes and methods of another package </li></ul><ul><li>A main method indicates where to begin executing a class (if it is designed to be run as a program) </li></ul>Appendix A: Introduction to Java
  12. 12. A Little Example of import and main <ul><li>import java.lang.*; </li></ul><ul><li>// all classes from javax.swing </li></ul><ul><li>public class HelloWorld { /* starts a class*/ </li></ul><ul><li>public static void main (String[] args) { </li></ul><ul><li>// starts a main method </li></ul><ul><li>System.out.println(“HelloWorld”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public = can be seen from any package </li></ul><ul><li>static = not “part of” an object </li></ul>Appendix A: Introduction to Java
  13. 13. Processing and Running HelloWorld <ul><li>javac </li></ul><ul><ul><li>Produces HelloWorld.class (byte code) </li></ul></ul><ul><li>java HelloWorld </li></ul><ul><ul><li>Starts the JVM and runs the main method </li></ul></ul>Appendix A: Introduction to Java
  14. 14. Primitive Data Types Appendix A: Introduction to Java Data type Range of values byte -128 .. 127 (8 bits) short -32,768 .. 32,767 (16 bits) int -2,147,483,648 .. 2,147,483,647 (32 bits) long -9,223,372,036,854,775,808 .. ... (64 bits) float +/-10 -38 to +/-10 +38 and 0, about 6 digits precision double +/-10 -308 to +/-10 +308 and 0, about 15 digits precision char Unicode characters (generally 16 bits per char) boolean True or false
  15. 15. Defining Your Own Classes <ul><li>The modifier private limits access to just this class </li></ul><ul><li>Only class members with public visibility can be accessed outside of the class* (* but see protected ) </li></ul><ul><li>Constructors initialize the data fields of an instance </li></ul>Appendix A: Introduction to Java
  16. 16. The Person Class <ul><li>// we have omitted javadoc to save space </li></ul><ul><li>public class Person { </li></ul><ul><li>private String givenName; </li></ul><ul><li>private String familyName; </li></ul><ul><li>private String IDNumber; </li></ul><ul><li>private int birthYear; </li></ul><ul><li>private static final int VOTE_AGE = 18; </li></ul><ul><li>private static final int SENIOR_AGE = 65; </li></ul><ul><li>... </li></ul>Appendix A: Introduction to Java
  17. 17. The Person Class (2) <ul><li>// constructors: fill in new objects </li></ul><ul><li>public Person(String first, String family, </li></ul><ul><li> String ID, int birth) { </li></ul><ul><li>this.givenName = first; </li></ul><ul><li>this.familyName = family; </li></ul><ul><li>this.IDNumber = ID; </li></ul><ul><li>this.birthYear = birth; </li></ul><ul><li>} </li></ul><ul><li>public Person (String ID) { </li></ul><ul><li>this.IDNumber = ID; </li></ul><ul><li>} </li></ul>Appendix A: Introduction to Java
  18. 18. The Person Class (3) <ul><li>// modifier and accessor for givenName </li></ul><ul><li>public void setGivenName (String given) { </li></ul><ul><li>this.givenName = given; </li></ul><ul><li>} </li></ul><ul><li>public String getGivenName () { </li></ul><ul><li>return this.givenName; </li></ul><ul><li>} </li></ul>Appendix A: Introduction to Java
  19. 19. The Person Class (4) <ul><li>// more interesting methods ... </li></ul><ul><li>public int age (int inYear) { </li></ul><ul><li>return inYear – birthYear; </li></ul><ul><li>} </li></ul><ul><li>public boolean canVote (int inYear) { </li></ul><ul><li>int theAge = age(inYear); </li></ul><ul><li>return theAge >= VOTE_AGE; </li></ul><ul><li>} </li></ul>Appendix A: Introduction to Java
  20. 20. The Person Class (5) <ul><li>// “printing” a Person </li></ul><ul><li>public String toString () { </li></ul><ul><li>return “Given name: “ + givenName + “n” </li></ul><ul><li>+ “Family name: “ + familyName + “n” </li></ul><ul><li>+ “ID number: “ + IDNumber + “n” </li></ul><ul><li>+ “Year of birth: “ + birthYear + “n”; </li></ul><ul><li>} </li></ul>Appendix A: Introduction to Java
  21. 21. The Person Class (6) <ul><li>// same Person? </li></ul><ul><li>public boolean equals (Person per) { </li></ul><ul><li>return (per == null) ? false : </li></ul><ul><li>this.IDNumber.equals(per.IDNumber); </li></ul><ul><li>} </li></ul>Appendix A: Introduction to Java
  22. 22. <ul><li>End for now </li></ul><ul><li>Be sure to try out different versions with different scenarios </li></ul><ul><li>NOTE: </li></ul><ul><li>Programming is a skill, you practice you will pass this course and you will be good at it </li></ul>