You are on page 1of 26

Introduction to Java

and object-oriented programming


Hello World!
Let’s print “Hello World”

Bina Nusantara Kemanggisan


Hello World!
All you need to do is type System.out.println(“Hello World”);

Bina Nusantara Kemanggisan


Loops?
Make an incrementing line (i.e. 0 1 2 3 4 5 6 7 8 9)

Bina Nusantara Kemanggisan


Loops?
To loop use, for(int i=0; i<10; i++) System.out.println(i+” “);

Bina Nusantara Kemanggisan


Let’s start learning
object-oriented

Bina Nusantara Kemanggisan


Object-oriented?
Code using the concept of “objects”, implementing this paradigm is called
object-oriented programming

Bina Nusantara Kemanggisan


Object-oriented?
Code using the concept of “objects”, implementing this paradigm is called
object-oriented programming

Bina Nusantara Kemanggisan


Object-oriented?
Each object has its’ own attributes and functions. A constructor is usually made
to initialize the creation of an object.

Bina Nusantara Kemanggisan


Implementation
1. Make classes, lots of classes!
2. Set class attributes.
3. Make a constructor!

Bina Nusantara Kemanggisan


Encapsulation
Encapsulation is used for information hiding.
Psstt… you don’t want your data being public! (it’s dangerous)

Bina Nusantara Kemanggisan


Encapsulation
There are 3 types of encapsulation:
public, protected and private

Bina Nusantara Kemanggisan


Encapsulation
Attributes that are public can be accessed by any class.
Attributes that are protected can only be accessed by child classes.
Attributes that are private can only be accessed by the class itself!

Bina Nusantara Kemanggisan


Implementation
Easy! Just add public, protected or private in front of your attribute / function
i.e. private int price;

Bina Nusantara Kemanggisan


Inheritance
Inheritance is used to make a child class inherit attributes of its’ parent class

Bina Nusantara Kemanggisan


Inheritance
Two important terms to remember for inheritance is parent and child

Bina Nusantara Kemanggisan


Inheritance
A child has all the attributes and functions of its parents (not the other way around)

Bina Nusantara Kemanggisan


Implementation
In Java inheritance can be implemented by using extends
i.e. public class Hatchback extends Car

In the example above, it means the class Hatchback is a child of the Car parent class.

Bina Nusantara Kemanggisan


Polymorphism
Polymorphism means “multiple forms”, essentially that is a literal
definition of polymorphism in object-oriented programming

Bina Nusantara Kemanggisan


Polymorphism
Polymorphism has variant implementations

Bina Nusantara Kemanggisan


Implementation
1. Referencing (having multiple inheritance thus making it polymorphic)
2. Virtual Methods (overriding parent class methods)

Bina Nusantara Kemanggisan


Oh… one more thing
overloading functions

Bina Nusantara Kemanggisan


Overloading Functions
Overloading functions are functions with the same name but have
different formal parameters

Bina Nusantara Kemanggisan


Implementation
public int a(){
This is an example of a overloading function. In
//functionstuff
this example, function a could receive two types of
}
parameters none or when an integer is passed.

public int a(int x){


Thus we can choose to implement a certain
//functionstuff
function by passing different parameters.
}

Bina Nusantara Kemanggisan


Study Reference
1. Tutorials Point: Java - Encapsulation
https://www.tutorialspoint.com/java/java_encapsulation.htm
2. Tutorials Point: Java - Inheritance
https://www.tutorialspoint.com/java/java_inheritance.htm
3. Tutorials Point: Java - Polymorphism
https://www.tutorialspoint.com/java/java_polymorphism.htm

Bina Nusantara Kemanggisan


and we’re done!

Bina Nusantara Kemanggisan

You might also like