SlideShare a Scribd company logo
Java Language and OOP
Part III
By 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.java
public 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
 public
package 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
 public
public int getValue();
Visible to all packages everywhere
 private
private int getValue();
Visible only inside class
 protected
protected int getValue();
Visible only to classes in the same package and subclass
Thank You

More Related Content

What's hot (18)

PDF
Java Day-7
People Strategists
 
PDF
Java Day-6
People Strategists
 
PPT
Core java concepts
Ram132
 
PDF
Java Day-4
People Strategists
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPT
Java tutorials
saryu2011
 
PPT
Java Basics
sunilsahu07
 
PPT
Core Java Concepts
mdfkhan625
 
PPT
Java Tutorials
Woxa Technologies
 
PDF
Java Day-5
People Strategists
 
PPT
Java
Prabhat gangwar
 
PPT
java training faridabad
Woxa Technologies
 
PPT
Core java
kasaragaddaslide
 
PPTX
Presentation 4th
Connex
 
PPTX
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Java inheritance
Hamid Ghorbani
 
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
PPT
core java
Vinodh Kumar
 
Java Day-7
People Strategists
 
Java Day-6
People Strategists
 
Core java concepts
Ram132
 
Java Day-4
People Strategists
 
Core java complete ppt(note)
arvind pandey
 
Java tutorials
saryu2011
 
Java Basics
sunilsahu07
 
Core Java Concepts
mdfkhan625
 
Java Tutorials
Woxa Technologies
 
Java Day-5
People Strategists
 
java training faridabad
Woxa Technologies
 
Core java
kasaragaddaslide
 
Presentation 4th
Connex
 
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Java inheritance
Hamid Ghorbani
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
core java
Vinodh Kumar
 

Viewers also liked (20)

PPT
Object Oriented Programming Concepts
thinkphp
 
PPT
03 objects and classes in java
MrMazharJutt
 
PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPTX
Diffrerent Modes Of Investment of IBBL-BUBT
al-amin shourov
 
PDF
Access modifiers in java
Muthukumaran Subramanian
 
PPT
Oop Introduction
guest7b957c71
 
PPTX
Java
Sneha Mudraje
 
PPT
Java Stack Traces
dbanttari
 
PDF
Linked list (java platform se 8 )
charan kumar
 
PPT
Abstract data types
Luis Goldster
 
PPTX
Heap and stack space in java
Talha Ocakçı
 
PPTX
Java Stack (Pilha)
Samuel Santos
 
PDF
Introduction to java and oop
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
PPTX
Java static keyword
Ahmed Shawky El-faky
 
PPTX
01 introduction to oop and java
রাকিন রাকিন
 
PPTX
Java Collections Framework Inroduction with Video Tutorial
Marcus Biel
 
PPT
Stack, queue and hashing
Dumindu Pahalawatta
 
PDF
JAVA Object Oriented Programming (OOP)
Prof. Erwin Globio
 
Object Oriented Programming Concepts
thinkphp
 
03 objects and classes in java
MrMazharJutt
 
Constructor and destructor
Shubham Vishwambhar
 
Diffrerent Modes Of Investment of IBBL-BUBT
al-amin shourov
 
Access modifiers in java
Muthukumaran Subramanian
 
Oop Introduction
guest7b957c71
 
Java Stack Traces
dbanttari
 
Linked list (java platform se 8 )
charan kumar
 
Abstract data types
Luis Goldster
 
Heap and stack space in java
Talha Ocakçı
 
Java Stack (Pilha)
Samuel Santos
 
Constructor in java
Pavith Gunasekara
 
Polymorphism
Nochiketa Chakraborty
 
Java static keyword
Ahmed Shawky El-faky
 
01 introduction to oop and java
রাকিন রাকিন
 
Java Collections Framework Inroduction with Video Tutorial
Marcus Biel
 
Stack, queue and hashing
Dumindu Pahalawatta
 
JAVA Object Oriented Programming (OOP)
Prof. Erwin Globio
 
Ad

Similar to 03 Java Language And OOP Part III (20)

PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPTX
Java
nirbhayverma8
 
PPTX
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
PPTX
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
PPTX
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
PPTX
unit 2 java.pptx
AshokKumar587867
 
PPT
packages and interfaces
madhavi patil
 
