SlideShare a Scribd company logo
Chapter 11 Inheritance and Polymorphism
1. (a) The printout is
A’s no-arg constructor is invoked
(b) The default constructor of B attempts to invoke the default of constructor of
A, but class A's default constructor is not defined.
2. All false.
(1) A subclass is an extension of a superclass and normally contains more details
information than its superclass.
(2) If a subclass’s constructor explicitly invoke a superclass’s constructor, the
superclass’s no-arg constructor is not invoked.
(3) You can only override accessible instance methods.
(4) You can only override accessible instance methods.
3. The following lines are erroneous:
{
radius = radius; // Must use this.radius = radius
}
class B extends Circle (missing extends)
{
Circle(radius); // Must use super(radius)
length = length; // Must use this.length = length
}
public double getArea()
{
return getArea()*length; // super.getArea()
}
4. Use super() or super(args). This statement must be the first in the constructor in
the subclass.
5. Use super.method() or super.method(args).
6. Method overloading defines methods of the same name in a class. Method
overriding modifies the methods that are defined in the superclasses.
7. It is method overridden.
8. It will be a syntax error.
9. It is method oveloading.
10. Yes, because these two methods are defined in the Object class; therefore, they are
available to all Java classes. The subclasses usually override these methods to
provide specific information for these methods.
The toString() method returns a string representation of the object; the equals()
method compares the contents of two objects to determine whether they are the
same.
11. B’s constructor is invoked
A’s constructor is invoked
The default constructor of Object is invoked, when new A(3)
is invoked. The Object’s constructor is invoked before any
statements in B’s constructor are executed.
12. (a)
(circle instanceof GeometricObject1) => true
(object1 instanceof GeometricObject1) => true
(circle instanceof Circle1) => true
(object1 instanceof Circle1) => false
(b)
Yes, because you can always cast from subclass to superclass.
(c)
Causing a runtime exception (ClassCastExcpetion)
13.
Is fruit instanceof Fruit true? true
Is fruit instanceof Orange true? false
Is fruit instanceof Apple true? true
Is fruit instanceof GoldDelicious true? true
Is fruit instanceof Macintosh true? false
Is orange instanceof Orange true? true
Is orange instanceof Fruit true? true
Is orange instanceof Apple true? false
Suppose the method makeApple is defined in the Apple
class. Can fruit invoke this method? Yes
Can orange invoke this method? No
Suppose the method makeOrangeJuice is defined in the
Orange class. Can orange invoke this method? Yes.
Can fruit invoke this method? No.
Is the statement Orange p = new Apple() legal? No
Is the statement Macintosh p = new Apple() legal? No
Is the statement Apple p = new Macintosh() legal? Yes
14.
Object apple = (Apple)fruit;
Causes a runtime ClassCastingException.
15. The output is false if the Circle class in (A) is
used. The Circle class has two overloaded methods:
equals(Circle circle) defined in the Circle class and
equals(Object circle) defined in the Object class,
inherited by the Circle class. At compilation time,
circle1.equals(circle2) is matched to equals(Circle
circle), because equals(Circle circle) is more specific
than equals(Object circle).
The output is true if the Circle class in (B) is used. The
Circle class overrides the equals(Object circle) method
defined in the Object class. At compilation time,
circle1.equals(circle2) is matched to equals(Circle circle)
and at runtime, the equals(Object circle) method
implemented in the Circle class is invoked.
16.
How do you create an ArrayList?
Use ArrayList list = new ArrayList();
How do you append an object to a list?
list.add(object);
How do you insert an object at the beginning of a
list?
list.add(0, object);
How do you find out the number of objects in a list?
list.size();
How do you remove a given object from a list?
list.remove(object);
How do you remove the last object from the list?
list.remove(list.size() - 1);
How do you check whether a given object is in a list?
list.contains(object);
How do you retrieve an object at a specified index
from a list?
list.get(index);
17.
Error 1:
String city = list.get(0);
Should be written as
String city = (String)list.get(0);
Error 2:
list.set(3, "Dallas");
is wrong because there is no element at index 3 in the
list.
Error 3:
list.get(3)
is wrong because there is no element at index 3 in the
list.
18. default visibility modifier.
19. protected.
20. If the question marks are replaced by blanks, can
class B be compiled? Yes.
If the question marks are replaced by private, can class B be
compiled? No.
If the question marks are replaced by protected, can class B be
compiled? Yes.
21. If the question marks are replaced by blanks, can
class B be compiled? No.
If the question marks are replaced by private, can class B be
compiled? No.
If the question marks are replaced by protected, can class B be
compiled? Yes.
22. Use the final keyword.
23. Find these terms in this chapter.
24. Indicate true or false for the following statements:
1. True.
2. False. (But yes in a subclass that extends the class where the protected
datum is defined.)
3. True.
4. A final class can have instances.
Answer: True
5. A final class can be extended.
Answer: False
6. A final method can be overridden.
Answer: False
7. You can always successfully cast a subclass to a superclass.
Answer: True
8. You can always successfully cast a superclass to a subclass.
Answer: False
25.
Matching a method signature and binding a method implementation
are two separate issues. The declared type of the reference
variable decides which method to match at compile time. The
compiler finds a matching method according to parameter type,
number of parameters, and order of the parameters at compile
time. A method may be implemented in several subclasses. The JVM
dynamically binds the implementation of the method at runtime,
decided by the actual class of the object referenced by the
variable.
26. See the text.
If the question marks are replaced by protected, can class B be
compiled? Yes.
22. Use the final keyword.
23. Find these terms in this chapter.
24. Indicate true or false for the following statements:
1. True.
2. False. (But yes in a subclass that extends the class where the protected
datum is defined.)
3. True.
4. A final class can have instances.
Answer: True
5. A final class can be extended.
Answer: False
6. A final method can be overridden.
Answer: False
7. You can always successfully cast a subclass to a superclass.
Answer: True
8. You can always successfully cast a superclass to a subclass.
Answer: False
25.
Matching a method signature and binding a method implementation
are two separate issues. The declared type of the reference
variable decides which method to match at compile time. The
compiler finds a matching method according to parameter type,
number of parameters, and order of the parameters at compile
time. A method may be implemented in several subclasses. The JVM
dynamically binds the implementation of the method at runtime,
decided by the actual class of the object referenced by the
variable.
26. See the text.

