CMSC 132 Exam review

¡Supera tus tareas y exámenes ahora con Quizwiz!

general summary of "super"

"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.

In an instance method (or constructor) of student class, what is "super"?

"super" sees the object as the superclass of the current class

In an instance method (or constructor) of student class, what is "this"?

"this" sees the object as the class it was declared in

How do "this" and "super" behave in memory?

'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 does the compareTo(T other) method work?

- if current object and parameter are equal, this method returns 0 - if current object is bigger than parameter, this method returns positive value - if current object is smaller than parameter, this method returns negative value

Advantages of early binding

-decision is made in the beginning so run time is a little bit faster -it is easier to understand

What can an enumeration consist of?

-enumerations are almost the same as classes they can have instance members (variables and methods) -they can have static members (variables and methods) -you can give them states/behaviors

Class vs Type

-every object has exactly one class -the class of an object cannot change -objects may be of many types

What method can be used to return the index of an instance in an enumeration?

.ordinal() example: Day.TUE.ordinal() returns the index of Tuesday (note: Day.values()[3] will return the day at the specified index)

How can we generate an array representing the pre defined constants in an enumeration?

.values() method can be called on an enum, it returns an array of the enums instances.

Study the proper equals method found in lecture.....

13... Sorry the example for the proper equals method is too big to put on Quizlet.

What is found in an enumeration?

An enum is like a class, there are a small number of pre-defined instances, identified with "symbolic constants". example: public enum Day { Sun, Mon, Tues, Wed, Thurs, Fri, Sat; }

A class called Dog extends a class called Animal, this means a Dog Is-A ____________.

Animal

Explain how to use hasNext() method from the Iterator interface

Anytime you are using an iterator you can call hasNext() on the iterator to ask if there are more elements or if you already went through them all.

What does API stand for and what is it?

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.

How would you create a new array list called x of type Cat? what is wrong with the deprecated version?

ArrayList<Cat> x = new ArrayList<>(); (note: the deprecated version does not specify what type of object goes into the list, if you were to add a Dog to Cat list, it would still compile but an exception will be thrown if you try to remove the Dog object).

What are some methods in the collections class?

Collections.shuffle(list) - shuffles an array list int max = Collections.max(list) - returns max + few more spoken about in lecture

The ________________ interface defines the "natural order" for instances of the class. It is used in hundreds of places in java libraries including Collections.sort, Collections.max, Collections.min, Collections.binarySearch, Arrays.sort, Arrays.binarySearch.

Comparable

The classes that implement an interface must implement ___________

Instance methods from interface (note: default instance method does not need to be implemented, it has already been inherited).

(T/F) When you pass a primitive variable to a method, the method CAN modify it

FALSE

(T/F) constructors for enumerations are always public

FALSE

(T/F) you cannot create polymorphic collections

FALSE

(T/F) your class only extends another class if you specify that.

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

(T/F) Down casting can be done implicitly

FALSE. Down casting is never done implicitly by compiler.

class X extends Y implements A, B, C {...} (T/F) A, B, and C are other classes

FALSE. They are interfaces.

class X extends Y implements A, B, C {...} (T/F) Y Is-A X

FALSE. X Is-A Y.

class X extends Y implements A, B, C {...} (T/F) X does not have to implement all unimplemented methods in A, B, C

FALSE. X must implement all unimplemented methods in A, B, C.

class X extends Y implements A, B, C {...} (T/F) X does not inherit the features of Y

FALSE. X will inherit all features of Y.

What is allowed in a java interface? What is not?

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 encapsulation?

Hiding something in a private shell, in order to access private members they use public methods.

(Foo)x What is an upcast?

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?

If the type of foo is below x's type in the inheritance diagram, it would be called a downcast.

When will (Foo)x compile?

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.

How would you access objects from an enumeration named 'Day' ?

If you wanted to access Monday you type: Day.Mon Day is the name of the enum, Mon is the object

Why do the wrapper classes exist?

In order to make collections of these values. Collections can only collect objects, not primitives.

What is allowed in a java class? What is not?

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 enumeration? What is not?

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.

When is c.contains(a) true? (c is a collection)

It is true if there is an object in the collection that "looks" the same as the argument a.

After creating a list of Strings, what would you type before being able to call methods from the iterator interface?

Iterator<String> iterator = nameOfList.iterator(); //examples of the methods being used: while(iterator.hasNext()){ String word = iterator.next() System.out.println(word); }

How is an instance of a subclass constructed?

Makes a call to the superclass constructor. Why? -members are private -superclass likely has "policies" that need to be enforced.

collections framework

Many classes/interfaces that allow easy use of data structures and types. (EXAMPLE: array lists)

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();

Option a works because x is polymorphic. Option b does not work because a person is not a student.

downcasting example

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.

What is an iterator?

Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Methods from Iterator interface include: hasNext(), next(), remove()

upcasting example

Student s = ....; Person p = (Person)s; can also be done implicitly: Person p = s; this works very well because a student is a person.

Define overriding

Subclass "replaces" inherited method from superclass with the exact same prototype, but different implementation.

What are privacy leaks?

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.

