MOOC/CS102 Introduction of CS2

Unit 2. The Building Blocks of Object-Oriented Programming

soicem 2016. 12. 10. 01:42

 본 포스팅은 자바의 객체 지향 프로그래밍에 대한 기초적인 내용을 담고 있습니다.



그림 1. JVM Memory Layout



 고전적 computer science의 언어 중 하나인 자바를 처음 사용할 시에 제가 느꼈던 장애 요소가 2가지 정도 존재했습니다.  

 첫 번째 , new의 키워드 의미를 정확하게 이해하지 못했습니다.  객체를 생성하는 것은 알겠는데, 그게 어떤 방식으로 생성되는지 알지 못했었습니다.  

 두 번째, 알아야되는 용어가 많아서 흐릿한 배경 지식을 가지고 코딩을 하였습니다.   polymorphism, Inheritance, encapsulation, class variable, instance variable, subclass, super class, garbage collector 등이 내포한 의미를 정확히 알지 못했습니다.

 제가 느꼈던 장애 요소 두 가지를 해결할 수 있을만한 좋은 질문들에 답하며, 느꼈던 답답함을 해결해보도록 하겠습니다.


 

 no

 Question 

 Answer

 1

 객체 지향 프로그래밍에서는 클래스와 객체를 사용한다.  클래스와 객체는 무엇인가?  클래스와 객체는 어떤 관계가 있나? 

 클래스란 객체를 정의해놓은 것이며객체를 생성하기 위해서 사용된다.  클래스는 객체를 생성하기 위해 사용될 뿐객체 그 자체는 아니다

 객체란 실제로 존재하는 것이며클래스의 속성과 기능을 가진다

 예를 들어, People이라는 클래스를 정의하였다고 가정하면, Namgue는 객체가 될 것이다.

 2

 인스턴스 변수와 인스턴스 메소드란 무엇인가?

 인스턴스 변수와 메소드는 static을 붙이지 않은 클래스 안의 변수이다인스턴스 객체가 생성될 때인스턴스 변수와 메소드는 생성된다.  객체가 해제되면인스턴스 변수와 메소드도 사라진다.

 3

 자바에서 null 포인터의 의미, 그리고 왜 이 값이 필수적인지 설명하라 

 객체 타입을 가지는 변수를 선언하였을 때객체 생성을 하지 않았을 경우해당 변수는 NULL 값을 가진다.  객체 생성을 하였을 경우 해당 변수는 객체에 대한 주소를 가진다.  NULL의 의미는 해당 객체의 주소를 변수가 가지고 있는지가지고 있지 않은지를 의미한다.

 4

 생성자가 무엇인가?  클래스 안에서 생성자의 목적은 무엇인가? 

