Upload
wesley-rush
View
69
Download
5
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
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• Primitive Types vs. Reference Types• Static • Object Vs. Class
Objects
• Inspired by real life.• Look around you, everything is object.• Example: – Marker in my hand. – Computer – Table– Chair– etc…
Object (Cont.)
MetoMeto
Illustration
FieldsFields
MethodsMethods
MethodsMethodsMethodsMethods
MethodsMethods
A Pen
• Fields– Height– Radius – Color
• Method– Can open it’s cap– Can write– Can close it’s cap
Car• Fields
– Dimensions– Speed– Gear– Direction– Number of Wheels– Number of Seats – Number of Doors
• Methods– Open door– Start car– Accelerate– Break
Object in Action
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
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.
}}
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
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
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.
Primitive Types vs. Reference Types
Object Vs Class
Basic flow of class and object
Static!!
this Reference
Core Conception of OOP
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.
Encapsulation - Example
Let’s try some examples
• Write a class of Triangle.• Fields– Base– Height
• Methods– Constructor, Getter and Setters– calculateArea
Let’s try some examples
• Now take use input of base and height• Then create object depending on the user
input.
Let’s try points
Back to triangle
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.