1 / 17

INTRO TO OBJECT ORIENTED PROGRAMMING –

Learn the basics of object-oriented programming, including the concept of classes and how to use them. Explore the structure and content of classes, message passing, and method invocation. Gain insight into developing OO applications.

dunnbarbara
Download Presentation

INTRO TO OBJECT ORIENTED PROGRAMMING –

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. INTRO TO OBJECT ORIENTED PROGRAMMING – Classes Pete Collingwood

  2. Preamble: • Last session focus was on: • Revision of Elements of procedural programming: Exercises to recall SEQUENCE SELECTION REPETITION PROCEDURE Data Structures Primitive Types (local, global, int, float, char, boolean....) Arrays • Looked at Java as implementation language close to C++ : essentially less complex, but difficulties with Keybd i/o Pete Collingwood

  3. This Session – Intro to OO Objective is to move on to Object Orientated programming and, in particular get a handle on the meaning of terms such as: Pete Collingwood

  4. organisation Identifying what classes Identifying what objects Fitting it all together Scenario’s Identifying which Objects interact & how Structure & Contentof classes. message passing method invocation attributes methods 50% basic programming skill 20% OOn 30% Libraries Insight.... • Developing an OO application involves: Pete Collingwood

  5. Classes • We’ll begin our exploration of OO by introducing the concept of a Class. • What is a Class? Simple View: A way of structuring code to simplify understanding and ease re-use.The principle used to provide the relevant structure is that we should attempt to group things together in a cohesive fashion (ie. create “blocks” in the system with a clear purpose, clear responsibilities, and everything within the block has a role in providing that purpose), ie. bring useful bits together. Pete Collingwood

  6. Example – Console Class • All the applications we’ve developed so far have required the use of keyboard input & screen output. • Typically, we’ve defined various methods (eg c_in( ), c_out( ) etc ), then cut & pasted these into the code we’ve developed. • It would seem to be sensible to • try and bring those definitions together, in one place. • be able to access the methods defined • be able to use the same definitions in more than one application. c_in () c_out() Pete Collingwood

  7. Classes in Java grouping related items together & protecting them from external influences. • Key mechanism used in OO to support this notion of encapsulation is the idea of a class ie. • a set which describes all those objects within the system having common properties. • In Java, we define a class using the syntax: public class Console{ } //end Classname public class <Classname>{ } //end Classname For Example Pete Collingwood

  8. public class Console{ } //end Classname Classes in Java – Naming Conventions In Java, the convention is that: • Class names begin with a Capital letter. • “public” classes must be saved in a separate file with Filename the same as Classname and a .java extension. • method & variable names begin with lower casecharacters, but Capital letters are used as word separators. Other guidelines for selection of names (class, variable or method) remain the same.... saved as Console.java Pete Collingwood

  9. What Goes in a Class? • Data • Methods aka: attributes Variables used in the class Collectively known as fields Data are used by all methods. Methods access and use Data. aka: operations, behaviours Pete Collingwood

  10. Example – Console class public class Console{ } //end Classname //=============== ATTRIBUTES ================= static InputStreamReader is = new InputStreamReader(System.in); static BufferedReader in = new BufferedReader(is); static String data = ""; //================= METHODS ================== public static String cin(){ try{data = in.readLine(); } catch(IOException e){System.err.println(e);} return data; } public static void cout(String s){ System.out.print(s); } Pete Collingwood

  11. Data Publicly available operations. Private functions implementing publicly available operations. What else do we need to know about Classes? • Accessibility of Contents! Pete Collingwood

  12. Name Attributes Public methods Private methods General Structure of a Class. Pete Collingwood

  13. So Far, So Good... • we can pull out elements for storing & re-use. • have the appropriate structures in place (class definitions, public private, static..) • BUT.... how do we use this class structure once we've set it up? • For Example... rewrite the HelloWorld example: Do we still need this? • import java.io.*; • public class HelloWorld{ • public static void main (String[] args){ • cout("Hello World!\n"); • getChar(); • }//end main • } How do we tell the compiler where cout( ) & getChar( ) are defined ie. that they are in the Console class and where the Console class can be found? Pete Collingwood

  14. Using Classes the Simple Way. • Methods in one class can be invoked from within another class, by declaring all public methods in the called class to be static and preceding the method call by the name of the class and a dot: public class HelloWorld{ public static void main (String[] args){ Console.cout("Hello World!\n"); Console.getChar(); }//end main }//end HelloWorld This only works 'cos the methods are (public) & static Read this as "use the getChar( ) method in the Console class" Pete Collingwood

  15. Classpaths & Packages How is the Console class found? • In fact, javac (the java compiler) looks for the Source files in a number of places. • Further, java (the byte code interpreter) will look for .class files in a number of places. • The relevant search paths can be specified in a number of ways (dependent a little bit on the compiler version or the environment used) eg. by specifying the Classpath or Sourcepath variables, and on the complexity of the system (packages). • We will cover these complexities later. For now, we will adopt a simpler solution..... Pete Collingwood

  16. Searching for Classes the simple way: 1. in a text based environment. Simply place all source files in the same directory and run javac & java as per usual. The javac and java tools will look in that directory and will also write the .class files to that directory. 2. in JBuilder. This is a bit more complex but still straightforward. Either you create a project in the folder containingyour source files or you create a project. Then you add the relevant files to that project. Pete Collingwood

  17. Summary • Tried to clarify the main elements of the structure of a class in general OO terms and in Java in particular. • We have introduced the concepts of public & private class fields • Demonstrated how we can use public static methods to simplify communication between classes. • EXERCISE: modify the TicTac3 file to use the Console class defined here Pete Collingwood

More Related