Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Inheritance Polymorphism Type compatibility Homework.

Similar presentations


Presentation on theme: "Agenda Inheritance Polymorphism Type compatibility Homework."— Presentation transcript:

1 Agenda Inheritance Polymorphism Type compatibility Homework

2 Inheritance What is inheritance?
What is the relationship between superclass and subclass? Which one is begger?(contains more data and more methods) superclass or subclass? Why do we want inheritance?

3 Inheritance hierarchy
Can a subclass be a superclass for another subclass? How do you define a “is-a” relationship if there is a superclass Person and a subclass Male? The is-a relationship is transitive. Name an example. What is method overriding?

4 Implementing subclasses
Which keyword is used for the inheritance relationship? Person Student Employee GradStudent UnderGrad

5 Inheriting instance mthods and variables
Can a subclass “UnderGrad” access the superclass “Student” state variable myName directly?  Can a subclass invoke the public accessor and mutator methods of the superclass? What if the state variable myName is public can a subclass access it?  Do the classes on the same level, in this case, GradStudent and UnderGrad, inherit anything from each other?

6 Method overriding and super keyword
What is method overriding? What is partial overriding? Why do we do partial method overriding?  Look at page 126 with the following codes public void computeGrade() { super.computeGrade(); if(getTestAverage() >= 90) setGrade(“Pass with distinction”); } where computerGrade(), getTestAverage() and setGrade() are all methods in the superclass. Can keyword “super” be eliminated from the statement super.computeGrade() like setGrade()?

7 Constructors and super
Can constructors be inherited? If a subclass has no constructor, what happens? If a subclass has no constructor and the superlass does not have a default constructor, what happens? How to invoke the superclass constructor in subclass? If the subclass like GradStudent has all the state variables as the superclass and plus its own state variable gradeID, how to write the default constructor?

8 What is wrong with the following codes and why?
public GradeStudent() { gradeID = 0; super(); } Can a subclass override static methods of the superclass? Can a subclass directly access the private members of its superclass?

9 Declaring subclass objects
Is the following legal? Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad(); GradStudent g = new Student(); UnderGrad u = new Student(); GradStudent g = new UnderGrad();

10 what is polymorphism? what would be a good example in last slide?
What is dynamic binding(late binding)? What is static binding(early binding)? In polymorphism, which binding is used at run time? With overloaded methods, which binding is used at compile time? On page 131, what is the box on the right saying about polymorphism?

11 Type compatibility Student s = new GradStudent();
Look at the statements and answer the questions Student s = new GradStudent(); GradStudent g = new GradStudent(); int x = s.getID(); int y = g.getID(); which statement will cause a compile time error and why? What can be done to fix the error? What is a downcast? What is ClassCastException?

12 What is wrong with the following codes?
Student u = new UnderGrad(); System.out.println((String) u); int x = ((GradStudent) u).getID();

13 Abstract classes Can an abstract class be instantiated?
Which keyword is used to declare an abstract class? Suppose we have an abstract class Vehicle and Car, Motorcyle and Truck are its subclasses. Which of the following is wrong? Why? a) Car aCar = new Car(); b) Truck aCar = new Truck(); c) Vehicle aVehicle = new Vehicle();

14 Look at the question 9 diagram
Look at the question 9 diagram. If Person is an abstract class, any of its abstracted methods must be implemented in any subclasses Student or Employee.. If Student fails to implement all abstracted methods, what needs to be done? Is it possible for an abstract class to have NO abstract methods? An abstract class may not have constructors. Correct?

15 Interfaces What is an Interface?
All the methods in an Interface are all public and abstract by default, correct? Assume Person is an Interface. Is it required the Student has to implement all the methods in Person, if Student is a nonabstract class? If Student fails to implement any of the methods in Person, what needs to be done to avoid compile-error?

16 How do you declare an interface?
Which keyword do you use to implement the interface? Write the declaration of Employee which inherits class Person and implements the interface Comparable Can a class have more than on superclass? Can a class implement more than one interface? How do you declare if a class Bird is a subclass of FlyingAnimal and implements interface of Flying and Walking.

17 Comparable interface What data type needs to implement Comparable to do the comparison? What is the only method in the class Comparable? If two objects being compared are not type compatible, what happens?

18 What is the signature of a abstract method? Give an example.
If any class contains any abstract method, does this class have to be declared an abstract class?

19 Homework Study the Barron's chapter 2 multiple-choice questions on class and objects(p ). There is a quiz III next Monday. Be sure to fully understand the materials. Look through the "Answers Explained" section for anything you don't understand. Turn in any homework you failed to submit.


Download ppt "Agenda Inheritance Polymorphism Type compatibility Homework."

Similar presentations


Ads by Google