JAVA homework help thank you Question 1: Describing Polymorphism Rules In your o
ID: 3675678 • Letter: J
Question
JAVA homework help thank you
Question 1: Describing Polymorphism Rules
In your own words, describe the following:
1.What is the difference between a declared type and actual type?
2.What are the rules for invoking methods based on the actual and declared type of an object?
Question 3: Trace Polymorphism
Use the classes below to answer the following questions.
1.What is printed by running the code below?
2.What is the actual and declared types of b1, b2, d2, b3, and e3?
3.Which of the following are legal assignments:
1.Book b = new Book();
2.Book b = new Dictionary();
3.Book b = new eBook();
4.eBook b = new Book();
5.eBook b = new Dictionary();
6.eBook b = new eBook();
7.Dictionary b = new Book();
8.Dictionary b = new Dictionary();9.
9.Dictionary b = new eBook();
10.Object b = new Book();
11.Object b = new Dictionary();
12.Object b = new eBook();
13.Book b = new Object();
14.Dictionary b = new Object();
15.eBook b = new Object();
public Book {
public void display() {
System.out.println("Book");
}
}
public class Dictionary extends Book {
public void display() {
System.out.println("Dictionary");
}
}
public class eBook extends Book {
public void display() {
super.display();
System.out.println("eBook");
}
}
in main:
Book b1 = new Book();
b1.display();
Book b2 = new Dictionary();
b2.display();
Dictionary d2 = new Dictionary();
d2.display();
Book b3 = new eBook();
b3.display();
eBook e3 = new eBook();
e3.display();
Explanation / Answer
1. What is the difference between a declared type and actual type?
A. Every variac=ble and expression has two types.They are declared type and actual type.
Declare type follow program syntax and it is static determined by conpiler
Actual type follow creation and dynamic in nature.
Ex: Object bha = new Walmat();
declare type of "bha" is "Object"
actual type is "Walmat"
2. What are the rules for invoking methods based on the actual and declared type of an object?
A. 1.Determine Class or Interface for object.
2.Determine Method to initialize an object with signature.
Question 3: Trace Polymorphism
1.What is printed by running the code below?
Java Code:
public class Book {
public void display() {
System.out.println("Book");
}
}
public class Dictionary extends Book {
public void display() {
System.out.println("Dictionary");
}
}
public class EBook extends Book {
public void display() {
super.display();
System.out.println("eBook");
}
}
public class Test{
public static void main(String []args){
Book b1 = new Book();
b1.display();
Book b2 = new Dictionary();
b2.display();
Dictionary d2 = new Dictionary();
d2.display();
Book b3 = new EBook();
b3.display();
EBook e3 = new EBook();
e3.display();
}
}
output:
Book
Dictionary
Dictionary
Book
EBook
Book
EBook
2.What is the actual and declared types of b1, b2, d2, b3?
A.
3.Which of the following are legal assignments:
A.Book b = new Book(); -legal
Book b = new Dictionary(); -legal
Book b = new EBook(); -legal
EBook b = new Book(); -legal
Book b = new Dictionary(); -legal
EBook b = new EBook(); -legal
Dictionary b = new Book(); -illegal
Dictionary b = new Dictionary(); -legal
Dictionary b = new EBook(); -illegal
Object b = new Book(); -legal
Object b = new Dictionary(); -legal
Object b = new EBook(); -legal
Book b = new Object(); -illegal
Dictionary b = new Object(); -illegal
EBook b = new Object(); -illegal
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.