java for abap programmers 1-2

Upload: aaron-villar

Post on 03-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Java for ABAP Programmers 1-2

    1/7

    Alistair C. Rooney 2002 All rights reserved

    JavaTM for ABAP programmers Lesson 1

    The Chicken and the Egg.

    JavaTM is a funny language. The more you learn about it the more you love it. The problem

    is where do you start to teach Java TM ?

    Its a fully Object Orientated Language. This means that most people coming from anABAP environment will not have had any real exposure to OO concepts. (Hands up those

    who have done the BC404). OO is very important to Java and some would say its

    crucial.

    Normally I wouldnt talk about Java at all for the first few lectures. I would talk about

    Object Orientated principles. Inheritance, Polymorphism, Encapsulation and the like.

    On the other hand its nice to seesome Java to keep the excitement going.

    The Chegg First then!

    The compromise that most lecturers come up with is this. Lets have a look at a Hello

    World type program and then well explore some OO basics and then back to Java.

    Our First Program!

    Lets have a look at a simple ABAP program.

    What will this produce? A list dialog with Hello World of Abapers. OK, now lets look

    at the same thing in Java.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    REPORT ztestacr .

    DATA: v_hello(11) TYPE c VALUE 'Hello World',v_abapers(10) TYPE c VALUE 'of Abapers'.

    START-OF-SELECTION.

    WRITE: /, v_hello, v_abapers.

  • 7/28/2019 Java for ABAP Programmers 1-2

    2/7

    Alistair C. Rooney 2002 All rights reserved

    Thats it! Thats your first program. Now we need to activate it, like we would activate

    the ABAP program. In fact the process is somewhat similar. The Java program does notcompile to native code but rather to bytecode which is then interpreted by the Java

    Virtual Machine. (More about the JVM later in the course). To do this we issue the

    command javac HelloAbapers.java. The file weve just written must be saved with a.java extension.

    Lets see two separate examples on the same screen. One with errors and then one withthe errors corrected.

    Lets have a look at the code weve just written. The first line defines the class. Notice Ihavent defined a variable for my string in this example. Ill explain why when we cover

    static variables.

    Notice the curly brackets. This is how we define blocks in Java. They can be positioned

    anywhere, but it looks a lot neater if they are lined up and indented. The first curly

    bracket opens the class block.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    class HelloAbapers{

    public static void main(String args[])

    {System.out.println(Hello World of Abapers);

    }}

  • 7/28/2019 Java for ABAP Programmers 1-2

    3/7

    Alistair C. Rooney 2002 All rights reserved

    The next line defines the method we are using. In our case the main method. Every

    Java class that can be called or run directly from the command line, must contain a main

    method.

    Lastly lets look at the line that does the work. We call a System object that contains a

    method println. (More about the notation later). This accepts a single parameter andprints it on the screen. The parameter is the string.

    Dont worry at this early stage about the cryptic things like public or static or args[].

    Well cover these things as we go along.

    Finally we need to know how to run the jolly program. From a command line we merelytype in java HelloAbapers.

    That was easy! Obviously there is a bit more to Java than this. Stay tuned for the next

    lesson where well start to explore the benefits of Object Orientation and what theconcepts mean.

    End of Lesson 1

    Alistair Rooney is an SAP ABAP consultant who also lectures Java 2 and OOA/OOD. He has 20 years IT experience ranging from

    operator to IT Manager. He is a member of the Institute of IT Management (London) and a member of the British Computer Society.

    He currently lives in Durban, South Africa and is married with 2 children, a cat and several unwanted moles in the lawn.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 1-2

    4/7

    Alistair C. Rooney 2002 All rights reserved

    Lesson 2 Object Orientation in a nutshell

    Help me, let me out Im trapped in this nutshell..Thats me in a nutshell: Austin Powers

    The Nutshell Encapsulation.

    Fantasize for a moment that you needed to speak to Bill Gates. Unless youre a big wig in

    IT the chances of you speaking directly to him are small. You will probably deal with oneor many intermediaries. They will listen to your ideas and pass them on to Steve Ballmer

    who may not even pass then on to Bill.

    Thats how encapsulation works. You dont get direct access to the private data within a

    class. These are hidden from you. Dont feel offended, its really for your own good. You

    need to use special methods to fetch this data for you or even change it.

    Since the data may not be changed directly and only through these methods, we can be

    confident that we have not changed the way the class works. Now heres the bonus. We

    dont have to test it or worry that its doing what we want. It is a black box that we cantrust will do the job. Java has a lot of these really neat classes available for use. Theyre

    called APIs. Kind of like super Function Modules. More about these later.

    Back to our nutshell. The simple diagram below illustrates this. See how the private data

    is protected by the methods. In Java we call the Accessor or Mutator methods.

    Lets look at another concept within OO. Inheritance.

    Inheritance & Polymorphism.

    Meet Joe Soap. Hes an FI Consultant, but he wants to go further. He wants to specialize

    in Treasury. Thats ok. He does some extra training, becomes better at Treasury and is

    now a more specialized consultant. Is he any less of an FI Consultant? No, of course not,he still retains all that good experience he built up. Lets see that diagrammatically:

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    Methods()

    Data

    FI Consultant

  • 7/28/2019 Java for ABAP Programmers 1-2

    5/7

    Alistair C. Rooney 2002 All rights reserved

    We could say that the TR consultant is a more specialized FI consultant. We could also

    say that the TR consultant Inherits all of the FI consultants Attributes and

    Behaviours.

    Lets take a more accurate analogy now. Lets look at a Shape. We dont know what kind

    of shape it is, but it has some common attributes with all shapes. It has an area and it hasa colour. Now we can give it a behaviour. A good example is that a shape knows how to

    calculate its area.

    Again a diagram will illustrate this. Notice the two attributes and the one behaviour. Thisis how we draw them in UML. (Unified Modelling Language).

    Now heres where it gets interesting. We can now create three more specialized shapes

    that will inherit the attributes and behaviours from the Shape class. We call these sub-classes. From their perspective we call Shape the super-class.

    The variables defined inside the brackets loosely equate to exporting/importing functions

    (depending where you look at them) for a function module. Bear in mind that these are

    always the parameters being passed to a method. (Or the message in UML speak).

    Notice that they are different in two of the classes (Circle, Triangle) and they are the

    same for the Square. The Square class is said to have Overridden the calcArea methodfrom the superclass. The other two classes are said to have Overloaded the method.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    FI:TR Consultant

    Shape

    - Area

    - Colour

    + calcArea(x,y)

    Square

    + calcArea(x,y)

    Triangle

    + calcArea(x,y,z)

    Circle

    + calcArea(r)

  • 7/28/2019 Java for ABAP Programmers 1-2

    6/7

    Alistair C. Rooney 2002 All rights reserved

    Again, Ill go into greater detail on these later. Essentially the difference is that the

    method signature is the same for the Square and different for the others. This is the

    essence of polymorphism. In later lessons Ill explain the concept of late-binding whichmakes this powerful in Java.

    Real World objects and the Conceptual model (A UML glimpse)

    This is not the right place for the details, but several companies with the right resources

    have done studies on OO versus non-OO before investing serious money in changing

    their methodology to an Object Orientated Approach. To my knowledge, none have been

    able to refute the claim that it is a far more efficient methodology.

    Boeing is a case in point. (Sharble and Cohen, 1994).

    Im going to introduce you to one of the most common artefacts (UML document) , the

    Conceptual Model. There are many more documents that can be used by OO and I wouldstrongly encourage people to do more research on the subject. It willmake you a better

    Java programmer. It will also enable you to write truly re-usable code.

    (See http://uml.tutorials.trireme.com/uml_tutorial_navigation.htm)

    Lets take a look at a video store for an example. (Most people can relate to that).

    Firstly we make a list of Candidate classes or Concepts.

    Assistant.

    Video.

    Video Catalogue.Customer.

    Video Store.

    Loan.

    There are a number of techniques used to extract these candidates, but we wont cover it

    here. We can now start to build associations between our classes. This would start togive us an indication of the responsibilities of each class, which is a very important

    aspect of OO.

    The following diagram illustrates this more fully.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    LoandueDate0..1

    Assistant

    Catalogue

    VideoStore

    Video

    VideoID

    Customer

    name

    1

    *

    1

    1

    1..* *

    Handles >

    http://uml.tutorials.trireme.com/uml_tutorial_navigation.htmhttp://uml.tutorials.trireme.com/uml_tutorial_navigation.htm
  • 7/28/2019 Java for ABAP Programmers 1-2

    7/7

    Alistair C. Rooney 2002 All rights reserved

    Notice the numbering of relationships. 1 to many, 1 to 1 etc.

    Well thats a very brief introduction to the wonderful world of UML/OO. Ive heard

    people say that it takes 6 months to a year to convert a procedural programmer to an

    Object Oriented programmer. You can only benefit from learning and applying OO so

    stick with it! Personally I wouldnt put a time on it. It depends how keen you are.

    Thats the end of Lesson 2. Sometime in the future Ill put together a full UML course. Ifthis is something youd be interested in email me [email protected] I can gauge

    the interest.

    In Lesson 3 well explore some Java Basics, such as the primitive data types.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    1

    1

    1

    1*

    *

    0..1

    Records

    Borrows >

    < Employs

    RecordsStorageOf >

    < LendsVideoTo

    < Maintains

    mailto:[email protected]:[email protected]:[email protected]:[email protected]