More Related Content

DOC
08review (1)
DOC
14review(abstract classandinterfaces)
PDF
The Expression Problem - Part 2
PDF
The Expression Problem - Part 1
PDF
Utility Classes
PDF
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
PDF
Applicative Functor
PDF
Functional Effects - Part 2
08review (1)
14review(abstract classandinterfaces)
The Expression Problem - Part 2
The Expression Problem - Part 1
Utility Classes
Linked Lists: The Role of Locking : The Art of Multiprocessor Programming : N...
Applicative Functor
Functional Effects - Part 2

What's hot (20)

PDF
Definitions of Functional Programming
PPTX
Methods common to all objects
PPTX
7 algorithm
PDF
Preparation Data Structures 05 chain linear_list
PDF
non-strict functions, bottom and scala by-name parameters
PPT
Ap Power Point Chpt8
PDF
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 2 - Profe...
PDF
Monad as functor with pair of natural transformations
PPTX
Writer Monad for logging execution of functions
PDF
Monad Laws Must be Checked
PDF
04 a ch03_programacion
PDF
Functional Effects - Part 1
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
PPT
Applying Generics
PDF
Scala 3 enum for a terser Option Monad Algebraic Data Type
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
PDF
‘go-to’ general-purpose sequential collections - from Java To Scala
PDF
Monad Fact #4
DOCX
Java execise
PDF
ES6 Template Literal & Tag Function
Definitions of Functional Programming
Methods common to all objects
7 algorithm
Preparation Data Structures 05 chain linear_list
non-strict functions, bottom and scala by-name parameters
Ap Power Point Chpt8
ICPSR - Complex Systems Models in the Social Sciences - Lab Session 2 - Profe...
Monad as functor with pair of natural transformations
Writer Monad for logging execution of functions
Monad Laws Must be Checked
04 a ch03_programacion
Functional Effects - Part 1
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Applying Generics
Scala 3 enum for a terser Option Monad Algebraic Data Type
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 4
‘go-to’ general-purpose sequential collections - from Java To Scala
Monad Fact #4
Java execise
ES6 Template Literal & Tag Function
Ad

