Upload
hari-christian
View
82
Download
0
Embed Size (px)
DESCRIPTION
03 Java Language And OOP Part III
Citation preview
Java Language and OOP Part IIIBy Hari Christian
Agenda
• 01 Class - Definition• 02 Create and Declare a Class• 03 Object - Definition• 04 Create and Declare an Object• 05 Constructor - Basic• 06 Constructor - Overload
Agenda
• 07 Method - Basic• 08 Method - Passing by Value• 09 Method - Passing by Reference• 10 Method - Overload• 11 Return Statement• 12 Package• 13 Import• 14 Organizing Statement• 15 Access Modifier
Class - Definition
• When you write code in Java, you are writing classes or interfaces
• Within those classes, as you know, are variables and methods
• How you declare your classes, methods, and variables dramatically affects your code's behavior, for example, a public method can be accessed from code running anywhere in your application
Class - Definition
• Source File Declaration Rules:– There can be only one public class per source code file
– Comments can appear at the beginning or end of any line in the source code file
– If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java
– If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present
Class - Definition
• Source File Declaration Rules:– If there are import statements, they must go between the
package statement and the class declaration– If there isn't a package statement, then the import
statement(s) must be the first line(s) in the source code file– If there are no package or import statements, the class
declaration must be the first line in the source code file– Import and package statements apply to all classes within a
source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports
– A file can have more than one nonpublic class– Files with no public classes can have a name that does not
match any of the classes in the file
Create and Declare a Class
• Car.java
public class Car { // Only one public class
// Variable
int gear;
String color;
// Methods
public void accelerate();
public void brake();
}
Object - Definition
• An object is an instance of a class
• An object belongs to a class
• An object belongs to a reference type
Create and Declare an Object
• CarTest.javapublic class CarTest {
public static void main(String[] args) {
Car honda = new Car();
honda.setGear(5);
honda.setColor(“white”);
Car Ferrari = new Car();
Car Pagani = new Car();
}
}
Constructor
• Constructor is called when create a new object
• Constructor allocates memory for a new instance of an object
• Constructor typically initialize the fields of the new object
Constructor
• Constructor has the same name as the class
• Constructor is written without return type
• All classes always have at least one constructor
• A constructor in the object's parent class is always called
Constructor
• Constructor has the same name as the class
• Constructor is written without return type
• All classes always have at least one constructor
• A constructor in the object's parent class is always called
Constructor
• There is Instance Initializer similar to constructor
{
System.out.println("I am an instance initializer!");
}
• No Destructors
Constructor
• Example:public class Parent {
public Parent() { } // default constructor
public static void main(String[] args) {
Parent p = new Parent();
}
}
Constructor
• Example:public class Parent {
public Parent() {
System.out.println(“PARENT”);
}
public static void main(String[] args) {
Parent p = new Parent();
}
}
Constructor
• Example:public class Parent {
public Parent() {
System.out.println(“PARENT”);
}
public static void main(String[] args) {
Parent p = new Parent(“TEST”); // Error or not?
}
}
Constructor
• Example:public class Parent {
public Parent() {
System.out.println(“PARENT”);
}
public static void main(String[] args) {
Parent p = new Parent(); // Error or not?
Parent p = new Parent(“TEST”); // Error or not?
}
}
Constructor
• Example:public class Parent {
public Parent(String name) {
System.out.println(“PARENT = ” + name);
}
public static void main(String[] args) {
Parent p = new Parent(“Hari”);
}
}
Constructor
• Example:public class Parent {
public Parent(String name) {
System.out.println(“PARENT = ” + name);
}
public static void main(String[] args) {
Parent p = new Parent(); // Error or not?
Parent p = new Parent(“TEST”); // Error or not?
}
}
Constructor
• Example:public class Parent {
public Parent() {
System.out.println(“PARENT ”);
}
}
public class Child extends Parent {
public Child() {
System.out.println(“CHILD”);
}
}
Constructor
• Example:public class TestConstructor {
public static void main(String[] args) {
Parent p = new Parent(); // Error or not?
Child c = new Child(); // Error or not?
}
}
Constructor
public class Employee {
private String nik;
private String name;
public Employee() {
}
public Employee(String nik, String name) {
this.nik = nik;
this.name = name;
}
// Setter Getter
}
Constructor - Overload
• Example:public class TestEmployee {
public static void main(String[] args) {
Employee h = new Employee(“1”, “Hari”);
Employee r = new Employee();
r.setNik(“2”);
r.setNik(“Rifki”);
}
}
Methods - Basic
• Methods are the OOP name for functions
• A method is always declared inside a class
• Format:[access] [modifiers] returnType methodName([param]) [throw] {
Statement;
}
Methods - Basic
• Example:public int addition(int num1, int num2) {
return num1 + num2;
}
Methods – Passing by Value
• "Passing by value" means that the argument's value is copied and is passed to the method. Inside the method you can modify the copy at will, and the modifications don't affect the original argument.
Methods - Passing by Value
• Example:public class Method {
public static void main(String[] args) {
int number = 5;
hitung(number);
System.out.println(“[main] number = ” + number);
}
public static void hitung(int number) {
number = 7;
System.out.println(“[hitung] number = ” + number);
}
}
Methods - Passing by Reference
• "Passing by reference" means that a reference to (i.e., the address of) the argument is passed to the method. Using the reference, the method is actually directly accessing the argument, not a copy of it. Any changes the method makes to the parameter are made to the actual object used as the argument. After you return from the method, that object will retain any new values set in the method.
Methods - Passing by Reference
• Example:public class Method {
public static void main(String[] args) {
Student s = new Student();
input(s);
System.out.println(“[main] nim = ” + s.getNim());
}
public static void input(Student s) {
s.setNim(“007”);
System.out.println(“[input] nim = ” + s.getNim());
}
}
Methods - Overload
• We can have several methods with the same name in a class
• This same methods is called Overload Methods
• This same methods must have DIFFERENT parameter
Methods - Overload
• Example:public class Method {
public static void hitung(int number) {
}
public static void hitung(double number) {
}
}
Methods - Overload
• Example:public class Method {
public static void addition(int num1, int num2) {
}
public static void addition(double num1, double num2) {
}
}
Methods - Overload
• Example:public class Method {
public static void calcAge(Date birthDate) {
}
public static void calcAge(Date date1, Date date2) {
}
}
Methods - Chaining
• Example:String x = "abc";
String y = x.concat("def").toUpperCase().replace('C','x');
System.out.println("y = " + y); // result is "y = ABxDEF“
• Tips: read it from left, use the result to invoking 2nd methods and so on.
Transfer of Control Statement - Return
• Format:
return;
return Expression;
• Explanation: return, is only used in method with no return value return Expression, is always used in every method
that actually does return a value
Transfer of Control Statement - Return
• Example:
void deleteCustomer(Customer c) {
if (c == null) return;
// Delete Code here
}
Transfer of Control Statement - Return
• Example:
int addition(int num1, int num2) {
return num1 + num2;
}
OR
int addition(int num1, int num2) {
int result = num1 + num2;
return result;
}
Package
• Must be unique• Package = Directory
• The identifier rules applied
• There are thus three purposes to packages:– To organize namespaces in your program so your
classes don't collide with each other– To organize namespaces for all organizations, so all
program names don't collide with each other– To control visibility of classes
Package
• Package a.b.c.D =– a
• b– c
» D.java
• Put in the very beginning of Java Code. Ex:
package id.ac.istb.javatraining
public class Test {}
Import
• You do import when you calling a class from another package
• Only one class with the same name can be imported
• There 3 ways to use class from another package– Import whole class in package– import the spesific class– Using full name
Import Whole Package
• Import whole class in package example:
package id.ac.istb.javatraining
import java.util.*; // Use * to import whole class
public class TestImport {
// Scanner is belong to java.util package
Scanner scan = new Scanner(System.in);
}
Import The Spesific Class
• Import the spesific class example:
package id.ac.istb.javatraining
import java.util.Scanner;
public class TestImport {
Scanner scan = new Scanner(System.in);
}
Import Full Name
• Using full name example:package id.ac.istb.javatraining
public class TestImport {
java.util.Scanner scan = new java.util.Scanner(System.in);
}
Import Static
• Static import example (new since JDK 5):• Without static import:public class TestImport {
System.out.println(Integer.MAX_VALUE);
}
• With static import:import static java.lang.Integer.*;
public class TestImport {
System.out.println(MAX_VALUE);
}
Organizing Statement - Local
• Local variable
Local variable live only inside the block of if, loop or method
• Example:
boolean valid = true
if (valid) {
int number = 5; // local variable
}
Organizing Statement - Instance
• Instance variable / class variable
Instance variable live only inside the class
• Example:public class Test {
static int number = 5; // instance variable
public static void main(String[] args) {
System.out.println(number);
}
}
Organizing Statement - This
• Using this keyword
• Example:public class Test {
static int number = 5; // instance variable
public static void main(String[] args) {
int number = 10; // local variable
System.out.println(number);
System.out.println(this.number);
}
}
Empty Statement
• Empty statement (semi colon) does nothing
• Example:public static void main(String[] args) {
; // empty variable
if (true) {
; // empty variable
} else {
System.out.println(“Do Something Here”);
}
}
Access Modifier
• There are four access modifier in Java:
default •Visible only in the same package
private •Visible only in the same class
protected •Visible only in the same class or subclass
public •Visible everywhere
Access Modifier - class default
package a.b.c;
class D { /*some code */ }
Class D will be visible only to classes in the same
package a.b.c, (typically this means the same directory)
and nowhere else
publicpackage a.b.c;
public class D { /*some code */ }
Class a.b.c.D will be visible to all packages everywhere
Access Modifier – field/method default
int getValue();
Visible only to classes in the same package
publicpublic int getValue();
Visible to all packages everywhere
privateprivate int getValue();
Visible only inside class
protectedprotected int getValue();
Visible only to classes in the same package and subclass
Thank You