chapter iv programming concept berorientasi object
to get of best of programming java, must understand well concept
programming berorientasi object, because java be language programming
berorientasi object. in part this be discussed important concepts in
programming berorientasi object, so that supposed we more easy in
study language java.
a.
object
in base it all thing there at world real can be assumed.
as a object. if look at more advanced, in base it there two
prima facie characteristics in a object, that is:
every object has attribute as status then
called as state.
every object has behavior will sold out then be called
as behaviour.
the simple example: bike object
has attribute (state): pedal, wheel, bar, and colour.
has behavior sold out (behaviour): ascending the speed,
the speed decreaseds, and bike tooth transfer.
in development ware soft berorientasi object, object
in ware soft will keep state- in variable and
keep behaviour information (behaviour) in method or
functions/procedure.
class
class differ with object. class be prototype
define variables and method according to general.
while object in other side is instansiasi from a class.
practice 10. class. java
class book {
string writer;
string title;
void isi(string isi1, string isi2) {
title = isi1;
writer = isi2;
}
25
void cetakkelayar() {
if(judul==null & pengarang==null) return;
system. out. println(" title: " + title +
" , writer: " + writer);
}
}
class essay {
public static void main(string[ args) {
book a, b, c, d;
a = b = c = d = new buku();
a. isi(" programming java" , " asep herman suyanto" );
a. cetakkelayar();
b. isi(null, null);
b. cetakkelayar();
c. isi(null, " champion prasetyo hendriyanto" );
c. cetakkelayar();
d. isi(" web programming" , null);
d. cetakkelayar();
}
}
enkapsulasi
in a object that contain variables and method-
method, can be determined right access in a variable or method from
object. variable enfolding and method in a object in part
protected here's called with enkapsulasi. so, enkapsulasi
can be interpretted as parcel (wrapper) patron program and data
being cultivated. this wrapper defines behaviour and protect
program and data being cultivated so that not being accessinged just any by
program other.
26
benefit from process enkapsulasi:
modularitas
source code from a object can be managed independently from
other object source code.
information hiding
because we can determine access right a variable/method from
object, with such we can hide information
unnecessary known object other.
inheritance
class can be defined with reference in class other
definition. inheritance be inheritance attribute and method in
a class got from class definition. every
subclass will inherit state (variables) and behaviour (method-
method) from superclass-. subclass then can add state
and behaviour new specific and can also modify (override)
state and behaviour that demoted by superclass-.
profit from inheritance:
subclass prepare state/behaviour specific
distinguish it with superclass, this matter will make
programmer java to use repeat source code from
superclass that there.
programmer java can define superclass special
has generik, called abstract class, to define
class with behaviour and state in general.
term in inheritance necessary payed:
extends
keyword this must we add in definition class be
subclass.
superclass
superclass used to show hierarchy class mean
class base from subclass/class child.
subclass
subclass class child or descendant according to hierarchy from superclass.
27
super
keyword this is used to call engine-builder from superclass
or be variable that threaten in superclass.
methode overriding
pendefinisian repeat method same in subclass.
in inheritance, method overriding differ with method
overloading. if method overriding define return
method same, good name method also signature or parameter
need in subclass, if method overloading define
method that has name same, but with signature different
in definition class same.
practice 11. inheritance. java
class a {
int x;
int y;
void tampilkannilaixy() {
system. out. println(" value x: " + x +" , y: " + y);
}
}
class b extends a {
int z;
void tampilkanjumlah() {
/subclass can accessing member from superclass
system. out. println(" total: " + (x+y+z);
}
}
class inheritance {
public static void main(string[ args) {
a varsuperob = new a();
b varsubob = new b();
system. out. println(" superclass" );
varsuperob. x = 10;
varsuperob. y = 20;
varsuperob. tampilkannilaixy();
28
system. out. println(" subclass" );
/member superclass can be accessinged from subclass he
varsubob. x = 5;
varsubob. y = 4;
varsubob. tampilkannilaixy();
system. out. println(" subclass total" );
/member addition there's only in subclass
varsubob. z = 30;
varsubob. tampilkanjumlah();
system. out. println(" subclass" );
varsubob. x = 15;
varsubob. y = 14;
varsubob. tampilkannilaixy();
system. out. println(" superclass" );
varsuperob. x = 10;
varsuperob. y = 20;
/super. x = 100; error
/super. y = 200; error
varsuperob. tampilkannilaixy();
system. out. println(" subclass total" );
varsubob. z = 60;
varsubob. tampilkanjumlah();
}
}