menu :
- this keyword
- his keyword
- overloading and overriding
- polymorphism
- variable and method scoping
- parameter passing
- package
custom java class
- modifying string, int, calendar, array, we can do it by using existing classes
- when we found another unique entity, we need to custom a class
using THIS keyword
- THIS adalah reference ke variable yang ada pada object itu sendiri
- dengan kata lain THIS adalah self referencing pointer
THIS using example
public class Rectangle{
private double length;
private double width;
public Rectangle(){
}
public void setLength(double length){
this.length = length;
}
public void setWidth(double width){
this.width = width;
}
}
examine this following snippet
- Rectangle kotak = new Rectangle()
- Rectangle is class method, owned by class, no need for isntantiation
- to use : Rectangle.ClassMethod();
- kotak = object, the instance method, owned by instance, need instantiation to use
- to use : kotak.InstanceMethod();
to make an instance method
- public static double compute(double length, double width)
- by adding static there
- need argument, it cant use a variable from the object
overloaded method
- is when methods with same name, is made with different signature
- example, the method print
- print can do it for many kinds
multiple constructors
- using THIS in a constructor means its calling other constructor
- ex :
public Rectangle(){
this(0., 0.);
}
public Rectangle(double length){
this(length, length);
}
public Rectangle(double length, double width){
setLength(length);
setWidth(width);
}
- the first Rectangle calls the second, and the second call the third
static initializers
- make the code get executed when a java class is loaded into the java system
- ex :
public class Point {
private static long x , y ;
static{
x = System.currentTimeMillis();
y = ++x;
}
public Point() {}
public static long getXY(){
return x+y;
}
}
- the code inside static is executed firstly after the class is loaded
overriding
- modify the behaviour of superclass method by rewriting it in the subclass
- tehe signature must exactly be the same
- ex :
public class Box extends Rectangle{
private double height;
public Box(){
this(0. , 0. , 0.);
}
public Box(double length, double width, double height){
super(length, width);
this.height = height;
}
public Box(double size){
this(size, size, size);
}
- Box is the subclass of Rectangle
- using THIS called the variable in that class
- using SUPER called the variable in its superclass
polymorphism
- where there are more classes extending from one including the superclass,
and most used as a bridge to get a value from its method
- ex :
public class TestShapes {
public static void main(String[] args) {
Rectangle square, dice, brick, rectangle;
square = new Rectangle(4. );
dice = new Box(3. );
brick = new Box(4. ,3. , 2.);
rectangle = new Rectangle(2. , 1.);
System.out.println(square);
System.out.println(dice);
System.out.println(brick);
System.out.println(rectangle);
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment