본문 바로가기
What I Learned/Java

Java Final Summary

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

<Week13>

1. Three Reference Types

a. (arrays)

b. Classes

c. Interfaces 

- consisted entirely of constants and abstract methods

- it cannot be instantiated

- all of its abstract methods must be overridden in the first concrete subclass

 - can have multiple interfaces

- implement rather than extends

i.e. public interface Comparable<E> {

       public int compareTo(E o);

    }

 

2. Abstract Class VS Interfaces

a. must import interfaces same way as for classes

b. can use interfaces as a data type in the declaration of identifiers

c. every method must be overridden in the derived class unless the derived class is abstract itself

d. can use an interface for casting

e. can add multiple interfaces by separating each interface with a comma

f. the form of a bounded generic type for an interface uses 'extends'

   i.e. public class MyClass<T extends MyInterface>{ }

g. << >> (guimettes) is called stereotype, reminds us that we are not dealing with a class

h. should be used when there is a relationship or even an abstract has a relationship

i. purpose of an interface: declare a common set of behaviors for its derived classes, and therefore, any objects instantiated them

 

Supertype Must implement/extend to a concrete class before object instantiation? Must override every base class method in the derived class?
A concrete class NO NO
An abstract class with no abstract method YES NO
A class with at least one abstract method YES NO
An interface YES YES

(+) Java SE 8

1) can include static and non-abstract methods in interfaces

2) Class (and superclasses) > interface (higher precedence )

3) Derived interfaces > Base interfaces

4) if 2, 3 not resolve the conflict -> the implementing class must override and provide a method with the same signature

 

3. The Comparable Interface

a. In order to search for objects or sort them into some kind of order, you need to be able to determine whether one object is greater than, less than, or equal to another

- negative integer: one object's property is less than another's

- zero: equal

- positive: one object's property is greater than another's

b. Comparator

   i.e. public interface Comparator<T>{

           public int compare(T obj1, T obj2);

       }

Comparable: allows comparison between this object with T

Comparator: only allows for comparisons within the same class of type T

-> Something's odd... need to re-check

https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

 

4. Marker Interfaces: Cloneable

public interface Cloneable{}

marker interfaces: Interfaces used in this way with no abstract methods

The Cloneable interface's association with an object indicates that the object can be cloned

 

5. Marker Interfaces: Serializable

stream: a string of bytes flowing through the memory, a data structure that keeps track of the start and end of a contiguous array of bytes in RAM

Serialization: a process in which data is converted to a form that can be safely shipped

Deserialization: On the receiving end of the transmission, the bytes are reconstructed back into objects in a process

When you wish to store a custom-built object stream to or from a file, you must signal that it is Serializable first

 

6. The Collection Framework

The Collection interface contains basic methods used to the manipulation of any group of objects of type E

a) Set: does not contain duplicates

b) Queue: yielding elements up in the order

c) List: gives the user full control over the ordering of the elements

d) Map: key-to-value operations

Not to be confused with the Collections class - only has static methods

Collections.sort( List<T extends Comparable> list )

Collections.sort( List<T> list, Comparator<? super T> c )

 


<Week14>

1. A Brief History of Java and GUIs

AWT: the Abstract Windows Toolkit

Swing

JavaFX

 

2. JFrame

javax.swing.jframe library

 

import javax.swing.JFrame;

public class MySwingApp extends JFrame{

   public static void main(String[] args){

       JFrame frame = new JFrame();

       frame.setSize(800, 500);    // low-resolution device may not work

       frame.setTitle("Welcome to Java Swing");

       frame.setVisible(true);

   }

}

 

3. Event Handling

event: anything that results from an interaction with the user.

addActionListener()

i.e. JButton btnClickMe = new JButton("Click Me");

btnClickMe.addActionListener(...);

 

public interface ActionListener{

   public void actionPerformed(ActionEvent e);

}

 

4-Steps

1) Create a class that implements ActionListener

2) Override its actionPerformed()

3) Instantiated a new object

4) Pass this object as the parameter to addActionListener()

 

public class MyBtnHandler implements ActionListener{

  public void actionPerformed(){

     // code to be executed

  }

}

MyBtnHandler mbh = new MyBtnHandler();

btnClickMe.addActionListener(mbh);

 

4. Inner Classes

inner class: a class within another outer class

- Advantage

1) easing code maintenance

2) can be made private -> visibility is limited

3) package contains fewer classes

 

anonymous inner class: load the new ActionListener object directly into the button's addActionListener() method

leftButton.addActionListener(
    new ActionListener(){    // this () do not appear in a named inner class
    @Override 

     public void actionPerformed(){…} 

});  // Closes the class first, then the method

 

 

5. Lambda Expressions

lambda expressions: simplified event handling way, the most concise form of even handling possible, new to Java 8, replaces the anonymous inner class with the single, concise expression

 

new ActionListener(){   
    @Override 

     public void actionPerformed(ActionEvent e){

          // expression

    }

}

 

===> (ActionEvent e) -> expression 

 

 


Written Questions

1. Why do we need compareTo() method?

- To search or sort the objects into a certain order, we need to know whether one is greater than, less than or equal to another.

 

2. Why have two ways to compare?

- Because Comparable has compareTo method that compares this with the object read in, however, Comparator has compare method which compares the same class Type T with T.

 

3. What is the purpose of using interface?

- To declare a common set of behaviors for derived classes, so, any objects instantiated from it.

 

4. Why downcasting is problematic for reference values?

- Because subclass reference value cannot access methods that do not exist in the superclass.

 

 

 

 

 

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

Hybrids (1~7) Summary  (0) 2019.04.21
Java Quiz Summary  (0) 2019.04.08