Sei sulla pagina 1di 13

PEMROGRAMAN LANJUT

Sistem Informasi FILKOM UB


Semester Genap 2016/2017

LATIHAN SOAL
PEWARISAN / INHERITANCE

Dr. Eng. Herman Tolle


Fakultas Ilmu Komputer, Universitas Brawijaya
LATIHAN 1
• Buat Diagram Class dari suatu relasi Superclass dan Subclass
• Lengkapi dengan atribut dan fungsi yang berkesesuaian
LATIHAN 2
• Buatlah Superclass Circle dan Subclass Cylinder
• Buat fungsi Menghitung Luas (getArea()) pada Class Circle
• Buat fungsi Menghitung Volume dan Luas Permukaan (menggunakan
nama fungsi yang sama: getArea()) pada Class Cylinder yang
menggunakan gabungan fungsi yang telah ada pada Class Circle
(getArea())
OVERRIDING:
+getArea()
+toString
public class Circle { // save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;

public Circle() { // 1st constructor, which sets both radius and color to default
radius = 1.0;
color = "red";
}

public Circle(double r) { // 2nd constructor with given radius, but color default
radius = r;
color = "red";
}

// A public method for retrieving the radius


public double getRadius() {
return radius;
}

// A public method for computing the area of circle


public double getArea() {
return radius*radius*Math.PI;
}
}
// Define Cylinder class, which is a subclass of Circle
public class Cylinder extends Circle {
private double height; // Private member variable

public Cylinder() { // constructor 1


super(); // invoke superclass' constructor Circle()
height = 1.0;
}
public Cylinder(double radius, double height) { // Constructor 2
super(radius); // invoke superclass' constructor Circle(radius)
this.height = height;
}

public double getHeight() {


return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume() {
return getArea()*height; // Use Circle's getArea()
}
}
// override the getArea() method inherited from superclass Circle
@Override
public double getArea() {
return 2*Math.PI*getRadius()*height + 2*super.getArea();
}
// need to change the getVolume() as well
public double getVolume() {
return super.getArea()*height; // use superclass' getArea()
}
// override the inherited toString()
@Override
public String toString() {
return "Cylinder: radius = " + getRadius() + " height = " + height;
}
public class Cylinder extends Circle { //save as "Cylinder.java"
private double height; // private variable

// Constructor with default color, radius and height


public Cylinder() {
super(); // call superclass no-arg constructor Circle()
height = 1.0;
}

// Constructor with default radius, color but given height


public Cylinder(double height) {
super(); // call superclass no-arg constructor Circle()
this.height = height;
}

// Constructor with default color, but given radius, height


public Cylinder(double radius, double height) {
super(radius); // call superclass constructor Circle(r)
this.height = height;
}

// A public method for retrieving the height


public double getHeight() { return height; }

// A public method for computing the volume of cylinder


// use superclass method getArea() to get the base area
public double getVolume()
{ return getArea()*height; }
}
// A test driver program for Cylinder class
public class TestCylinder {
public static void main(String[] args) {
Cylinder cy1 = new Cylinder(); // Use constructor 1
System.out.println("Radius is " + cy1.getRadius()
+ " Height is " + cy1.getHeight()
+ " Color is " + cy1.getColor()
+ " Base area is " + cy1.getArea()
+ " Volume is " + cy1.getVolume());

Cylinder cy2 = new Cylinder(5.0, 2.0); // Use constructor 2


System.out.println("Radius is " + cy2.getRadius()
+ " Height is " + cy2.getHeight()
+ " Color is " + cy2.getColor()
+ " Base area is " + cy2.getArea()
+ " Volume is " + cy2.getVolume());
}
}
OVERRIDE & HIDE METHOD
Consider an example that contains two classes. The first is Animal, which contains one
instance method and one static method:
The second class, a subclass of Animal, is called Cat:

public class Animal { public class Cat extends Animal {


public static void testClassMethod() {
public static void testClassMethod() {
System.out.println
System.out.println ("The static method in Cat");
("The static method in Animal"); }
} public void testInstanceMethod() {
System.out.println
public void testInstanceMethod() { ("The instance method in Cat");
System.out.println }
("The instance method in Animal");
public static void main(String[] args) {
}
Cat myCat = new Cat();
} Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
OVERRIDE & HIDE METHOD
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
The output from this program is as follows:
Animal.testClassMethod(); The static method in Animal
myAnimal.testInstanceMethod(); The instance method in Cat
Cat.testClassMethod();
myCat.testInstanceMethod(); The static method in Cat
The instance method in Cat
}
}
Latihan 3
public class ClassA {
public void methodOne(int i) { }
public void methodTwo(int i) { }
public static void methodThree(int i) { }
public static void methodFour(int i) { }
}

public class ClassB extends ClassA {


public static void methodOne(int i) { }
public void methodTwo(int i) { }
public void methodThree(int i) { }
public static void methodFour(int i) { }
}

a. Which method overrides a method in the superclass?


b. Which method hides a method in the superclass?
c. What do the other methods do?
Tugas
• Buatlah program yang mengimplementasi inheritance antara 2 objek,
pilihan studi kasus:
1. Circle à Cylinder
2. Circle à Bola
3. Triangle à Pyramida
4. Triangle à Prisma
5. Triangle à Limas Segitiga

Potrebbero piacerti anche