What is a constructor?  What is the purpose of a constructor in a class?

 객체 생성 시에만 호출되며필드를 초기화한다.  클래스와 같은 이름을 사용해야 하며, 여러 개의 매개변수를 가질 수 있다

 5

 kumquat이란 클래스의 변수 타입을 가지는 fruit이 있다.  "fruit = new Kumquat();"이 무엇을 의미하는가?  그리고 위의 구문이 실행될 때, 컴퓨터에선 어떤 동작을 수행하는가?

 Kumquat이란 클래스를 타입으로 가지는 fruit이란 이름의 객체를 생성한다. "fruit = new Kumquat();" 구문이 실행이 된다면, Method Area에 저장되어 있던 Kumquat 클래스의 요소들이 heap에 복사된다해당 복사된 요소들에 대한 참조 주소는 fruit 변수에 초기화된다.

 6

 인스턴스 변수와 인스턴스 메소드가 의미하는 바가 무엇인가?

 인스턴스 변수와 메소드는 객체가 가지는 특성과 기능을 의미한다.  해당 객체에 종속적인 특징과 기능을 의미하며해당 객체 이외에 범용적으로 사용될 수 없다.

 7

 서브 클래스와 수퍼 클래스의 의미를 설명하라 

 객체 지향 프로그래밍에서선언된 클래스의 모든 요소들은 다른 클래스로 상속될 수 있다.  다른 클래스로부터 상속받은 클래스는 상속 받은 요소들을 수정하거나또는 추가할 수 있다.  상속을 받은 클래스를 서브 클래스상속을 한 클래스는 수퍼 클래스라 칭한다.  예를 들면,

 

 public class Class1 extends Class2{ ~ }

 

 와 같은 상황에서 Class1 Class2의 서브클래스고 Class2 Class1의 수퍼클래스다.

 8

 Playr라는 Class에서 선언되어 있지 않음에도 불구하고 toString()이란 메소드를 사용할 수 있는 이유에 대하여 설명하라

 자바에서 최상위 클래스는 Object 클래스이다.  기본적으로 모든 클래스는 Object 클래스로부터 상속받는다.  toString() Object 클래스에 정의된 메소드이다.  따라서사용자가 따로 정의하지 않아도 toString을 정의하고 있는 Object 클래스에서 상속받으므로, toString() 메소드를 사용할 수 있다.

 

 9

 다형성에 대해 설명하라

 다형성은 하나의 요소가 다양하게 변화될 수 있음을 의미한다.  다형성엔 오버로딩오버라이딩이 두 가지 요소가 있다.  


 오버로딩은 한 클래스에 이름이 동일한 생성자 혹은 메소드가 중복 정의되어 있는 경우를 말한다오버로딩은 생성자 혹은 메소드 따위를 호출할 때매개변수의 타입 또는 수를 가변적으로 만든다.  예를 들어문자열 타입의 매개변수를 정수형으로 받고 싶을 경우정수형의 매개변수를 받는 생성자 혹은 메소드를 추가해주면 된다.


 오버라이딩은 상속 구조에서 발생한다.  클래스를 상속할 경우메소드를 오버라이드하면 메소드에 대한 재정의를 해야한다.  이때상위 클래스에서 정의한 일반 메소드의 구현을 하위 클래스에서 모두 무시하고 다시 재정의해서 사용할 수 있다.  그러므로 상위 클래스에서 정의된 메소드와 하위 클래스에서 상속된 메소드의 이름을 같지만안의 내용은 다르다.  메소드를 오버라이딩할 경우 상위 클래스의 메소드는 무시되고하위 클래스의 메소드가 사용된다.

 10

 자바는 메모리 관리를 위해 가비지 컬렉터를 사용한다.  가비지 컬렉터는 무엇인가?  가비지 컬렉터의 대안에는 무엇이 있나? 

 객체를 생성하면 힙에 메모리가 할당된다.  해당 객체를 사용하고 있을 경우에 힙에 객체에 대한 메모리가 상주한다.  이후 객체의 사용이 끝난 경우메모리 할당을 해제시켜줘야 한다.  해제시키지 않고 이러한 메모리 할당을 지속적으로 방치할 경우 메모리 누수가 생기고 메모리 관리에 문제가 생겨 정상 동작이 불가능하게 될 것이다.  이러한 상황을 방지하기 위하여 Java에서는 실행 도메인이 끝났을 때자동으로 메모리를 해제시켜주는 가비지 컬렉터를 지원한다.  만약가비지 컬렉터를 지원하지 않을 경우, c++처럼 close 함수를 써서 수동적으로 해제시켜야 한다.

 11

 추상 클래스란 무엇인가, 자바에서 추상 클래스를 어떻게 식별하는가?

 추상 클래스란추상 메소드를 하나 이상 포함하고 있는 클래스를 말한다.  추상 메소드라 함은, abstract 키워드를 사용하고선언부만 존재하는 메소드를 뜻한다.  추상 클래스는 완전하지 않은 클래스이기에 반드시 상속하거나 익명 클래스로 추상 메소드를 정의해야하며그렇지 않을 경우 객체 생성을 할 수 없다.  자바에서 추상 클래스를 인식하는 방법은, abstract 키워드 또는 추상 메소드가 클래스 안에 있는지 확인하면 된다.

 12

 "this" 가 무엇을 의미하나

 this 키워드는 클래스 안에서 해당 클래스 자체를 가리킬 때 사용한다보통 클래스의 생성자에서 매개변수와 필드의 이름을 같게 지정하는데, 이때 클래스의 필드를 지정할 경우, this 키워드를 사용하여 생성자의 매개변수와 구분한다.

 13

 상속이 무엇인가?

 

 14

 캡슐화가 무엇인가? 


