Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET A step to equip our...

34
Introduction to Java & .NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java & .NET A step to equip our team with the OO programming basics An initiative to realize our values FAST Session 1

Transcript of Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET A step to equip our...

Page 1: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java & .NETSEAP/BTIS/DB&I/Aug 2010 1

Introduction to Java & .NET

A step to equip our team with the OO programming basics An initiative to realize our values FAST

Session 1

Page 2: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 2

Agenda

1. Introduction

2. Java, .NET, and Procedural Languages

3. Object Oriented Basics

Page 3: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 3

1. Introduction

Page 4: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 4

Introduction

Benefitsa) broaden our knowledge by keeping up with the trendb) understand and support project programming phases

Objectivesa) realize the benefits set forthb) able to test change existing programs in our department

Difficultiesa) none of us come from OO programming backgroundb) your contribution is necessary to continue the sessiond) training touches surface onlyd) commit time to further study

Page 5: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 5

Introduction

Task Aug Sep Oct Nov Dec Jan - Mar

1: Introduction & OO Zhang

2: Java Basics (1) Zhang

3: Java Basics (2) Zhang

4: Visio Studio ?

5: C# Introduction ?

6: VB (1) VBScript ?

7: VB (2) ?

8: Recode program

Page 6: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 6

2. Java, .NET, and Procedural Languages

Page 7: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 7

What is Java

Java is a programming language and computing platform first released by Sun Microsystems in 1995. (Now belong to Oracle.)

It is the underlying technology that powers state-of-the-art programs including utilities, games, and business applications. Java runs on more than 850 million personal computers worldwide, and on billions of devices worldwide, including mobile and TV devices.

The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture.

Java is general-purpose, concurrent, class-based, and object-oriented, and is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere".

Page 8: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 8

What is Java

One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on any supported hardware/operating-system platform.

This is achieved by compiling the Java language code to an intermediate representation called Java bytecode, instead of directly to platform-specific machine code. Java bytecode instructions are analogous to machine code, but are intended to be interpreted by a virtual machine (VM) written specifically for the host hardware. End-users commonly use a Java Runtime Environment (JRE) installed on their own machine for standalone Java applications, or in a Web browser for Java applets.

Page 9: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 9

What is .NET

The Microsoft .NET Framework is a software framework that can be installed on computers running Microsoft Windows operating systems.

It includes a large library of coded solutions to common programming problems and a virtual machine that manages the execution of programs written specifically for the framework. The .NET Framework supports multiple programming languages in a manner that allows language interoperability, whereby each language can utilize code written in other languages; in particular, the .NET library is available to all the programming languages that .NET encompasses.

The .NET Framework is a Microsoft offering and is intended to be used by most new applications created for the Windows platform.

Page 10: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 10

What is .NET

.NET and JVM are different. Java byte code isn't compatible with .NET byte code.

Page 11: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 11

Procedural Languages

• e.g. FORTRAN, C, BASIC, Pascal, PL/SQL, etc

Characteristics:

• Data is contained in data structures directly accessed by routines

• Data structures are separated from the routines’ code

• Different code modules access and modify the same data structures

• Data structures and code have different owners

• We may eventually lose knowledge of which code uses which data structure

Page 12: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 12

Difference Between OOL & PPL

A major factor in the invention of Object-Oriented approach is to remove some of the flaws encountered with the procedural approach.

Object Orientation Languages (OOL) is concerned to develop an application based on real time while Procedural Programming Languages (PPL) are more concerned with the processing of procedures and functions.

In OOL, more emphasis is given on data rather than procedures, while the programs are divided into Objects and the data is encapsulated (Hidden) from the external environment, providing more security to data which is not applicable or rather possible in PPL. In PPL, its possible to expose Data and/or variables to the external entities which is STRICTLY restricted IN OOL.

In OOL, the Objects communicate with each other via Functions while there is no communication in PPL rather its simply a passing values to the Arguments to the Functions and / or procedures.

OOL follows Bottom Up Approach of Program Execution while in PPL its Top Down approach.

OOL concepts includes Inheritance, Encapsulation and Data Abstraction, Late Binding, Polymorphism, Multithreading, and Message Passing while PPL is simply a programming in a traditional way of calling functions and returning values.

Source: www.wikianswers.com

Page 13: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 13

3. Object Oriented Basics

Page 14: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 14

Why OO?A technology that strongly ties data structures to the code routines that relate to them, fusing them into objects• Offers a good methodological framework for software engineering• Provides the foundation for systematic software engineering

Objects relate to the “real world” more closely than modules of routines• Encourage better modeling and implementation of systems.• Provide a focus throughout analysis, design, and implementation• Emphasize state, behavior, and interaction

Benefits of OO programming:

• Reusability• Stability• Reliability• Data Hiding• Faster Design• Easier maintenance• Design Independence

