'Back-End/Java_1'에 해당되는 글 48건

donaricano-btn

 Super keyword

- The super keyword is a reference variable that is used to refer immediate parent class object


1. Usage

- super is used to refer immediate parent class instance variable

- super() is used to invoke immediate parent class constructor

- super is used to invoke immediate parent class method


2. parent class instance variable


- 50


3. To invoke parent class constructor

 

- vehicle, bike

- super() is added in each class constructor automatically by compiler

- If In the above example Bike doesn't have a super(), It will print out same result


4. To invoke parent class method

- It should be used in case subclass contains the same method as parent class

 

- In case there is no method in subclass as parent, there is no need to use super.

 



'Back-End > Java_1' 카테고리의 다른 글

[Java] Final keyword  (0) 2016.09.07
[Java] Instance initializer block  (0) 2016.09.07
[Java] Covariant Return type  (0) 2016.08.24
[Java] Overloading vs Overriding  (0) 2016.08.24
[Java] Method Overriding  (0) 2016.08.24
블로그 이미지

리딩리드

,
donaricano-btn

 Covariant Return type


- The covariant return type specifies that the return type may vary in the same direction as the subclass

- Since Java5, it is possible to override method by changing the return type if subclass overrides and method whose return type is Non-primitive


1. Example


- The return type of the get() of A class is A, but the return type of the get() of Test class is Test

- Both methods have different return type but it is overriding

'Back-End > Java_1' 카테고리의 다른 글

[Java] Instance initializer block  (0) 2016.09.07
[Java] Super keyword  (0) 2016.08.25
[Java] Overloading vs Overriding  (0) 2016.08.24
[Java] Method Overriding  (0) 2016.08.24
[Java] Aggregation in java  (0) 2016.08.23
블로그 이미지

리딩리드

,
donaricano-btn

Overloading vs Overriding


 No

Overloading 

Overriding 

 1

 To increase the readability of the program

To provide the specific implementation of the method 

 2

Overloading it performed within class 

Overriding occurs in two classes 

 3

parameter must be different 

parameter must be same 

compile time polymorphism 

run time polymorphism 

Return type can be same or different, but you must have to change the parameter 

Return type must be same or covarient 


'Back-End > Java_1' 카테고리의 다른 글

[Java] Super keyword  (0) 2016.08.25
[Java] Covariant Return type  (0) 2016.08.24
[Java] Method Overriding  (0) 2016.08.24
[Java] Aggregation in java  (0) 2016.08.23
[Java] Inheritance in Java  (0) 2016.08.23
블로그 이미지

리딩리드

,
donaricano-btn

Method Overriding


- If subclass has the same method as declared in the parent class, it is known as method overriding


1. Usage

- Method overriding is used to provide specific implementation of a method that is already provided by its super class

- Method overriding is used for runtime polymorphism


2. Rule

- method must have same name as in the parent

- method must have same parameter


3. Problem without method overriding


- Problem is that I have to provide a specific implementation of run() method


4. Example

 


5. Question

1) Can we override static method?

- No, static method can't be overriden. It can be proved by runtime polymorphism

2) Why we can't overriden static method?

- Static method is bound with class whereas instance method is bound with object

- Static belongs to class area

- instance belongs to heap area



'Back-End > Java_1' 카테고리의 다른 글

[Java] Covariant Return type  (0) 2016.08.24
[Java] Overloading vs Overriding  (0) 2016.08.24
[Java] Aggregation in java  (0) 2016.08.23
[Java] Inheritance in Java  (0) 2016.08.23
[Java] Input by Console  (0) 2016.08.23
블로그 이미지

리딩리드

,