Viewers also liked (13)

PDF
Daniel Witherite UT-UXO Certificate
PDF
Casa design 2014
PPTX
Sistemas de Información en la Empresa
PPTX
Growth Hacking with Kentico
PPT
Csc1100 lecture09 ch07_pt2
PPTX
El informe de investigación
PDF
Mediterranean – Adriatic Underwater Cultural Heritage links
PDF
YALI_Certificate (2)
PDF
Apostila de empilhadeira rv1
PPTX
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
PPT
Oportunidad Negocio (2 )
PDF
Startup Braga - value proposition workshop
PDF
Brochure Seminario 23 de mayo
Daniel Witherite UT-UXO Certificate
Casa design 2014
Sistemas de Información en la Empresa
Growth Hacking with Kentico
Csc1100 lecture09 ch07_pt2
El informe de investigación
Mediterranean – Adriatic Underwater Cultural Heritage links
YALI_Certificate (2)
Apostila de empilhadeira rv1
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
Oportunidad Negocio (2 )
Startup Braga - value proposition workshop
Brochure Seminario 23 de mayo
Ad

Similar to 11review(inheritance andpolymorphism) (20)

DOCX
Corejavainterviewquestions.doc
PPT
Chapter 8 Inheritance
PPTX
PPT Lecture-1.4.pptx
PDF
PPT
Chap11
PPT
Conceitos Fundamentais de Orientação a Objetos
PPT
Questões de Certificação SCJP
DOC
Core java interview questions
DOCX
Java Core Parctical
PDF
Java OOP Programming language (Part 5) - Inheritance
PPT
Java Programming - Inheritance
PPT
9781439035665 ppt ch10
PDF
Object oriented programming java inheritance
PPT
InheritanceAndPolymorphismprein Java.ppt
PPT
06 InheritanceAndPolymorphism.ppt
DOCX
Java interview questions
PPTX
Java session2
PPT
Object and class
PPTX
Advanced Java - Lec #5 - Inheritance and Polymorphism.pptx
DOCX
Core java questions
Corejavainterviewquestions.doc
Chapter 8 Inheritance
PPT Lecture-1.4.pptx
Chap11
Conceitos Fundamentais de Orientação a Objetos
Questões de Certificação SCJP
Core java interview questions
Java Core Parctical
Java OOP Programming language (Part 5) - Inheritance
Java Programming - Inheritance
9781439035665 ppt ch10
Object oriented programming java inheritance
InheritanceAndPolymorphismprein Java.ppt
06 InheritanceAndPolymorphism.ppt
Java interview questions
Java session2
Object and class
Advanced Java - Lec #5 - Inheritance and Polymorphism.pptx
Core java questions

More from IIUM (20)

PDF
How to use_000webhost
PDF
Chapter 2
PDF
Chapter 1
PDF
Kreydle internship-multimedia
PDF
03phpbldgblock
PDF
Chap2 practice key
PDF
Group p1
PDF
Tutorial import n auto pilot blogspot friendly seo
PDF
Visual sceneperception encycloperception-sage-oliva2009
PDF
03 the htm_lforms
PDF
Exercise on algo analysis answer
PDF
Redo midterm
PDF
Heaps
PDF
Report format
PDF
Edpuzzle guidelines
PDF
Final Exam Paper
PDF
Final Exam Paper
PDF
Group assignment 1 s21516
PDF
Avl tree-rotations
PDF
Week12 graph
How to use_000webhost
Chapter 2
Chapter 1
Kreydle internship-multimedia
03phpbldgblock
Chap2 practice key
Group p1
Tutorial import n auto pilot blogspot friendly seo
Visual sceneperception encycloperception-sage-oliva2009
03 the htm_lforms
Exercise on algo analysis answer
Redo midterm
Heaps
Report format
Edpuzzle guidelines
Final Exam Paper
Final Exam Paper
Group assignment 1 s21516
Avl tree-rotations
Week12 graph

