OOP Debug 6
OOP Debug 6
1. Define and distinguish the terms single inheritance and multiple inheritance.
1.Single inheritance is one in which the derived Single inheritance is one in which the derived
class inherits the single base class. class inherits the single base class.
2.In single inheritance, the derived class uses In single inheritance, the derived class uses the
the features of the single base class. features of the single base class.
3.Single inheritance requires small run time as Single inheritance requires small run time as
compared to multiple inheritance due to less compared to multiple inheritance due to less
overhead. overhead.
7.Single inheritance can be implemented in any Single inheritance can be implemented in any
programming language. programming language.
A nice illustration of double inheritance is parents. There are two parents for every child; it's just the
way it is. That you can create classes utilizing multiple inheritance makes sense.
3. A and B are two classes. A inherits properties from B, so A is a ___________ class of B and B is a
__________ class of A.
If A has attributes a1 and a2, methods getA1() and getA2(), and B has attributes b1 and b2, methods
getB1() and getB2(), then by means of inheritance, the actual definition of A and B would be:
6. Given the following class hierarchy diagram, which class’s printLine() method would be used for
each of the messages below (assuming z is an object instantiated from class F):
NOT YET ANSWERED
7.What can you say about the method printLine(String x) of class G in Question 6?
NOT YET ANSWERED
9.Discuss the problem associated with multiple inheritance. How does Java overcome it and what
feature is provided in Java to achieve multiple inheritance? Discuss the limitation of this feature.
Due to two factors, Java does not support multiple inheritance. First, every class in Java is a
descendant of the Object class. A subclass has the potential to inherit from more than one
superclass, giving it the ability to do so. Every class in Java has a constructor, whether it is explicitly
written or not.
10.An abstract class contains one or more abstracted methods. (Fill in the blank.) Distinguish
between an abstract class and an interface. Which is better and why?
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since
abstract methods. Java 8, it can have default and static methods
also.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) An abstract class can extend another Java An interface can extend another Java interface
class and implement multiple Java interfaces. only.
7) An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
9)Example: Example:
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves
fully abstraction (100%).
11.What is the expected output for the code in listing 6-7.
interface I {
void x();
void y(); }
class A implements I {
A() {}
class B extends A {
B() {}
System.out.println("in B.y"); }
void z() {
w();
x(); }
A aa = new A();
B bb = new B();
bb.z();
bb.y(); } }
PROGRAMS
Module 6
Test drive for cylinder class Testcylinder.java
* A test driver for the Cylinder class.
*/
public class TestCylinder {
public static void main(String[] args) {
Cylinder cy1 = new Cylinder();
//Construced a Circle with Circle()
//Constructed a Cylinder with Cylinder()
System.out.println("Radius is " + cy1.getRadius()
+ ", Height is " + cy1.getHeight()
+ ", Color is " + cy1.getColor()
+ ", Base area is " + cy1.getArea()
+ ", Volume is " + cy1.getVolume());
//Radius is 1.0, Height is 1.0, Color is red,
//Base area is 3.141592653589793, Volume is 3.141592653589793
// Constructors
public Circle() {
this.radius = 1.0;
this.color = "red";
System.out.println("Construced a Circle with Circle()"); // for
debugging
}
public Circle(double radius) {
this.radius = radius;
this.color = "red";
System.out.println("Construced a Circle with Circle(radius)"); //
for debugging
}
public Circle(double radius, String color) {
this.radius = radius;
this.color = color;
System.out.println("Construced a Circle with Circle(radius,
color)"); // for debugging
}
// Constructors
public Cylinder() {
super(); // invoke superclass' constructor Circle()
this.height = 1.0;
System.out.println("Constructed a Cylinder with Cylinder()"); //
for debugging
}
public Cylinder(double height) {
super(); // invoke superclass' constructor Circle()
this.height = height;
System.out.println("Constructed a Cylinder with
Cylinder(height)"); // for debugging
}
public Cylinder(double height, double radius) {
super(radius); // invoke superclass' constructor Circle(radius)
this.height = height;
System.out.println("Constructed a Cylinder with Cylinder(height,
radius)"); // for debugging
}
public Cylinder(double height, double radius, String color) {
super(radius, color); // invoke superclass' constructor
Circle(radius, color)
this.height = height;
System.out.println("Constructed a Cylinder with Cylinder(height,
radius, color)"); // for debugging
}
/** Constructs a Student instance with the given name and address */
public Student(String name, String address) {
super(name, address);
numCourses = 0;
courses = new String[MAX_COURSES];
grades = new int[MAX_COURSES];
}
/**
* The Teacher class, subclass of Person.
*/
public class Teacher extends Person {
// private instance variables
private int numCourses; // number of courses taught currently
private String[] courses; // course codes
private static final int MAX_COURSES = 5; // maximum courses
/** Constructs a Teacher instance with the given name and address */
public Teacher(String name, String address) {
super(name, address);
numCourses = 0;
courses = new String[MAX_COURSES];
}
/** Adds a course. Returns false if the course has already existed */
public boolean addCourse(String course) {
// Check if the course already in the course list
for (int i = 0; i < numCourses; i++) {
if (courses[i].equals(course)) return false;
}
courses[numCourses] = course;
numCourses++;
return true;
}
/** Removes a course. Returns false if the course cannot be found in the course list */
public boolean removeCourse(String course) {
boolean found = false;
// Look for the course index
int courseIndex = -1; // need to initialize
for (int i = 0; i < numCourses; i++) {
if (courses[i].equals(course)) {
courseIndex = i;
found = true;
break;
}
}
if (found) {
// Remove the course and re-arrange for courses array
for (int i = courseIndex; i < numCourses-1; i++) {
courses[i] = courses[i+1];
}
numCourses--;
return true;
} else {
return false;
}
}
}
/**
* The superclass Person has name and address.
*/
public class Person {
// private instance variables
private String name, address;
/** Constructs a Person instance with the given name and address */
public Person(String name, String address) {
this.name = name;
this.address = address;
}