Tutorial 6: Inheritance and Interface


Overview on this Tutorial:

1. Inheritance (InheritanceTest.java)

2. Interface (InterfaceTest.java)

3. Some special syntaxes (Special.java)


Inheritance (InheritanceTest.java):

1. Inheritance is a technique for deriving a new class from an existing class.

2. The existing class called superclass, while the new class is called subclass.

3. If class B is the subclass of class A, you should add "extends A" when declaring class B:

class B extends A{
    // ...
}

4. In our example, class compStudent is a subclass of Student. So we have:

class CompStudent extends Student
{
    // ...
}

5. "subclass" basically contains all the varibles and functions from the "superclass". This is the main reason for the use of Inheritance.

6. Within the "subclass", you can use the keyword "super" to refer to the "superclass".

7. You can redefine functions in "subclass", such as "average()" in our example.

8. If the function in the "superclass" is "abstract", the function must be defined in the "subclass".

9. You can add new variables and functions in "subclass", such as "getCompMark()" and "compMark" in our example.

10. "CompStudent" is a kind of "Student", so you can write:

CompStudent calvin = new CompStudent("Calvin",71,65,80,90);
Student calvinChu = calvin;

11. But "Student" may not be a "CompStudent", so you have to specify the change of type:

Student calvin = new CompStudent("Calvin",71,65,80,90);
CompStudent calvinChu = (CompStudent)calvin;

12. If the "Student" is actually not a "CompStudent", error will occur:

Student calvin = new Student("Calvin",71,65,80);
CompStudent calvinChu = (CompStudent)calvin;

Question:

1. What is the "superclass" in the class InheritanceTest?

2. Check the class definition of "MIDlet", why we must add the implementations of the functions "pauseApp()" and "destroyApp()"?


Interface (InterfaceTest.java)

1. In Java, a class can extend at most one superclass, so we have to use interface.

2. If a class B "implements" an interface C, you should add "implements C" when declaring class B.

3. You can have a class B as shown:

class B extends A implements C {
    // ...
};

4. In our example, the class "Student" implements the interface "MyComparable".

class Student implements MyComparable {
    // ...
};

5. An interface is very similar to a class, but all the functions in an interface are "abstract" by default and all the variables in an interface are "static" by default.

6. A class can implement multiple interfaces.


Some special syntaxes (Special.java)

1. You can see two special syntaxes in the program.

2. The first one is the use of "switch":

switch(remainder) {
    case 0: System.out.println("The remainder is 0."); break;
    case 1: System.out.println("The remainder is 1."); break;
    case 2: System.out.println("The remainder is 2."); break;
    default: System.out.println("The remainder is 3.");
}

which gives the same result as:

if(remainder==0) {
    System.out.println("The remainder is 0.");
}
else if(remainder==1) {
    System.out.println("The remainder is 1.");
}
else if(remainder==2) {
    System.out.println("The remainder is 2.");
}
else {
    System.out.println("The remainder is 3.");
}

3. The second one is the use of "(<condition>)<v1>:<v2>":

String s=(remainder==0)?"Divisible!":"Not divisible!" ;

which gives the same result as:

String s;
if(remainder==0) {
    s="Divisible!";
}
else {
    s="Not Divisible!";
}