Recently uploaded (20)

PDF
TISSUE LECTURE (anatomy and physiology )
PPTX
Introduction to Medical Microbiology for 400L Medical Students
PPTX
Cardiovascular - antihypertensive medical backgrounds
PPTX
Epidemiology of diptheria, pertusis and tetanus with their prevention
PDF
Copy of OB - Exam #2 Study Guide. pdf
PPTX
Effects of lipid metabolism 22 asfelagi.pptx
PPT
Dermatology for member of royalcollege.ppt
PPTX
NRP and care of Newborn.pptx- APPT presentation about neonatal resuscitation ...
PDF
OSCE SERIES - Set 7 ( Questions & Answers ).pdf
PPTX
Human Reproduction: Anatomy, Physiology & Clinical Insights.pptx
PDF
The_EHRA_Book_of_Interventional Electrophysiology.pdf
PDF
Lecture 8- Cornea and Sclera .pdf 5tg year
PPT
Infections Member of Royal College of Physicians.ppt
PPTX
Medical Law and Ethics powerpoint presen
PDF
OSCE SERIES ( Questions & Answers ) - Set 5.pdf
PPTX
Radiation Dose Management for Patients in Medical Imaging- Avinesh Shrestha
PPTX
IMAGING EQUIPMENiiiiìiiiiiTpptxeiuueueur
PDF
Calcified coronary lesions management tips and tricks
PDF
SEMEN PREPARATION TECHNIGUES FOR INTRAUTERINE INSEMINATION.pdf
PPTX
Neonate anatomy and physiology presentation
TISSUE LECTURE (anatomy and physiology )
Introduction to Medical Microbiology for 400L Medical Students
Cardiovascular - antihypertensive medical backgrounds
Epidemiology of diptheria, pertusis and tetanus with their prevention
Copy of OB - Exam #2 Study Guide. pdf
Effects of lipid metabolism 22 asfelagi.pptx
Dermatology for member of royalcollege.ppt
NRP and care of Newborn.pptx- APPT presentation about neonatal resuscitation ...
OSCE SERIES - Set 7 ( Questions & Answers ).pdf
Human Reproduction: Anatomy, Physiology & Clinical Insights.pptx
The_EHRA_Book_of_Interventional Electrophysiology.pdf
Lecture 8- Cornea and Sclera .pdf 5tg year
Infections Member of Royal College of Physicians.ppt
Medical Law and Ethics powerpoint presen
OSCE SERIES ( Questions & Answers ) - Set 5.pdf
Radiation Dose Management for Patients in Medical Imaging- Avinesh Shrestha
IMAGING EQUIPMENiiiiìiiiiiTpptxeiuueueur
Calcified coronary lesions management tips and tricks
SEMEN PREPARATION TECHNIGUES FOR INTRAUTERINE INSEMINATION.pdf
Neonate anatomy and physiology presentation

