AP Computer Science A : Class Inheritance

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

Example Question #12 : Class Design

For this question, consider the following code:

private static class Philosopher {

        private String name;

        private String favoriteSubject;

        public Philosopher(String n, String f) {

                name = n;

                favoriteSubject = f;

        }       

        public String getName() {

                return name;

        }       

        public String getFavoriteSubject() {

                return favoriteSubject;

        }       

        public void speak() {

                System.out.println("Hello, World!  My name is "+name + ".  My favorite subject is "+favoriteSubject);

        }

}

private static class Nominalist extends Philosopher {

        boolean franciscan;   

        public Nominalist(String n,boolean frank) {

                super(n,"logic");

                franciscan = frank;

        }       

        public void speak() {

                super.speak();

                if(franciscan) {

                        System.out.println("I am a Franciscan");

                } else {

                        System.out.println("I am not a Franciscan");

                }

        }       

        public String whoMightHaveTaughtMe() {

                if(franciscan) {

                        return "Perhaps William of Ockham?....";

                } else {

                        return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";

                }

        }

public static void main(String[] args) {

        Philosopher p = new Nominalist("Nicanor",false);

        System.out.println(p.whoMightHaveTaughtMe());

}

What is the output for this main method?

Possible Answers:

Perhaps William of Ockham?....

Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!

There is no output. 

This will not compile.  That method is defined on the subclass, but the variable is defined as the superclass Philosopher, meaning that the method is not visible.

This will not compile.  You cannot call a subclass function within the scope of the main method.

Correct answer:

This will not compile.  That method is defined on the subclass, but the variable is defined as the superclass Philosopher, meaning that the method is not visible.

Explanation:

When you assign a subclass (like Nominalist) to a super class (like Philosopher) you will not be able to call any of the subclass-specific methods on the variable in question.  This code does just that, which will prevent the code from compiling.

Example Question #1 : Class Inheritance

Consider the following code:

private static class Philosopher {

        private String name;

        private String favoriteSubject;     

        public Philosopher(String n, String f) {

                name = n;

                favoriteSubject = f;

        }       

        public String getName() {

                return name;

        }       

        public String getFavoriteSubject() {

                return favoriteSubject;

        }      

        public void speak() {

                System.out.println("Hello, World!  My name is "+name + ".  My favorite subject is "+favoriteSubject);

        }

}

private static class Nominalist extends Philosopher {

        boolean franciscan;     

        public Nominalist(String n,boolean frank) {

                super(n,"logic");

                franciscan = frank;

        }      

        public void speak() {

                super.speak();

                if(franciscan) {

                        System.out.println("I am a Franciscan");

                } else {

                        System.out.println("I am not a Franciscan");

                }

        }   

        public String whoMightHaveTaughtMe() {

                if(franciscan) {

                        return "Perhaps William of Ockham?....";

                } else {

                        return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";

                }

        }

public static void main(String[] args) {

     Philosopher[] phils = {

               new Philosopher("Petrus","Ethics"),

               new Nominalist("Minimus Maximus",false),

               new Nominalist("Theodoric",true)};

     for(int i = 0; i < phils.length; i++) {

          phils[i].speak();

     }

}

What is the output for the code above?

Possible Answers:

Hello, World!  My name is Petrus.  My favorite subject is Ethics

Hello, World!  My name is Minimus Maximus.  My favorite subject is logic

Hello, World!  My name is Theodoric.  My favorite subject is logic

Hello, World!  My name is Petrus. My favorite subject is Ethics

Hello, World!  My name is Minimus Maximus. My favorite subject is logic

I am not a Franciscan

Hello, World!  My name is Theodoric.  My favorite subject is logic

I am a Franciscan

Hello, World!  My name is Petrus.  My favorite subject is Ethics

Hello, World!  My name is Minimus Maximus.  My favorite subject is logic

Hello, World!  My name is Theodoric.  My favorite subject is logic

Hello, World!  My name is Minimus Maximus.  My favorite subject is logic. I am not a Franciscan

Hello, World!  My name is Petrus.  My favorite subject is Ethics

Hello, World!  My name is Theodoric.  My favorite subject is logic. I am a Franciscan

Hello, World!  My name is Petrus.  My favorite subject is Ethics

Hello, World!  My name is Minimus Maximus.  My favorite subject is logic I am not a Franciscan

Hello, World!  My name is Theodoric.  My favorite subject is logic I am a Franciscan

Correct answer:

Hello, World!  My name is Petrus. My favorite subject is Ethics

Hello, World!  My name is Minimus Maximus. My favorite subject is logic

I am not a Franciscan

Hello, World!  My name is Theodoric.  My favorite subject is logic

I am a Franciscan

Explanation:

Three things must be kept in mind for this question.  First (and foremost), given the way that inheritance works, you know that for each object, you will get an output that matches the given object's particular type—even though the array is an array of the superclass type (namely Philosopher).  Second, you must notice that the lines "I am a Franciscan" and "I am not a Franciscan" come a line after the data before it (which is printed by using the super call). Finally, be careful about punctuation! There are periods missing in some of the output lines.

Example Question #3 : Class Inheritance

Consider the following code:

public class Rectangle {

