Tutorial 3: Inheritance Part A Topic: Inheritance 1. Consider the following class definition. class Student { private String name; private String matric_no; a. Write the definition of an empty class named UndergradStudent which is a subclass of Student class. b. State the reason why UndergradStudent class cannot make direct access to the attributes name and matric_no in Student class, although the attributes are inherited by the UndergradStudent class. c. Make the necessary changes to the two attributes so that they can be accessed by the UndergradStudent class. Topic: Constructor and super() 2. Every constructor defined in a subclass must call one of the constructors of its superclass (True/False). 3. Consider the following class definition. class Student { private String name; private String matric_no; public Student(String n, String m) { name = n; matric_no = m; class UndergradStudent extends Student { private String programme; public UndergradStudent(String n, String m, String p) { programme = p; super(n, m); 1
class PostgradStudent extends Student { private String supervisor; public PostgradStudent(String s) { supervisor = s; a. State the reason why the code inside the constructor of UndergradStudent class will cause a compilation error. b. Make necessary changes to the constructor of UndergradStudent class to solve the compilation error. c. State the reason why the code inside the constructor of PostgradStudent class will cause a compilation error. d. Make necessary changes to the constructor of PostgradStudent class to solve the compilation error. Topic: Method Overriding and super Keyword 4. What does method overriding mean? 5. Write the output of the following Java program. public class ClassA { public void dothis() { System.out.println("doThis in ClassA"); public class ClassB extends ClassA { public void dothis() { System.out.println("doThis in ClassB"); public class ClassC extends ClassA { public class ClassD extends ClassB { public void dothis() { System.out.println("doThis in ClassD"); public class ClassE extends ClassB { public class ClassF extends ClassE { 2
public class Test { ClassC c = new ClassC(); ClassD d = new ClassD(); ClassF f = new ClassF(); c.dothis(); d.dothis(); f.dothis(); 6. Write the output of the execution of the following classes. public class A { public void m1() { System.out.println("m1 in A"); public class B extends A { public void m1() { System.out.println("m1 in B"); super.m1(); public class C extends A { public class Test { B b = new B(); C c = new C(); b.m1(); c.m1(); 3
Topic: Method Overloading vs. Method Overriding 7. Consider the following classes and describe the relationship between the following methods: public class A { protected void m1() { System.out.println("m1 in A"); private void m2() { System.out.println("m2 in A"); public class B extends A { public void m1() { System.out.println("m1 in B"); public void m1(string x) { System.out.println("m1(String) in B"); public void m2() { System.out.println("m2 in B"); a. m1 in B and m1 in A b. m1(string) in B and m1 in A c. m2 in B and m2 in A d. Write the output of the following: public class TestAB { A a1 = new A(); A a2 = new B(); B b = new B(); a1.m1(); a2.m1(); b.m1(); b.m1("astring"); b.m2(); 4
Topic: Polymorphism 8. Pak Ahmad has a small farm, and the following Figure 1 represents the animals (a dog, a chicken and a duck) that he has at his farm. Figure 1 All animals at Pak Ahmad s farm can walk and run, and although the dog also can jump, the chicken and the duck have the advantage because as birds, they also can fly. However, the duck has the advantage over the chicken because the duck also can swim. Consider the following class definitions, and answer the following questions. class Animal { protected String animal_type; public Animal(String t) { animal_type = t; System.out.println(animal_type + " can walk and run"); class Bird extends Animal { public Bird(String t) { super(t); super.move(); System.out.println(animal_type + " also can fly"); a. Write the definition for the Chicken class. 5
b. Name the feature in OOP that allows a subclass to replace the implementation of a method in the super class with different implementation. c. Consider the following definitions for move method in the Dog class and Duck class. Dog class: super.move(); System.out.println(animal_type + " also can jump"); Duck class: super.move(); System.out.println(animal_type + " also can swim"); Write the output of the following code. class Test { Animal[] animals = {new Dog(), new Duck(), new Chicken(); for (int i=0; i<animals.length; i++) animals[i].move(); // Line 1 i. Output at Line 1 when i is 0: ii. Output at Line 1 when i is 1: iii. Output at Line 1 when i is 2: 6
Part B Problem Description We are developing a Course Registration System for a school, and to do this we can define a superclass called Person to store common properties for person such as name and address, and subclasses Student and Teacher for their specific properties. For students, we need to maintain the courses taken and their respective marks; add a course with mark, print all courses taken by students and their average mark. A student takes no more than 5 courses for the entire program. For teachers, we need to maintain the courses taught currently, and able to add or remove a course taught. A teacher teaches not more than 5 courses concurrently. Tasks 1. Draw a UML class diagram to model the classes in the system. 2. Define a suitable class with methods to implement the functionalities for each class. 7