11review(inheritance andpolymorphism)

  • 1. Chapter 11 Inheritance and Polymorphism 1. (a) The printout is A’s no-arg constructor is invoked (b) The default constructor of B attempts to invoke the default of constructor of A, but class A's default constructor is not defined. 2. All false. (1) A subclass is an extension of a superclass and normally contains more details information than its superclass. (2) If a subclass’s constructor explicitly invoke a superclass’s constructor, the superclass’s no-arg constructor is not invoked. (3) You can only override accessible instance methods. (4) You can only override accessible instance methods. 3. The following lines are erroneous: { radius = radius; // Must use this.radius = radius } class B extends Circle (missing extends) { Circle(radius); // Must use super(radius) length = length; // Must use this.length = length } public double getArea() { return getArea()*length; // super.getArea() } 4. Use super() or super(args). This statement must be the first in the constructor in the subclass. 5. Use super.method() or super.method(args). 6. Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses. 7. It is method overridden. 8. It will be a syntax error.
  • 2. 9. It is method oveloading. 10. Yes, because these two methods are defined in the Object class; therefore, they are available to all Java classes. The subclasses usually override these methods to provide specific information for these methods. The toString() method returns a string representation of the object; the equals() method compares the contents of two objects to determine whether they are the same. 11. B’s constructor is invoked A’s constructor is invoked The default constructor of Object is invoked, when new A(3) is invoked. The Object’s constructor is invoked before any statements in B’s constructor are executed. 12. (a) (circle instanceof GeometricObject1) => true (object1 instanceof GeometricObject1) => true (circle instanceof Circle1) => true (object1 instanceof Circle1) => false (b) Yes, because you can always cast from subclass to superclass. (c) Causing a runtime exception (ClassCastExcpetion) 13. Is fruit instanceof Fruit true? true Is fruit instanceof Orange true? false Is fruit instanceof Apple true? true Is fruit instanceof GoldDelicious true? true Is fruit instanceof Macintosh true? false Is orange instanceof Orange true? true Is orange instanceof Fruit true? true Is orange instanceof Apple true? false Suppose the method makeApple is defined in the Apple class. Can fruit invoke this method? Yes
  • 3. Can orange invoke this method? No Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? Yes. Can fruit invoke this method? No. Is the statement Orange p = new Apple() legal? No Is the statement Macintosh p = new Apple() legal? No Is the statement Apple p = new Macintosh() legal? Yes 14. Object apple = (Apple)fruit; Causes a runtime ClassCastingException. 15. The output is false if the Circle class in (A) is used. The Circle class has two overloaded methods: equals(Circle circle) defined in the Circle class and equals(Object circle) defined in the Object class, inherited by the Circle class. At compilation time, circle1.equals(circle2) is matched to equals(Circle circle), because equals(Circle circle) is more specific than equals(Object circle). The output is true if the Circle class in (B) is used. The Circle class overrides the equals(Object circle) method defined in the Object class. At compilation time, circle1.equals(circle2) is matched to equals(Circle circle) and at runtime, the equals(Object circle) method implemented in the Circle class is invoked. 16. How do you create an ArrayList? Use ArrayList list = new ArrayList(); How do you append an object to a list? list.add(object); How do you insert an object at the beginning of a list? list.add(0, object); How do you find out the number of objects in a list? list.size();
  • 4. How do you remove a given object from a list? list.remove(object); How do you remove the last object from the list? list.remove(list.size() - 1); How do you check whether a given object is in a list? list.contains(object); How do you retrieve an object at a specified index from a list? list.get(index); 17. Error 1: String city = list.get(0); Should be written as String city = (String)list.get(0); Error 2: list.set(3, "Dallas"); is wrong because there is no element at index 3 in the list. Error 3: list.get(3) is wrong because there is no element at index 3 in the list. 18. default visibility modifier. 19. protected. 20. If the question marks are replaced by blanks, can class B be compiled? Yes. If the question marks are replaced by private, can class B be compiled? No. If the question marks are replaced by protected, can class B be compiled? Yes. 21. If the question marks are replaced by blanks, can class B be compiled? No. If the question marks are replaced by private, can class B be compiled? No.
  • 5. If the question marks are replaced by protected, can class B be compiled? Yes. 22. Use the final keyword. 23. Find these terms in this chapter. 24. Indicate true or false for the following statements: 1. True. 2. False. (But yes in a subclass that extends the class where the protected datum is defined.) 3. True. 4. A final class can have instances. Answer: True 5. A final class can be extended. Answer: False 6. A final method can be overridden. Answer: False 7. You can always successfully cast a subclass to a superclass. Answer: True 8. You can always successfully cast a superclass to a subclass. Answer: False 25. Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable. 26. See the text.
  • 6. If the question marks are replaced by protected, can class B be compiled? Yes. 22. Use the final keyword. 23. Find these terms in this chapter. 24. Indicate true or false for the following statements: 1. True. 2. False. (But yes in a subclass that extends the class where the protected datum is defined.) 3. True. 4. A final class can have instances. Answer: True 5. A final class can be extended. Answer: False 6. A final method can be overridden. Answer: False 7. You can always successfully cast a subclass to a superclass. Answer: True 8. You can always successfully cast a superclass to a subclass. Answer: False 25. Matching a method signature and binding a method implementation are two separate issues. The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable. 26. See the text.