20
CHAPTER 9 ABSTRACT CLASS & INTERFACE Oum Saokosal, Chief of Computer Science National Polytechnic Institute of Cambodia Tel: (855)-12-252-752 E-mail: [email protected] 1

Java Programming - Introduction to Abstract Class

Embed Size (px)

Citation preview

Page 1: Java Programming - Introduction to Abstract Class

CHAPTER 9ABSTRACT CLASS & INTERFACE

Oum Saokosal, Chief of Computer ScienceNational Polytechnic Institute of CambodiaTel: (855)-12-252-752E-mail: [email protected]

1

Page 2: Java Programming - Introduction to Abstract Class

ABSTRACT CLASS

2

Page 3: Java Programming - Introduction to Abstract Class

Abstract Class• Introduction• What is abstract class?• How to make a class to be abstract?• How to use abstract class? • Importance of abstract class

3

Page 4: Java Programming - Introduction to Abstract Class

Introduction (1)

4

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: Java Programming - Introduction to Abstract Class

Introduction (2)CB: So what is your problem? SR: Ok! First I assume we have three 3 classes like this.

5

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: Java Programming - Introduction to Abstract Class

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;

}}

6

Page 7: Java Programming - Introduction to Abstract Class

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.

7

Page 8: Java Programming - Introduction to Abstract Class

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; }}

8

Page 9: Java Programming - Introduction to Abstract Class

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.

9

Page 10: Java Programming - Introduction to Abstract Class

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();

10

Page 11: Java Programming - Introduction to Abstract Class

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?

11

Page 12: Java Programming - Introduction to Abstract Class

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.

12

Page 13: Java Programming - Introduction to Abstract Class

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?

13

Page 14: Java Programming - Introduction to Abstract Class

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.

14

Page 15: Java Programming - Introduction to Abstract Class

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.

15

Page 16: Java Programming - Introduction to Abstract Class

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();}

16

Page 17: Java Programming - Introduction to Abstract Class

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; } }

17

Page 18: Java Programming - Introduction to Abstract Class

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();

18

Page 19: Java Programming - Introduction to Abstract Class

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 “”;

}}

19

Page 20: Java Programming - Introduction to Abstract Class

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.

20