KU COSE102 2019기말고사 복습 Part 2

3 minute read

KU COSE102 2019 기말고사 복습 Part 2

되냐 되냐?

상속

JAVA 14

Object-oriented programming allows you to define new classes from existing classes. This is called inheritance.

Inheritance enables you to define a general class (i.e., a superclass) and later extend it to more specialized classes (i.e., subclasses).

super

JAVA 14

JAVA 14 - constructor relation

The keyword super refers to the superclass and can be used to invoke the superclass’s methods and constructors.

The this Reference, introduced the use of the keyword this to reference the calling object.

The keyword super refers to the superclass of the class in which super appears.

It can be used in two ways:

  • To call a superclass constructor.
  • To call a superclass method.

Override

JAVA 16

To override a method, the method must be defined in the subclass using the same signature and the same return type as in its superclass.

Overloading

JAVA 11

Overload vs Override

Overloading means to define multiple methods with the same name but different signatures.

Overriding means to provide a new implementation for a method in the subclass.

Object Class

JAVA 19

Every class in Java is descended from the java.lang.Object class.

toString()

JAVA 19

public String toString()

Invoking toString() on an object returns a string that describes the object.

By default, it returns a string consisting of a class name of which the object is an instance, an at sign (@), and the object’s memory address in hexadecimal.

equals()

JAVA 19

Like the toString() method, the equals(Object) method is another useful method defined in the Object class.

public boolean equals(Object o)

default implementation

public boolean equals(Object obj) {
  return (this == obj);
}

ArrayList

JAVA 22

An ArrayList object can be used to store a list of objects.

final

JAVA 13

Error & Exception

JAVA 08

Exception handling enables a program to deal with exceptional situations and continue its normal execution.

Exceptions are thrown from a method. The caller of the method can catch and handle the exception.

Exceptions are objects, and objects are defined using classes. The root class for exceptions is java.lang.Throwable.

Custom Exception

JAVA 38

try-catch-finally

JAVA 08

Abstract Class 추상 클래스

JAVA 17

A superclass defines common behavior for related subclasses. An interface can be used to define common behavior for classes (including unrelated classes).

An abstract class cannot be used to create objects. An abstract class can contain abstract methods, which are implemented in concrete subclasses.

An abstract method cannot be contained in a nonabstract class.

If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as abstract.

In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented.

Also note that abstract methods are nonstatic.

A class that contains abstract methods must be abstract.

However, it is possible to define an abstract class that doesn’t contain any abstract methods.

In this case, you cannot create instances of the class using the new operator.

This class is used as a base class for defining subclasses.

Interface 인터페이스

JAVA 17

An interface is a class-like construct that contains only constants and abstract methods.

An interface is treated like a special class in Java.

Each interface is compiled into a separate bytecode file, just like a regular class.

You can use an interface more or less the same way you use an abstract class.

abcandint

Calendar Class

JAVA 42

Comparable Interface

The Comparable interface defines the compareTo method for comparing objects.

package java.lang;

public interface Comparable<E> {
    public int compareTo(E o);
}

The compareTo method determines the order of this object with the specified object o and returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than o.

The Comparable interface is a generic interface.

The generic type E is replaced by a concrete type when implementing this interface.

comparable

Since all Comparable objects have the compareTo method, the java.util.Arrays.sort(Object[]) method in the Java API uses the compareTo method to compare and sorts the objects in an array, provided that the objects are instances of the Comparable interface.

예시코드

출력결과

Width: 3.4 Height: 5.4 Area: 18.36
Width: 1.4 Height: 25.4 Area: 35.559999999999995
Width: 7.4 Height: 35.4 Area: 261.96
Width: 13.24 Height: 55.4 Area: 733.496

Cloneable Interface

JAVA 39

JAVA 41

The Cloneable interface specifies that an object can be cloned.

Categories: ,

Updated:

Comments