donaricano-btn

Abstract class

- A class that is declared with abstract keyword, is known as abstract class

- It can have abstract and non-abstract methods


1. Abstraction in java

- Abstraction is a process of hiding the implementation details 

- It showing only functionality to the user

- Abstraction lets you focus on what the object does instead of how it does it


1) Ways to achieve Abstraction

- Abstract class

- Interface

2. Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
abstract class Shape{
    abstract void draw();
}
 
class Rectangle extends Shape{
    void draw(){
        System.out.println("Drawing rec");
    }
}
class Circle extends Shape{
    void draw(){
        System.out.println("draw circle");
    }
}
 
public class Test{
    public static void main(String[]args){
         
        Shape s = new Circle();
        s.draw();
    }
}

3. Abstract class having constructor, data member, methods

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
abstract class Bike{
    Bike(){
        System.out.println("Bike is created");
    }
    abstract void run();
    void changeGear(){
        System.out.println("gear changed");
    }
}
 
class Honda extends Bike{
    void run(){
        System.out.println("running");
    }
}
 
public class Test{
    public static void main(String[]args){
         
        Bike obj = new Honda();
        obj.run();
        obj.changeGear();
    }
}

- If there is any abstract method in a class, that class must be abstract

- If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstraction



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

[Java] Package  (0) 2016.09.12
[Java] Interface  (0) 2016.09.12
[Java] Instanceof  (0) 2016.09.09
[Java] Static Binding and Dynamic Binding  (0) 2016.09.08
[Java] Runtime Polymorphism  (0) 2016.09.08
블로그 이미지

리딩리드

,