Unformatted text preview:

1 What is early static binding What is an advantage Early or static binding is a process in which the method binding between a method call and the method definition occurs at compile time It is also called compile time binding or static dispatch The advantage of early binding is that it leads to faster code execution since the binding has already been established during compilation 2 What is late dynamic binding What is an advantage Late or dynamic binding is a process in which the method binding between a method call and the method definition occurs at runtime It is also called runtime binding or dynamic dispatch The advantage of late binding is that it allows for more flexibility and extensibility in the code since new methods can be added or existing methods can be overridden without having to change the existing code 3 What kind of binding does Java use Java uses late dynamic binding This allows decisions to be made at runtime rather than at compile time This also allows for dynamic dispatch to run smoothly 4 What is dynamic dispatch Come up with a code fragment that demonstrates dynamic dispatch Dynamic dispatch is the process of selecting which implementation of a method to call at runtime based on the actual type of the object that the method is called on Here is an example of dynamic dispatch in Java public class Animal public void makeSound System out println The animal makes a sound public class Dog extends Animal public void makeSound System out println The dog barks public class Main public static void main String args Animal animal1 new Animal Animal animal2 new Dog animal1 makeSound prints The animal makes a sound animal2 makeSound prints The dog barks 5 Consider the following public class Plain public void perform String s public class Cool extends Plain public void perform String s public void perform String a String b Plain x new Plain Plain y new Cool Cool z new Cool if For each of the following decide whether or not it will compile and it does compile decide which of the three versions of perform will run x perform Wow perform in Plain class z perform Great Job perform in the Cool class 2nd method y perform Hey perform in the Cool class 1st method y perform What s Up compiles but will throw compilation error because a perform method with 2 arguments as String is not defined in the Plain class and y is an instance of the Cool class 6 What class is always at the top of every inheritance diagram What are some of the methods in this class that are commonly overridden The Object class is always at the top of every inheritance diagram in Java Some of the methods in this class that are commonly overridden include the toString method and the equals method 7 What is an upcast Will Java implicitly do an upcast sometimes without you having to use a casting operator An upcast is when you cast an object to one of its superclasses Java will implicitly do an upcast sometimes without you having to use a casting operator such as when assigning an object of a subclass to a variable of its superclass type 8 What is a downcast Will Java implicitly do a downcast sometimes without you having to use a casting operator A downcast is when you cast an object to one of its subclasses Java will not implicitly do a downcast and you need to use a casting operator for it It can cause a ClassCastException at runtime if the object being cast is not an instance of the subclass 9 Write a class called Book with the specifications below public class Book implements Comparable Book private String title private String authorFirstName private String authorLastName private int year public enum DefaultValues NO TITLE No Title NO FIRST NAME No First Name NO LAST NAME No Last Name public Book String title String authorFirstName String authorLastName private final String value DefaultValues String value this value value public String getValue return value int year this title title this authorFirstName authorFirstName this authorLastName authorLastName this year year public Book this DefaultValues NO TITLE getValue DefaultValues NO FIRST NAME getValue DefaultValues NO LAST NAME getValue 0 public Book Book other this title other title this authorFirstName other authorFirstName this authorLastName other authorLastName this year other year public String getTitle return title public String getAuthorFirstName return authorFirstName public String getAuthorLastName return authorLastName public int getYear return year Override public boolean equals Object other if other null getClass other getClass return false Book otherBook Book other return title equals otherBook title authorFirstName equals otherBook authorFirstName authorLastName equals otherBook authorLastName Override public int compareTo Book other int lastNameComparison authorLastName compareTo other authorLastName if lastNameComparison 0 return lastNameComparison return authorFirstName compareTo other authorFirstName Override public String toString return title by authorFirstName authorLastName year above 10 Define a class called ElectronicBook that extends the Book class The class has the following specifications a Instance variables all private webAddress string sizeMB int b Constructors and instance methods Constructor with parameters title author s firstname author s lastname year web address and sizeMB Default constructor Initializes the object with the values NO WEB ADDRESS and 0 for sizeMB The values associated with the Book default constructor will be used Copy constructor equals method toString calls the super class toString method and concatenates to it the values for webAddress and sizeMB getWebAddress returns the web address compareTo method we should be able to sort electronic books based on sizeMB Feel free to add any other methods you think might be useful public class ElectronicBook extends Book private String webAddress private int sizeMB public ElectronicBook String title String authorFirstName String authorLastName int year String webAddress int sizeMB super title authorFirstName authorLastName year this webAddress webAddress this sizeMB sizeMB public ElectronicBook super note the use of super to call the empty constructor this webAddress NO WEB ADDRESS this sizeMB 0 public ElectronicBook ElectronicBook other super other refers to the copy constructor in the Book class this webAddress other webAddress this sizeMB other sizeMB public boolean equals Object obj if obj instanceof ElectronicBook return false ElectronicBook other


View Full Document

UMD CMSC 131 - Set #3

Documents in this Course
Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Set #3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Set #3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Set #3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?