















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















What do you find in a java interface? - Correct Answer-method prototypes, final static variables, static methods (note: instance methods are ok if you use the term 'default'). The classes that implement an interface must implement ___________ - Correct Answer-Instance methods from interface (note: default instance method does not need to be implemented, it has already been inherited). When should you use the key word 'Override'? - Correct Answer-When you are implementing a method from an interface. If there is default implementation of a method you can use override to implement it differently. (reminder: if it is already implemented in the interface, you do not need to implement it in a different class because it has already inherited it). (T/F) - You can add other code to the classes that inherit the superclass features aside from the interface method implementations. - Correct Answer-TRUE (T/F) you cannot create polymorphic collections - Correct Answer-FALSE What does API stand for and what is it? - Correct Answer-Application Programming Interface, the API is part of a model that are exposed to the user (public features). For example, when you write projects you use the String class, it has an API which are the features from that class available to users. Overall in an API you find public variables, public methods, and the contracts of the methods. What did Fawzi mention is important to remember in regards to the API? - Correct Answer-When changing code, make sure to leave the API the same so the outside user doesn't have anything to worry about. What is encapsulation? - Correct Answer-Hiding something in a private shell, in order to access private members they use public methods. What are the 2 types of encapsulation? What do they each mean? - Correct Answer- data encapsulation - make the data private procedural encapsulation - switching out an algorithm behind the scenes and no user will know (rule of thumb: encapsulate what may change later). Why do the wrapper classes exist? - Correct Answer-In order to make collections of these values. Collections can only collect objects, not primitives.
(T/F) all the wrapper classes are immutable. - Correct Answer-TRUE. You cannot change the state of the object. Which declaration is correct? a) Integer x = new Integer(7); b) Integer x = Integer.valueOf(7); - Correct Answer-b is correct, a is deprecated. b is a better option because the Integer object is immutable so there is no need to create a new copy of seven every time, since aliasing is not a concern. explain auto-boxing - Correct Answer-java is expecting an integer object but you give it a primitive, it changes it to the wrapper class Integer automatically instead, this is called auto boxing. explain auto-unboxing - Correct Answer-if you give it an integer object when java is expecting primitive int, java will accept it and unboxes the 7, and turns it into a primitive. collections framework - Correct Answer-Many classes/interfaces that allow easy use of data structures and types. (EXAMPLE: array lists) How would you create a new array list called x of type Cat? what is wrong with the deprecated version? - Correct Answer-ArrayList
What exception gets thrown when using a for each loop to remove elements from a collection? - Correct Answer-java.util.ConcurrentModificationException (T/F) When you pass a primitive variable to a method, the method CAN modify it - Correct Answer-FALSE (T/F) A method call that passes an object as a parameter can potentially modify the state of a reference to that object. - Correct Answer-TRUE What does it mean for a class/object to be immutable? - Correct Answer-The state (instance variables) are set during construction but cannot be modified after that. What does it mean for a class/object to be mutable? - Correct Answer-The state is set during construction and it might be modified later. What are privacy leaks? - Correct Answer-Suppose a mutable member has been intentionally encapsulated (made private), modification of its 'state' within the class is expected ("mutation"). Direct mutation by external code should not be possible, if it does then there is a privacy leak. A constructer is returning a direct reference to a private instance variable called symbols, therefore if you change a variable pointing to that reference, it will change the private variable as well. How can we avoid this? - Correct Answer-use the copyOf method so it creates a copy rather than pointing it to the direct reference. What we did here to fix the problem is called a 'defensive copy' (note: copyOf works on arrays not array lists) "Exposing private mutable data is typically a ____________" - Fawzi - Correct Answer- privacy leak How does the compareTo(T other) method work? - Correct Answer-- if current object and parameter are equal, this method returns 0
A class called Dog extends a class called Animal, this would make Dog the _____ class and Animal the __________ class. - Correct Answer-sub, super (T/F) inheritance can allow you to take advantage of polymorphism. - Correct Answer- TRUE Imagine there is a class called Bird and 2 classes that directly extend it are Eagle and Turkey. Additionally there is a class called Bald Eagle that extends Eagle. What kinds of objects can b represent? public void takeBath(Bird b) {....} - Correct Answer-b can represent a Bird, Eagle, Bald Eagle, or a Turkey If a class called Bird extends a class called animal, and a class called Turkey extends Bird, What does Turkey inherit from Bird? - Correct Answer-Turkey will inherit any methods Bird inherited from its superclass as well as any additional methods in the Bird class. (note: Turkey which is a subclass of Bird inherits the overridden methods within Bird rather than the methods from Birds superclass.) //if you are confused watch lecture # What is allowed in a java class? What is not? - Correct Answer-Instance variables, static variables, instance method implementations, static method implementations, instance method prototypes with no code that are declared "abstract", constructors, unlimited number of instances. Cannot have instance method prototypes with no code that aren't declared "abstract." What is allowed in a java interface? What is not? - Correct Answer-Final static variables, instance method implementations declared as default, static method implementations, instance method prototypes with no code. Cannot have constructors or any number of instances. What is allowed in a java enumeration? What is not? - Correct Answer-Instance variables, static variables, instance method implementations, static method implementations, private constructors only, fixed number of instances. Cannot have instance method prototypes without code, does not have unlimited instances, constructors cant be public. implements vs extends - Correct Answer-classes implement interfaces classes extend other classes class X extends Y implements A, B, C {...}
When is c.contains(a) true? (c is a collection) - Correct Answer-It is true if there is an object in the collection that "looks" the same as the argument a. (T/F) The "default" implementation of equals (inherited from object class) is the same as == - Correct Answer-TRUE (Note: because the default implementation of the equals method works the same as ==, it is important to override it and implement it differently if needed.) Study the proper equals method found in lecture..... - Correct Answer-13... Sorry the example for the proper equals method is too big to put on Quizlet. Let's say there is a class called Student that extends Person. Which of these are ok? a) Person x = new Student(); b) Student y = new Person(); - Correct Answer-Option a works because x is polymorphic. Option b does not work because a person is not a student. Let's say there is a class called Student that extends Person and we write: Student s = new Student(); Person p = s; //Person has instances such as name and ID //Student has year and GPA what objects will we be able to access if we use s? - Correct Answer-The whole thing. This includes name, ID, year, and GPA which belong to the Person class and Student class. Let's say there is a class called Student that extends Person and we write: Student s = new Student(); Person p = s; //Person has instances such as name and ID //Student has instances such as year and GPA which of these objects will we be able to access if we use p? - Correct Answer-p can only access the features belonging to the Person class (name and ID number). In an instance method (or constructor) of student class, what is "this"? - Correct Answer-"this" sees the object as the class it was declared in In an instance method (or constructor) of student class, what is "super"? - Correct Answer-"super" sees the object as the superclass of the current class How do "this" and "super" behave in memory? - Correct Answer-'this' is referring to a whole object including features inherited from a potential superclass, 'super' is only referring to features from super class when called inside a subclass.
How is an instance of a subclass constructed? - Correct Answer-Makes a call to the superclass constructor. Why? -members are private -superclass likely has "policies" that need to be enforced. (T/F) If you are in a class that extends another class, the subclass cannot make an assignment to a private variable from its super class. - Correct Answer-TRUE. But how can we fix this? To fix this we need to have a public constructor in the superclass to take care of those variables, and then in the subclass there will be another constructor to take care of the rest of the variable assignments found in itself. flip this card for an example where we use super to invoke the constructor from the super class - Correct Answer-public class Student extends Person{ private double GPA; private int admitYear; public Student(String name, int ID, double GPA, int admitYear){ super(name, ID); this.GPA = GPA; this.admitYear = admitYear; } general summary of "super" - Correct Answer-"super" is used in constructors to invoke super class version of constructor, and you'll see it in many methods such as overridden methods. if you need to call a method from the super class inside the over ridden method of the subclass, you need to put super.nameOfMethod to prevent using the new overridden version. (T/F) your class only extends another class if you specify that. - Correct Answer-FALSE. -every class you write does extend something -if your class doesn't explicitly extend something, it will extend an object -object is always at the root of the inheritance tree What class is said to be 'the mother of all classes'? - Correct Answer-the object class What is found in the object class? - Correct Answer-clone(), toString(), equals(Object obj), finalize(), getClass(), hashCode(), notify() (T/F) every single object you write inherits the code from the object class. - Correct Answer-TRUE Class vs Type - Correct Answer--every object has exactly one class -the class of an object cannot change -objects may be of many types
What's better about late binding? - Correct Answer-The same line can be used to call different walk methods, it just depends on the class of the current object. We can take advantage of polymorphism. Does java have early binding or late binding? - Correct Answer-java has late/dynamic binding Does c++ have early binding or late binding? - Correct Answer-c++ does early binding (Fawzi used to program in c++ a lot) Whoever is reading this, make sure you understand..... - Correct Answer-the late binding example Fawzi showed at the end of lecture 18. He said it will be quiz/exam material and I cannot type the whole thing here! When casting, what are we trying to do when writing this statement? (Foo)x - Correct Answer-x is not type foo, and we are trying to treat the object x refers to as if it were type foo. It will not change it to foo but it will treat it that way. (note: x is a reference and foo is a type) When will (Foo)x compile? - Correct Answer-If you look at the inheritance diagram and the type of either x or foo is above the type of x or foo then it will compile. (Foo)x What is an upcast? - Correct Answer-If the type of foo is above x's type in the inheritance diagram, it would be called an upcast. (Foo)x What is a downcast? - Correct Answer-If the type of foo is below x's type in the inheritance diagram, it would be called a downcast. upcasting example - Correct Answer-Student s = ....; Person p = (Person)s; can also be done implicitly: Person p = s; this works very well because a student is a person. downcasting example - Correct Answer-Person p = ....; Student s = (Student)p; Sometimes this works sometimes it does not. -if p refers to an object of type student it's fine -if p refers to an object that is not type student, ClassCastException is thrown. (T/F) Down casting can be done implicitly - Correct Answer-FALSE. Down casting is never done implicitly by compiler.
Person p = new Person(); Student s = new Student(); Person tricky = new Student(); Wont compile, throws exception, or works fine? a) Person y = s; b) Student y = p; c) Student y = (Student)p; d) Student y = tricky; e) Student y = (Student)tricky; f) (Faculty)s g) (Faculty)tricky - Correct Answer-a) implicit upcast - works fine b) attempted downcast - wont compile c) compiles, but throws exception d) downcast - does not compile e) works fine f) does not compile g) compiles, but throws exception Lets say there is a class called Student that extends a class called Person and we write: Student s = .....; Person p = (Person)s; Is this up casting or down casting? will it work? - Correct Answer-This is upcasting and it will work without any issues. Lets say there is a class called Student that extends a class called Person and we write: Person p = ......; Student s = (Student)p; Is this up casting or down casting? will it work? - Correct Answer-This is down casting. It compiles but will throw an exception during run time if the object p on the heap is not type student, if it is type student than this will work without any issue. Lets say there is a class called Student that extends a class called Person and we write: Person p = ......; Student s = (Student)p; How can we make sure this down cast will not throw any exceptions? - Correct Answer- if (p instanceof Student) { Student s = (Student)p; }
access levels of: 'package' (default) - Correct Answer-public features can be accessed: -class -package access levels of: 'private' - Correct Answer-public features can be accessed: -class Can you override a variable that is inherited? - Correct Answer-Yes it is called shadowing. Not a good idea to do that... Features of abstract classes - Correct Answer-- Cannot be instantiated directly, (cannot write Foo f = new Foo())
If you have a class that implements the Comparable interface and defines the natural order inside of the class (with the compareTo method), how would you be able to write an additional compare method if you wanted to order your objects differently from the natural order? - Correct Answer-You could create a separate class (implementing Comparator) which can be provided to define additional orderings. The Comparator interface has one method called compare that expects 2 parameters. ex: public class PlanetMassComparator implements Comparator
Ex: situations where exceptions will occur (2/2 checked exceptions). - Correct Answer-- A file not found exception can happen when your program is supposed to operate on a file and the file is not there.
What are the two kinds of nested classes? - Correct Answer-1) Static nested class: public class A { private static class B { ...... } }
(T/F) Inner classes have a feature where they can access its outer classes private instances. - Correct Answer-TRUE. Anonymous inner class example: new Foo() { ......... }; - Correct Answer-Anonymous inner classes do not have names, in this example we see new Foo(), and inside the brackets we can implement the Foo interface. The definition of the anonymous class and instantiation go inside the brackets. When would you use an anonymous inner class? - Correct Answer-Used when a class will only be instantiated in one place in the code. Additional features of anonymous inner classes - Correct Answer-- The class is declared and instantiated at the same time
(T/F) Your class does not always extend another class. - Correct Answer-FALSE. Your class ALWAYS extends something. (T/F) If the super class of your class is not cloneable, then you cannot make your class cloneable. - Correct Answer-TRUE. (it has to be a chain) To make your class "Cloneable" you must: - Correct Answer-- Ensure that the superclass of your class is the Object class or implements cloneable.