27
Session 2: Introduction to Object Oriented Programming

Java Training

Embed Size (px)

DESCRIPTION

Java Training. Session 2: Introduction to Object Oriented Programming. Objectives. Classes, Objects, Methods, Instance Variables Instantiating an Object of a Class Initializing Objects with Constructors Methods with Parameters Instance Variables, set Methods and get Methods - PowerPoint PPT Presentation

Citation preview

Page 1: Java Training

Session 2: Introduction to Object Oriented Programming

Page 2: Java Training

Objectives• Classes, Objects, Methods, Instance Variables• Instantiating an Object of a Class• Initializing Objects with Constructors• Methods with Parameters• Instance Variables, set Methods and get Methods• Primitive Types vs. Reference Types• Static • Object Vs. Class

Page 3: Java Training

Objects

• Inspired by real life.• Look around you, everything is object.• Example: – Marker in my hand. – Computer – Table– Chair– etc…

Page 4: Java Training

Object (Cont.)

Page 5: Java Training

MetoMeto

Illustration

FieldsFields

MethodsMethods

MethodsMethodsMethodsMethods

MethodsMethods

Page 6: Java Training

A Pen

• Fields– Height– Radius – Color

• Method– Can open it’s cap– Can write– Can close it’s cap

Page 7: Java Training

Car• Fields

– Dimensions– Speed– Gear– Direction– Number of Wheels– Number of Seats – Number of Doors

• Methods– Open door– Start car– Accelerate– Break

Page 8: Java Training
Page 9: Java Training

Object in Action

Page 10: Java Training

Class: GradeBook, File: GradeBook.java

public class GradeBook{

private String courseName;private String courseTeacher;public GradeBook(String name){

courseName = name;}

public void setCourseName(String name){

courseName = name;}

public String getCourseName(){

return courseName;}public void displayMessage(){

System.out.println("CourseName is: " + courseName);}public String getCourseTeacher(){

return courseTeacher;}

}

Instance Variables –Note the naming convention

Instance Variables –Note the naming convention

ConstructorConstructor

Public method -Carefully note the naming convention

Public method -Carefully note the naming convention

Page 11: Java Training

Class: GradeBookTest, File: GradeBookTest.java

public class GradeBookTest{

public static void main(Stringargs[ ]){

// Instantiate or create two GradeBook objectsGradeBook gb1 = new GradeBook(“OOPL”);GradeBook gb2 = new GradeBook(“DS”);

System.out.printf(“gb1 course name is: %s\n”, gb1.getCourseName());gb1.setCourseName(“DLD”);System.out.printf(“gb1 course name is: %s\n”, gb1.getCourseName());System.out.printf(“gb1 course teacher is: %s\n”, gb1.getCourseTeacher());// The above statement is trying to print something that is not initialized yet.

}}

Page 12: Java Training

Executing the Program

• To compile the source files type– javacGradeBook.javaGradeBookTest.java– Or, javac *.java

• It produces two class files named GradeBook.class and GradeBookTest.class

• To run the program type– java GradeBookTest

• We used GradeBookTest as the argument to java as class GradeBookTest contains the “main” method.

• If we use “java GradeBook” then the JVM will throw an ERROR (more dangerous than an EXCEPTION) and will terminate immediately.– Exception in thread “main” java.lang.NoSuchMethodError: main

Page 13: Java Training

Output of “java GradeBookTest”

The String courseTeacher is “null”.

The String courseTeacher is “null”.

But using a reference which is “null” will cause unexpected results or exceptions in many cases

But using a reference which is “null” will cause unexpected results or exceptions in many cases

Page 14: Java Training

Some Words on Multiple Classes in the Same Directory

• There is a special relationship between classes that are compiled in the same directory on disk.

• By default, such classes are considered to be in the same package – known as the default package.

• Classes in the same package are implicitly imported into the source code files of other classes in the same package.

• Thus, an import declaration is not required when one class in a package uses another in the same package.

Page 15: Java Training

Primitive Types vs. Reference Types

Page 16: Java Training

Object Vs Class

Page 17: Java Training

Basic flow of class and object

Page 18: Java Training

Static!!

Page 19: Java Training

this Reference

Page 20: Java Training

Core Conception of OOP

Page 21: Java Training

Encapsulation

• The most important thing software engineers want to achieve.

• It is a mechanism for restricting access to some of the object's components.

• Provide user methods to interact but user directly can not change the fields.

Page 22: Java Training

Encapsulation - Example

Page 23: Java Training

Let’s try some examples

• Write a class of Triangle.• Fields– Base– Height

• Methods– Constructor, Getter and Setters– calculateArea

Page 24: Java Training

Let’s try some examples

• Now take use input of base and height• Then create object depending on the user

input.

Page 25: Java Training

Let’s try points

Page 26: Java Training

Back to triangle

Page 27: Java Training

Few things

• JRE – the package containing only JVM and related software. Can’t compile java codes with a jre.

• JDK – the package containing software to run and compile java code.

• Java Doc – All class function’s detail is provided in java doc format.– http://download.oracle.com/javase/6/docs/api/

• Java Tutorial – Official java tutorial from oracle.