Top Banner
Java Language and OOP Part III By Hari Christian
52

03 Java Language And OOP Part III

Jan 15, 2015

Download

Software

Hari Christian

03 Java Language And OOP Part III
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 03 Java Language And OOP Part III

Java Language and OOP Part IIIBy Hari Christian

Page 2: 03 Java Language And OOP Part III

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

Page 3: 03 Java Language And OOP Part III

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

Page 4: 03 Java Language And OOP Part III

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

Page 5: 03 Java Language And OOP Part III

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

Page 6: 03 Java Language And OOP Part III

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

Page 7: 03 Java Language And OOP Part III

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();

}

Page 8: 03 Java Language And OOP Part III

Object - Definition

• An object is an instance of a class

• An object belongs to a class

• An object belongs to a reference type

Page 9: 03 Java Language And OOP Part III

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();

}

}

Page 10: 03 Java Language And OOP Part III

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

Page 11: 03 Java Language And OOP Part III

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

Page 12: 03 Java Language And OOP Part III

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

Page 13: 03 Java Language And OOP Part III

Constructor

• There is Instance Initializer similar to constructor

{

System.out.println("I am an instance initializer!");

}

• No Destructors

Page 14: 03 Java Language And OOP Part III

Constructor

• Example:public class Parent {

public Parent() { } // default constructor

public static void main(String[] args) {

Parent p = new Parent();

}

}

Page 15: 03 Java Language And OOP Part III

Constructor

• Example:public class Parent {

public Parent() {

System.out.println(“PARENT”);

}

public static void main(String[] args) {

Parent p = new Parent();

}

}

Page 16: 03 Java Language And OOP Part III

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?

}

}

Page 17: 03 Java Language And OOP Part III

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?

}

}

Page 18: 03 Java Language And OOP Part III

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”);

}

}

Page 19: 03 Java Language And OOP Part III

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?

}

}

Page 20: 03 Java Language And OOP Part III

Constructor

• Example:public class Parent {

public Parent() {

System.out.println(“PARENT ”);

}

}

public class Child extends Parent {

public Child() {

System.out.println(“CHILD”);

}

}

Page 21: 03 Java Language And OOP Part III

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?

}

}

Page 22: 03 Java Language And OOP Part III

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

}

Page 23: 03 Java Language And OOP Part III

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”);

}

}

Page 24: 03 Java Language And OOP Part III

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;

}

Page 25: 03 Java Language And OOP Part III

Methods - Basic

• Example:public int addition(int num1, int num2) {

return num1 + num2;

}

Page 26: 03 Java Language And OOP Part III

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.

Page 27: 03 Java Language And OOP Part III

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);

}

}

Page 28: 03 Java Language And OOP Part III

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.

Page 29: 03 Java Language And OOP Part III

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());

}

}

Page 30: 03 Java Language And OOP Part III

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

Page 31: 03 Java Language And OOP Part III

Methods - Overload

• Example:public class Method {

public static void hitung(int number) {

}

public static void hitung(double number) {

}

}

Page 32: 03 Java Language And OOP Part III

Methods - Overload

• Example:public class Method {

public static void addition(int num1, int num2) {

}

public static void addition(double num1, double num2) {

}

}

Page 33: 03 Java Language And OOP Part III

Methods - Overload

• Example:public class Method {

public static void calcAge(Date birthDate) {

}

public static void calcAge(Date date1, Date date2) {

}

}

Page 34: 03 Java Language And OOP Part III

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.

Page 35: 03 Java Language And OOP Part III

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

Page 36: 03 Java Language And OOP Part III

Transfer of Control Statement - Return

• Example:

void deleteCustomer(Customer c) {

if (c == null) return;

// Delete Code here

}

Page 37: 03 Java Language And OOP Part III

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;

}

Page 38: 03 Java Language And OOP Part III

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

Page 39: 03 Java Language And OOP Part III

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 {}

Page 40: 03 Java Language And OOP Part III

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

Page 41: 03 Java Language And OOP Part III

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);

}

Page 42: 03 Java Language And OOP Part III

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);

}

Page 43: 03 Java Language And OOP Part III

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);

}

Page 44: 03 Java Language And OOP Part III

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);

}

Page 45: 03 Java Language And OOP Part III

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

}

Page 46: 03 Java Language And OOP Part III

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);

}

}

Page 47: 03 Java Language And OOP Part III

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);

}

}

Page 48: 03 Java Language And OOP Part III

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”);

}

}

Page 49: 03 Java Language And OOP Part III

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

Page 50: 03 Java Language And OOP Part III

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

Page 51: 03 Java Language And OOP Part III

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

Page 52: 03 Java Language And OOP Part III

Thank You