Java Training
-
Upload
wesley-rush -
Category
Documents
-
view
69 -
download
5
description
Transcript of Java Training
![Page 1: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/1.jpg)
Session 2: Introduction to Object Oriented Programming
![Page 2: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/3.jpg)
Objects
• Inspired by real life.• Look around you, everything is object.• Example: – Marker in my hand. – Computer – Table– Chair– etc…
![Page 4: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/4.jpg)
Object (Cont.)
![Page 5: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/5.jpg)
MetoMeto
Illustration
FieldsFields
MethodsMethods
MethodsMethodsMethodsMethods
MethodsMethods
![Page 6: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/6.jpg)
A Pen
• Fields– Height– Radius – Color
• Method– Can open it’s cap– Can write– Can close it’s cap
![Page 7: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/8.jpg)
![Page 9: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/9.jpg)
Object in Action
![Page 10: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/15.jpg)
Primitive Types vs. Reference Types
![Page 16: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/16.jpg)
Object Vs Class
![Page 17: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/17.jpg)
Basic flow of class and object
![Page 18: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/18.jpg)
Static!!
![Page 19: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/19.jpg)
this Reference
![Page 20: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/20.jpg)
Core Conception of OOP
![Page 21: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/21.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/22.jpg)
Encapsulation - Example
![Page 23: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/23.jpg)
Let’s try some examples
• Write a class of Triangle.• Fields– Base– Height
• Methods– Constructor, Getter and Setters– calculateArea
![Page 24: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/25.jpg)
Let’s try points
![Page 26: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/26.jpg)
Back to triangle
![Page 27: Java Training](https://reader033.fdocuments.in/reader033/viewer/2022061618/56813861550346895da00ef3/html5/thumbnails/27.jpg)
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.