20
1 Chapter 9 Abstract Class & Interface Oum Saokosal, Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail: [email protected]

Chapter 9 Abstract Class

  • View
    4.591

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 9 Abstract ClassTaught by Oum Saokosal, Head of Information Technology, National Polytechnic Institute of Cambodia

Citation preview

Page 1: Chapter 9 Abstract Class

1

Chapter 9Abstract Class & Interface

Oum Saokosal, Head of IT Department

National Polytechnic Institute of Cambodia

Tel: (855)-12-417214

E-mail: [email protected]

Page 2: Chapter 9 Abstract Class

2

Abstract Class

Page 3: Chapter 9 Abstract Class

3

Abstract Class

• Introduction

• What is abstract class?

• How to make a class to be abstract?

• How to use abstract class?

• Importance of abstract class

Page 4: Chapter 9 Abstract Class

4

Introduction (1)

Today’s class is about abstract class. It sounds to me it make no sense. Do you know something about it?

Well. I know it, but you know..., I‘ve never understood it until I met some problem, then I realized I needed abstract class and I knew it.

I will let you know the problem.

Page 5: Chapter 9 Abstract Class

5

Introduction (2)

CB: So what is your problem?

SR: Ok! First I assume we have three 3 classes like this.

Shape

-color:String

+Shape()+Shape(color)+isFilled():boolean+setFilled(filled):void+getArea():double+getParimeter():double

Circle

-radius:double

+Circle()+Circle(radius:double)+getRadius():double+setRadius(radius):void+getArea():double+getPerimeter():double

Rectangle-width,height:double

+Rectangle()+Rectangle(width,height)+getArea():double+getPerimeter():double

Page 6: Chapter 9 Abstract Class

6

Introduction (3)

CB: I’ve got it. These classes we have met so far.

SR: That’s right. Let’s see the code of Shape:

public class Shape {public Shape(){}public double getArea(){

return 0.0; }

public double getPerimeter(){ return 0.0;

}}

Page 7: Chapter 9 Abstract Class

7

Introduction (4)

SR: We can see that in Shape class, the two methods return zero. It’s not so useful here. public double getArea(){ return 0.0;}public double getPerimeter(){ return 0.0;}

CB: Why do you say that?

SR: You can see that we cannot do anything with zero.

CB: I guess not. I guess these two methods are not important here but later these are for its subclasses.

Page 8: Chapter 9 Abstract Class

8

Introduction (5)SR: Yes you’re right. Actually, these methods was really

designed not for itself but for its children (subclasses).

SR: Here is some codes:

public class Circle extends Shape{ private double radius; public Circle(double radius){ this.radius = radius; } @Override public double getArea(){ return radius*radius*Math.PI; } @Override public double getPerimeter(){ return 2*radius*Math.PI; }}

Page 9: Chapter 9 Abstract Class

9

Introduction (6)

CB: I think we all know it. It should not be a problem like you said.

SR: OK. Let’s me finish my story.

CB: OK. Go on...

SR: Can you imagine if you use polymorphism like this:

Shape shape = new Circle();

shape.getArea();

CB: Because in Circle we overrides the getArea() method, then it calls getArea() in Circle.

Page 10: Chapter 9 Abstract Class

10

Introduction (7)

SR: What about if we don’t override getArea() in Circle?

public class Circle extends Shape{ private double radius; public Circle(double radius){ this.radius = radius; } }

CB: So...

SR: And what will we get when using polymorphism:

Shape shape = new Circle();

shape.getArea();

Page 11: Chapter 9 Abstract Class

11

Introduction (8)

CB: getArea() is from Shape because Circle has no getArea(). It should not be a problem.

SR: Do you remember what the value that getArea() return. Here is the code:

public double getArea(){

return 0.0;

}

CB: Yes. it returns 0.

SR: So can you see the problem.

CB: Yehh... A bit. Can you tell me more?

Page 12: Chapter 9 Abstract Class

12

Introduction (9)

SR: You know, in my experience, sometimes we expected to get a right calculation from subclass just like this:

public static void main(String[] args){

showArea(new Circle());

}

public static showArea(Shape s){

System.out.print(s.getArea());

}

SR: But I never get it right because I forgot to override in my subclass, in this example, Circle class.

Page 13: Chapter 9 Abstract Class

13

Introduction (9)

CB: Oh I see.

SR: You know what? To ensure that which methods I have to override in subclass, I have to reopen the superclass and find out the methods to be overridden.

CB: Oh really?

SR: Yes. Also sometimes I cannot find which methods in superclass that I have to override.

CB: Hmmm...

SR: And even more seriously, usually we have to use someone’s classes or use Java API library. So can you imagine which method should be overridden?

CB: I can tell if I can see someone’s codes. I don’t know?

Page 14: Chapter 9 Abstract Class

14

Introduction (10)

SR: You see? This is the point. If you want our subclass have which methods to be overridden, we have to make that methods

and the superclass to be abstract.

CB: What? Abstract?

SR: Yehh abstract.

CB: So what is abstract class?

SR: Let’s see it at the next slide.

Page 15: Chapter 9 Abstract Class

15

What is abstract class?

• Abstract class is just like other class, but it marks with abstract keyword.

• In abstract class, methods that we want to be overridden in its subclass must mark with abstract too. Moreover, those methods must not contain any code.

• However, abstract class can have normal properties, constructors, and other methods.

Page 16: Chapter 9 Abstract Class

16

How to make a class to be abstract? (1)

Here is an example:

public abstract class Shape { private String color; public Shape(){}

public String getColor() { return color; } public void setColor(String color) { this.color = color; } public abstract double getArea(); public abstract double getPerimeter();}

Page 17: Chapter 9 Abstract Class

17

How to make a class to be abstract? (2)

• And then in subclass, the method that mark with abstract keyword, it will automatically request to be override without any excuse.

public class Circle extends Shape{ private double radius public Circle(){} public Circle(double radius){ this.radius = radius; } @Override public double getArea(){ return radius*radius*Math.PI; } @Override public double getPerimeter(){ return 2*radius*Math.PI; } }

Page 18: Chapter 9 Abstract Class

18

How to use abstract class? (1)

• You can use an abstract class by inheriting it using extends keyword.

public class Circle extends Shape {

}

• Abstract class can also be a type.

Shape sh;//Shape is a type of sh variable

• Because abstract class can also be a type, we can use polymorphism as well.

Shape sh = new Circle();

sh.getArea();

Page 19: Chapter 9 Abstract Class

19

How to use abstract class? (2)

• You CANNOT create instances of abstract classes using the new operator.

Shape shape = new Shape();// Compile Error

• We can make an abstract class by not making any method abstract also. There is no any error.

public abstract class Shape {

public String getColor(){

return “”;

}

}

Page 20: Chapter 9 Abstract Class

20

Importance of abstract class

• Abstract class is always a superclass. It means when you make an abstract class, you have to think that the class must be a superclass later.

• Abstract class is the way to guarantee that its closed subclasses MUST override abstract methods.

• The only reason that we have to make abstract class is because of polymorphism.

• It makes no sense if we make abstract class, but we don’t use any polymorphism.