본문 바로가기
What I Learned/Java

Java Quiz Summary

by 단풍국범생이 2019. 4. 8.

word => means it was on the exam

 

<Week10 - A>

1. Inheritance

Inheritance allows a subclass to be derived from its parent superclass.

The subclasses will inherit all the non-private members (fields and methods).

Inheritance is a form of code reuse.

Once subclass was inherited, no need to use super to access superclass methods; they are inherited automatically.

 

2. <is a> VS <has a>

<is a>: Inheritance

<has a>: Aggregation and Composition

 

3. Diagram

UML arrow points up to the superclass, the methods are passed down to the subclass

In subclass UML, it only needs to display the new members that will be added to the subclass.

 

4. Can you inherit from every class?

No. Classes that are declared final cannot be extended nor subclassed.

final makes each new object immutable; you cannot change it in a subclass.

 

5. Can you inherit from more than one class?

No. In Java, only single inheritance is permitted.

The multiple inheritances create a diamond problem.

 

6. The protected Access Modifier

Protected members are public inside a package and private outside the package.

But, they are visible via inheritance even in an external package.

Modifier Class Package Subclass World
public YES YES YES YES
protected YES YES YES NO
(none) YES YES NO NO
private YES NO NO NO

 

7. Super

(Reminder) Signature

(1) name of the method

(2) number of the parameters

(3) type of each parameter

(4) order of the parameters

 

Same name and a different signature -> overloaded

A subclass method has the same name and signature as in superclass -> overridden

 

[1] Subclasses don't inherit constructors; must call superclass constructors using super(...).

[2] Super(...) must appear on the first line of code

[3] Cannot use super to access private properties

 

8. Overloading VS Overriding

Overload

Override

Same method name, different signature Same method name and signature

Different Code

May chain constructors and methods using this

Different Code

May call on superclass methods using super

Can overload constructors Constructors not inherited

 

9. @Override

Overriden header declaration causes Java to check that the method exists in the superclass.

It acts as signature-checker.

 

10. Can every method in a non-final class be overridden?

No. Methods can themselves be made final; you have to use this method as is, cannot override it.

 

11. Using instanceof

instanceof operator can be used to distinguish between objects derived from the same parent class.

i.e. if(obj instance of Circle){ ... }

 

 


<Week10 - B>

1. Arrays Definition

Datatype[] identifier = new Datatype[arraysize];

 

2. The Arrays Utility Class

(1) The Arrays class should not be confused with the Array class.

(2) The Arrays class is capable of manipulating arrays of objects. However, this requires the use of generics.

generics: A way of passing the type of class to a method, which uses a new notation in the form <ClassName>.

 

3. ArrayList

(1) Must specify the type of the class held by the ArrayList using the generic element, <E>.

This notation warns us that the ArrayList expects us to specify the type of data used.

(2) As of Java 7, no longer need to write the data type on both sides. -> type inference

i.e. ArrayList<String> seasons = new ArrayList<>();

(3) Only use ArrayList with a reference type. However, Java contains classes that wrap the primitive data types.

 

4. Generic Methods

(1) The angular brackets, diamond notation, will hold the name of the actual data type being used at compile time.

(2) Thus <E> stands for a generic data type.

(3) <E> does not represent the data being passed; it represents the type of data being passed (reference type)

i.e.

1

2

3

4

5

6

public class GenericPrintTest{

    public static <E> void print(E[] list){

        for(E printThis: list) System.out.print(printThis+" ");

        System.out.println();

    }

}

 

(4) The declaration of <E> always appears after static and before the return type.

(5) When a class has been parameterized with a type, the methods inside it do not need to be redeclared.

i.e. +copyOf(original: T[], newLength: int) : <T> T[]

     -> public static <T> T[] copyOf(T[] original, int newLength){}

 

5. array VS ArrayList

array ArrayList
String[] list = new String[10]; ArrayList<String> list = new ArrayList<>();
list[index] list.get(index);
list[index] = "London"; list.set(index, "London");
list.length list.size();
  list.add("Paris");
  list.add(index, "Rome");
  list.remove(index);
  list.remove("London");
  list.clear();

+ using asList() method to convert from array to ArrayList.

   toArray() method to conver ArrayList to array.

 

6. Formal VS Actual parameters and types

A method declaration has a set of formal parameters that appear in the method header.

While the identifier that you use to pass information to the methods is called the actual parameter.

 

 


<Week11 - A>

(1) CookieCutter superclass - abstract class

    GingerBreadManCookieCutter - concrete class

(2) Certain general features are abstracted away from a class into a superclass - abstraction

(3) Abstraction and Encapsulation are complementary

- Abstraction addresses the common observable behaviors of a class

  Encapsulation hides the features that should be kept hidden from the user