(T/F) - You can add other code to the classes that inherit the superclass features aside from the interface method implementations.

TRUE

(T/F) A method call that passes an object as a parameter can potentially modify the state of a reference to that object.

TRUE

(T/F) enumerations have a fixed number of objects defined inside.

TRUE

(T/F) every single object you write inherits the code from the object class.

TRUE

(T/F) inheritance can allow you to take advantage of polymorphism.

TRUE

(T/F) with enumerations you should not use .equals method, use double ==

TRUE

class X extends Y implements A, B, C {...} (T/F) An instance of X can be assigned to variables of types X, Y, A, B, C.

TRUE

class X extends Y implements A, B, C {...} (T/F) An instance of X is only "class X"

TRUE

class X extends Y implements A, B, C {...} (T/F) X can only extend one thing

TRUE

class X extends Y implements A, B, C {...} (T/F) X is a subclass of Y (Y is the superclass of X)

TRUE

(T/F) The "default" implementation of equals (inherited from object class) is the same as ==

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.)

class X extends Y implements A, B, C {...} (T/F) X "can do" A, B, C

TRUE.

(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.

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.

(T/F) all the wrapper classes are immutable.

TRUE. You cannot change the state of the object.

Suppose there is a class called Student that extends class Person and they both contain a method walk(); Person p = new Student(); p.walk(); //explain late/dynamic binding in this context.

The decision of what method will get called is made during run time, while the program is running and java gets to that line, it checks where the object p is at this moment and sees what class it is and then call the method from that class. We see that p is a new student so it will use the walk method from the student class.

Explain how to use next() method from the Iterator interface

The iterator goes through each element of collection one by one, when you call next() method it moves the marker forward to the next element and returns an element.

What's better about late binding?

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.

What does it mean for a class/object to be immutable?

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?

The state is set during construction and it might be modified later.

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?

The whole thing. This includes name, ID, year, and GPA which belong to the Person class and Student class.

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?

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: Student s = .....; Person p = (Person)s; Is this up casting or down casting? will it work?

This is upcasting and it will work without any issues.

If a class called Bird extends a class called animal, and a class called Turkey extends Bird, What does Turkey inherit from Bird?

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 #11

Define overloading

Two or more methods in a class have the same name but different parameters.

What did Fawzi mention is important to remember in regards to the API?

When changing code, make sure to leave the API the same so the outside user doesn't have anything to worry about.

When should you use the key word 'Override'?

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).

Lets say there is a class called GradStudent that extends Student, and Student extends Person. a) Is this statement allowed? Person p = new GradStudent(); b) what is the class of the instantiated object?

a) Yes, it is allowed because a grad student is a person. b) grad student is the class.

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

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

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) {....}

b can represent a Bird, Eagle, Bald Eagle, or a Turkey

Which declaration is correct? a) Integer x = new Integer(7); b) Integer x = Integer.valueOf(7);

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.

Does c++ have early binding or late binding?

c++ does early binding (Fawzi used to program in c++ a lot)

implements vs extends

classes implement interfaces classes extend other classes

What is found in the object class?

clone(), toString(), equals(Object obj), finalize(), getClass(), hashCode(), notify()

What are the 2 types of encapsulation? What do they each mean?

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).

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?

if (p instanceof Student) { Student s = (Student)p; } (instanceof helps guarantee it will not throw a class caste exception)

explain auto-unboxing

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.

How would the compareTo() method work when used on enumerations?

its return value is based on the order you typed the Days in. So Monday will be greater than Wednesday since it was typed first.

Does java have early binding or late binding?

java has late/dynamic binding

explain auto-boxing

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.

What exception gets thrown when using a for each loop to remove elements from a collection?

java.util.ConcurrentModificationException

What do you find in a java interface?

method prototypes, final static variables, static methods (note: instance methods are ok if you use the term 'default').

A class can only extend ________ other class. But a class can implement multiple ___________(s).

one, interface

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?

p can only access the features belonging to the Person class (name and ID number).

"Exposing private mutable data is typically a ____________" - Fawzi

privacy leak

flip this card for an example where we use super to invoke the constructor from the super class

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; }

A class called Dog extends a class called Animal, this would make Dog the _____ class and Animal the __________ class.

sub, super

Suppose there is a class called Student that extends class Person and they both contain a method walk(); Person p = new Student(); p.walk(); //explain early binding/static binding in this context.

the compiler decides everything, it checks to see what type of variable p is and based on that they will bind that piece of code to the walk() method in the Person class. So this decision of what method is called is decided early during compilation, before the code has even run.

Whoever is reading this, make sure you understand.....

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!

What class is said to be 'the mother of all classes'?

the object class

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?

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)

When is x.equals(y) true?

when x and y are objects that look the same (same state).

When is x == y true?

when x and y are the same object (aliases)

When casting, what are we trying to do when writing this statement? (Foo)x

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)


Conjuntos de estudio relacionados

Solving Linear Equations and Inequalities

View Set

Why is water called the universal solvent

View Set

NR327 Maternal-Newborn Exam 2 Review

View Set

biochem ch17, 21, 19 clicker questions

View Set