PPTX
Unit3 part1-class
DevaKumari Vijay
 
PPTX
Lecture 5
talha ijaz
 
PPTX
Ch-2ppt.pptx
ssuser8347a1
 
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
PPTX
Java basics
Shivanshu Purwar
 
PPTX
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
PPTX
JAVA-PPT'S.pptx
RaazIndia
 
PPTX
Android Training (Java Review)
Khaled Anaqwa
 
PPT
Lecture 2 classes i
the_wumberlog
 
PPT
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
PPTX
Class and Object.pptx
Hailsh
 
PPTX
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Java PPT OOPS prepared by Abhinav J.pptx
JainSaab2
 
Classes, Objects and Method - Object Oriented Programming with Java
Radhika Talaviya
 
UNIT I OOP AND JAVA FUNDAMENTALS CONSTRUCTOR
mohanrajm63
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
unit 2 java.pptx
AshokKumar587867
 
packages and interfaces
madhavi patil
 
Unit3 part1-class
DevaKumari Vijay
 
Lecture 5
talha ijaz
 
Ch-2ppt.pptx
ssuser8347a1
 
JAVA Class Presentation.pdf Vsjsjsnheheh
AnushreeP4
 
Java basics
Shivanshu Purwar
 
JAVA-PPT'S-complete-chrome.pptx
KunalYadav65140
 
JAVA-PPT'S.pptx
RaazIndia
 
Android Training (Java Review)
Khaled Anaqwa
 
Lecture 2 classes i
the_wumberlog
 
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Class and Object.pptx
Hailsh
 
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
Ad

Recently uploaded (20)

PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Executive Business Intelligence Dashboards
vandeslie24
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 

03 Java Language And OOP Part III

  • 1. Java Language and OOP Part III By Hari Christian
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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
  • 7. 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(); }
  • 8. Object - Definition • An object is an instance of a class • An object belongs to a class • An object belongs to a reference type
  • 9. Create and Declare an Object • CarTest.java public 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(); } }
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Constructor • There is Instance Initializer similar to constructor { System.out.println("I am an instance initializer!"); } • No Destructors
  • 14. Constructor • Example: public class Parent { public Parent() { } // default constructor public static void main(String[] args) { Parent p = new Parent(); } }
  • 15. Constructor • Example: public class Parent { public Parent() { System.out.println(“PARENT”); } public static void main(String[] args) { Parent p = new Parent(); } }
  • 16. 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? } }
  • 17. 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? } }
  • 18. 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”); } }
  • 19. 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? } }
  • 20. Constructor • Example: public class Parent { public Parent() { System.out.println(“PARENT ”); } } public class Child extends Parent { public Child() { System.out.println(“CHILD”); } }
  • 21. 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? } }
  • 22. 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 }
  • 23. 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”); } }
  • 24. 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; }
  • 25. Methods - Basic • Example: public int addition(int num1, int num2) { return num1 + num2; }
  • 26. 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.
  • 27. 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); } }
  • 28. 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.
  • 29. 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()); } }
  • 30. 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
  • 31. Methods - Overload • Example: public class Method { public static void hitung(int number) { } public static void hitung(double number) { } }
  • 32. Methods - Overload • Example: public class Method { public static void addition(int num1, int num2) { } public static void addition(double num1, double num2) { } }
  • 33. Methods - Overload • Example: public class Method { public static void calcAge(Date birthDate) { } public static void calcAge(Date date1, Date date2) { } }
  • 34. 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.
  • 35. 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
  • 36. Transfer of Control Statement - Return • Example: void deleteCustomer(Customer c) { if (c == null) return; // Delete Code here }
  • 37. 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; }
  • 38. 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
  • 39. 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 {}
  • 40. 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
  • 41. 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); }
  • 42. 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); }
  • 43. 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); }
  • 44. 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); }
  • 45. 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 }
  • 46. 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); } }
  • 47. 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); } }
  • 48. 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”); } }
  • 49. 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
  • 50. 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  public package a.b.c; public class D { /*some code */ } Class a.b.c.D will be visible to all packages everywhere
  • 51. Access Modifier – field/method  default int getValue(); Visible only to classes in the same package  public public int getValue(); Visible to all packages everywhere  private private int getValue(); Visible only inside class  protected protected int getValue(); Visible only to classes in the same package and subclass