Page 15: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 15

OO Building Blocks

There are differences between different OO languages, but general building blocks are:

ObjectsWith individual characteristics

ClassesObjects belong to a type

AssociationsObjects know about each other and communicate with each other

InheritanceA type may be a specialization of another type.

PolymorphismA specialized type can be treated in a generic way

Page 16: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 16

UML – Unified Modelling Language

This object-oriented system of notation has evolved from the work of Grady Booch, James Rumbaugh, Ivar Jacobson, and the Rational Software Corporation.

These renowned computer scientists fused their respective technologies into a single, standardized model. Today, UML is accepted by the Object Management Group (OMG) as the standard for modeling object oriented programs.

Types of UML Diagrams

UML defines nine types of diagrams: class (package), object, use case, sequence, collaboration, statechart, activity, component, and deployment.

Source: http://www.smartdraw.com/resources/tutorials/uml-diagrams/

Page 17: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 17

Example

Page 18: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 18

Example

Page 19: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 19

Terminology

• Object: A thing that exists in the domain of the problem

E.g. An office building might have a number of ‘Elevators’, ‘Offices’, ‘SecurityDoors’, etc.

• Object Oriented: Software is object-oriented if the design and implementation of that software is based on the interaction between the objects of the domain

• Class: A template used for the creation of objects A class describes various attributes an object might have

e.g. A ‘Person’ class might have various attributes, including age, weight, height, eye colour, address, etc.

Page 20: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 20

Objects and Classes

Bank Account

A class(the concept)

John’s Bank AccountBalance: $5,257

An object(the realization)

Bill’s Bank AccountBalance: $1,245,069

Mary’s Bank AccountBalance: $16,833

Multiple objectsfrom the same class

Page 21: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 21

Example of a Class

class Employee { // Fields private String name; //Can get but not change private double salary; // Cannot get or set // Constructor Employee(String n, double s) { name = n; salary = s; } // Methods void pay () { System.out.println("Pay to the order of " + name + " $" + salary); } public String getName() { return name; } // getter}

Page 22: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 22

Association (“has-a”)public class AntiAirCraftGun { private Bomber target; private int positionX; private int positionY; private int damage; public void setTarget(Bomber newTarget) { this.target = newTarget; } //rest of AntiAircraftGun class } public class Bomber { private AntiAirCraftGun target; private int positionX; private int positionY; private int damage; public void setTarget(AntiAirCraftGun newTarget) { this.target = newTarget; } //rest of Bomber class }

Page 23: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 23

Association - Composition

Composition gives us a 'part-of' relationship. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it.

public class Engine{ . . . }public class Car{ Engine e = new Engine(); .......}

As you can see in the example code above, Car manages the lifetime of Engine.

Page 24: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 24

Association - Aggregation

Within aggregation, the lifetime of the part is not managed by the whole.

public class Address{ . . .}public class Person{ private Address address; public Person(Address address) { this.address = address; } . . .}

Person would then be used as follows:Address address = new Address();Person person = new Person(address);orPerson person = new Person( new Address() );

Page 25: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 25

Inheritance Inheritance is the process by which one object acquires the propertiesof another object. By use of inheritance, an object need only define allof its characteristics that make it unique within its class, it can inheritits general attributes from its parent.

Account

Checking Mortgage Loan

In C# there may be more than one rootIn C# an object may have more than one parent (immediate superclass)

But, Java has a single, strict hierarchy

Page 26: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 26

Inheritance

Page 27: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 27

Inheritance

Page 28: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 28

Interface

Page 29: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 29

Polymorphism

Classical paradigmMust explicitly invoke correct version

Polymorphism(from Greek, meaning“many forms”) is a feature that allows one interface to be used for a general class of actions, i.e. one interface, multiple methods.

Page 30: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 30

Polymorphism

Object-oriented paradigm

All that is needed is myFile.open()Correct method invoked at run-time (dynamically)Method open can be applied to objects of different classes

Page 31: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 31

Overriding

Overloading

Genericity

public class MyListUtils<T> { ArrayList<T> copy(T[] list) { ArrayList<T> list2 = new ArrayList<T>(); for (int i = 0; i < obj.length; i++) list2.add(list[i]); return false; }}

Polymorphism

Page 32: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 32

Another Example

Page 33: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 33

Further Studies

And vast resources over Internet and Library books, such as:

http://www.youtube.com/watch?v=C2hSJOS9pks -> Introduction to UMLhttp://www.youtube.com/watch?v=NtfJxA1J3h8 -> OO Programming

Page 34: Introduction to Java &.NET SEAP/BTIS/DB&I/Aug 2010 1 Introduction to Java &.NET  A step to equip our team with the OO programming basics  An initiative.

Introduction to Java and .NETSEAP/BTIS/DB&I/Aug 2010 34

Thank You!