표 1. 한글 버전

 


 no

 Question 

 Answer

 1

 Object-oriented programming uses classes and objects.  What are classes and what are objects?  What is the relationship between classes and objects

 A Class is an defined object and used to create an object.  A Class is just used to create object, not object itself.  An object is actually there, and it has the properties and functions of a class.

 For example, If there is a class called "People", "Namgue" created by the People class becomes an object.

 2

 What are instance variable and instance method

 Instance variable and method are variable in the class that are not static.  when Instance object is created, Instance variable and method are created.  when the object is released, Instance variable and method are disappeared.

 3

 Explain carefully what null means in Java, and why this special value is necessary?

 If you declare a variable that has an object type, and you do not create an object, the variable has a NULL value. when the object is created, the variable has the address of object.  NULL means that the variable has or does not have the address of the object.

 4

 What is a constructor?  What is the purpose of a constructor in a class?

 called only when creating an object, initializing the field.  Constructor must use same name as the class, sometimes Constructor can have multiple parameters.

 5

 Suppose that kumquat is the name of a class and that fruit is a variable of type Kumquat.  What is the meaning of the statement "fruit = new Kumquat();"?  That is, what does the computer do when it executes this statement?

 Creating object named "fruit" has "Kumquat" class type.  when "fruit = new Kumquat();" statement is executed, The resources of Kumquat class stored in the Method Area are copied to  the heap memory.  The reference address of the copied resources initialize to the variable fruit.

 6

 What is meant by the terms instance variable and instance method?

 Instance variable and method represent character and function of an object.  That is a feature and function dependent on the object, and It can not be used generally.

 7

 Explain what is meant by the terms subclass and superclass

 In Object-Oriented-Progamming, The elements of declared class can be inherited to another class.  The Class that inherited another class can modify and append inherited class's elements.  The inherited Class is called subclass, and the inheriting class called a superclass.  For example,

 

public class Class1 extends Class2{ ~ }

 

 In above state, Class1 is subclass, Class2 is Class1's super class.

 8

 Explain why the class Player that is defined in the previous question has an instance method named toString(), even though no definition of this method appears in the definition of the class.

 The top class in Java is Object class.  basically, All classes inherit from the Object class that defines toString.  therefore, toString() can be used in any class that do not defines toString.

 9

 Explain the term polymorphism.

 The polymorphism consist of the two elements that are overloading and overriding.

 

 Overloading is that A class has multiple definition of a constructors or methods with the same name.  When calling a constructor or method, overloading makes the type or number of parameters variable.  For example, If you want to get a string type parameter as an integer, add a constructor or method that takes an integer type parameter.     

 

 Overriding occurs in inheritance structure.  When you inherit a class, you override the method and redefine the method. At this time, The implementation of the method defined by the superclass is ignored by the subclass and can be redefined.  Therefore, The methods defined by superclass or subclass have same name, but method's contents are different.


 10

 Java uses "garbage collection" for memory management.  Explain what is meant here by garbage collection.  What is the alternative to garbage collection?

 When the object is created, Object's memory is allocated in heap memory. If the object be using, The memory for the object resides in the heap.  when the object is finished, the object's memory allocation must be released.  If you leave memory allocation, memory leaks will occur, memory management problems will occur, and normal operation will be impossible.  To prevent this situation, Java supports a garbage collector that automatically frees memory when the execution domain ends.  if Java does not support garbage collector, As in c++, you have to manually release the memory using the close() function.

 11

 What is an abstract class, and how can you recognize an abstract class in Java.

 An abstract class is a class that contains one or more abstract methods.  An abstract method is a method that has an abstract keyword and has only a declaration part.  Because an abstract class is an incomplete class, it must inherit or define an abstract method as an anonymous class, otherwise the object can't be created.  The way to recognize abstract classes in Java is to check that abstract keyword is in declaration part or abstract method is in the class.

 12

 What is this?

 this is used to point to the class itself in the class.  Normally, parameter of constructor and field name are same in class, In this case, specifying a field for a class, use the this keyword to distinguish it from the constructor's parameters. 

 13

 What is Inheritance?

 

 14

 What is Encapsulation?

 

표 2. English version

 


 질문을 보고 제가 임의로 작성한 답변입니다.  정확한 답변은 레퍼런스의 문제들을 참조하면 됩니다.  14가지 질문에 답해본다면, 5번 질문을 통하여 new 키워드에 대한 이해를, 그 외 문항들로 자바, 나아가서 oop에 관한 배경지식을 얻을 수 있을 듯 합니다.



Unit 2.docx



reference


1. http://math.hws.edu/javanotes/c5/quiz.html

'MOOC > CS102 Introduction of CS2' 카테고리의 다른 글

Unit 5. Recursion 中  (0) 2016.12.30
Unit 3. Assessment Review  (0) 2016.12.30
Unit 3. C++ STL  (0) 2016.12.28
Unit 3. STL Main Design Idea  (0) 2016.12.28
Unit 2. Assessment Review  (0) 2016.12.19