     private double width, height;

     public Rectangle(double w,double h) {

          width = w;

          height = h;

     }

    

     public double getArea() {

          return width * height;

     }

    

     public double getPerimeter() {

          return 2 * width + 2 * height;

     }

}

 

public class Square {

     private double side;

     public Square(double s) {

          side = s;

     }

     public double getArea() {

          return side * side;

     }

     public double getPerimeter() {

          return 4 * side;

     }

}

Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?

Possible Answers:

public class Square {

     private Rectangle r;

     public Square(double s) {

          r = new Rectangle(s,s);

     }

     public double getArea() {

          return r.getArea();

     }

     public double getPerimeter() {

          return r.getPerimeter();

     }

}

public class Square implements Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

         new Rectangle(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     private double side;

     public Square(double s) {

          super(s,s);

     }

}

Correct answer:

public class Square extends Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

Explanation:

We know that a square really is just a subclass of a rectangle, for it is merely a rectangle having four sides that are all equal. Using inheritance, you can very easily reuse much of your Rectangle code. First, you need to extend the Rectangle class:

public class Square extends Rectangle { . . .

Next, you can be rid of the field side. This allows you to alter the constructor for Square to call the Rectangle constructor. You do this using super (because Rectangle is the superclass).

After this, you can delete the getArea and getPerimeter methods, for they will be handled by the superclass. This gives you a very simple bit of code!

Example Question #11 : Class Design

Which of the following is TRUE about the Object class?

Possible Answers:

Object is the only class that never has any descendants.

None of these answers are true.

Object inherits a minimum of four classes.

Object has one superclass, Entity.

Every class has all of Object's methods.

Correct answer:

Every class has all of Object's methods.

Explanation:

Object is the most basic class which all others, even user made ones, inherit. Therefore, all classes have Object's methods. A way to think of classes is to think of a tree: the Object class is the lowest node on the tree, where all other nodes can connect back to.

Example Question #2 : Class Inheritance

class Z

{

public:

void Func4();

};

class Y : public Z

{

public:

       virtual void Func3();

};

class B : public Y

public:

       virtual void Func1();

       void Func2();

};

class C : public B

public:

       virtual void Func1();

};

 

What is the base class in the above code.

Possible Answers:

Class B

Class C

None of them

Class Z

Class Y

Correct answer:

Class Z

Explanation:

The base class is a class that doesn't derivate from any other class. Starting from the bottom of the code, we see that class C is inherited from class B, which is inherited from class Y, which is finally inherited by class Z. Class Z is the base class.

Example Question #4 : Class Inheritance

True or False.

The class BetterMan inherits from the class Man.

 

public class BetterMan extends Man {

 

}

Possible Answers:

False

True

Correct answer:

True

Explanation:

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

Learning Tools by Varsity Tutors