- A programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency.

(4) Class hierarchy: Series of super and subclasses whether abstract or concrete

(5) OOP 4 Concepts

   1. Encapsulation: Properties and methods are bundled together into a class in a process; data hiding

   2. Inheritance

   3. Abstraction: The certain features found in subclasses are abstracted away into abstract superclasses, which cannot be instantiated.

   4. Polymorphism: It allows an object of one class to be treated as if it were derived from a class higher up in the class hierarchy.

i.e. GingerbreadManCookie class could be treated as if it had been derived from the CookieCutter, Bakeware, Kitchenware classes since a GingerbreadManCookie class belongs to all of these categories.

(6) A problem of Polymorphism: Objects of one type polymorphically have access to the objects of another, perhaps inappropriate type, is called type safety.

Solution: Java uses generic declarations to handle this problem. And Interfaces.

+ casting, not recommended.

 


<Week11 - B>

1. Abstract Class

Italics: indicates abstract class in UML

 

2. Abstract Method

(1) Lack a body; no curly braces {}

(2) The word abstract must be included in the class header; the header must include abstract.

(3) An abstract class does not need to contain any abstract methods.

(4) A subclass does not need to implement all the abstract methods found in the superclass, but only if the subclass is abstract itself.

(5) Object itself is not an abstract class.

(6) While you cannot create an instance of an abstract class, you can use an abstract class as a data type.

 


<Week12 - A>

(1) Polymorphism: A variable of a superclass type can be used to store or pass a variable of one of its subclasses.

i.e. public void displayObject(GeometricObject object){...}

    Circle circ = new Circle();

    displayObject(circ);

-> Formal parameter type: GeometricObject, Actual type: Circle

 

(2) Polymorphism works with 'is a' relationships

i.e. public class GregorianCalendar extends Calendar

    GregorianCalendar object 'is a' Calendar object as well.

 

(3) Generic Methods and Polymorphism

Generic methods can be used inside classes to help eliminate the need for overloading.

(We can eliminate the need for 'instanceOf' by the thoughtful use of generics)

i.e. public static <T extends GeometricObject> boolean equalArea( T obj1, T obj2 ){ ... }

-> The generic type is said to be bounded. T is bounded to being either a GeometricObject or one of its subclasses.

In many cases, wildcard '?' is equivalent to 'T'. However, this won't work all the time.

i.e. static void printList(ArrayList<? extends Number> list) {...}   -> Can be float and int and so on

    static <T extends Number> void printList(ArrayList<T> list) {...}  -> Guaranteed to be of the same type

 

(4) Why do we use generic methods?

It makes sure that the object is the right one.

To catch errors at compile time rather than run time. Even if we use 'instanceOf' to determine the type of an object, we have to wait until run time.

 

(5) Data Structures

A stack is a type of data structure, a specialized program designed to organize and manage data.

Analogy - Plates may be pushed onto the top of the stack, or popped off the top.

Java automatically wraps a primitive data type in its class equivalent is called autoboxing.

When used in <Object>, as a generic type, without a specific type, a generic class is called a raw type.

-> Not recommended

 


<Week12 - B>

Exceptions: They are thrown by methods when unresolvable problems are encountered during execution.

Exception Handling(=Error Handling): It means dealing with the different kinds of exceptions that can be generated during normal program execution.

 

(1) Two parts to Exceptions and Exception Handling

  1. An exception is declared so that when it is thrown during execution.

  2. It is caught in the appropriate catch statement.

 

(2) Exception Handling model

  1. Provide a try-catch block

- try-catch block that traps exceptions and deals with them.

- Following execution of the catch block, program execution does not return to the statement after the one that triggered the exception.

- Responsibility for catching the exception works its way progressively back through the original call stack.

  2. Throw the exception

- explicitly using the throw keyword.

- For a certain class of exceptions, called checked exceptions, we need to add a third operation

  3. Declare the exception

- The throws keyword is used to declare the kind of exception that could occur.

 

(3) Polymorphism in Exception Handling

The superclass cannot precede subclass

It is possible to deal with multiple catches on the same line using | (OR) notation.

It is possible to declare multiple throws in the same method's header.

 

(4) Checked exception VS Unchecked exception

Checked exception: you must use throws in the method header. (IOExcpetion, FileNotFoundException)

i.e. private double division(int n1, int n2) throws IOException {

          if(n2==0) throw new IOException();

          return ((double)n1/n2);

     }

Unchecked exception: you are not required to do so.

 

 

 

 

'What I Learned > Java' 카테고리의 다른 글

Java Final Summary  (0) 2019.04.21
Hybrids (1~7) Summary  (0) 2019.04.21