Chapter 9 Abstract Class


Published on

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

Published in: Technology, News & Politics
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Chapter 9 Abstract Class

  1. 1. Chapter 9 Abstract Class & Interface Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail:
  2. 2. Abstract Class
  3. 3. Abstract Class <ul><li>Introduction </li></ul><ul><li>What is abstract class? </li></ul><ul><li>How to make a class to be abstract? </li></ul><ul><li>How to use abstract class? </li></ul><ul><li>Importance of abstract class </li></ul>
  4. 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.
  5. 5. Introduction (2) <ul><li>CB: So what is your problem? </li></ul><ul><li>SR: Ok! First I assume we have three 3 classes like this. </li></ul>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
  6. 6. Introduction (3) <ul><li>CB: I’ve got it. These classes we have met so far. </li></ul><ul><li>SR: That’s right. Let’s see the code of Shape : </li></ul><ul><ul><li>public class Shape { </li></ul></ul><ul><ul><li>public Shape(){} </li></ul></ul><ul><ul><li>public double getArea(){ </li></ul></ul><ul><ul><li>return 0.0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public double getPerimeter(){ </li></ul></ul><ul><ul><li>return 0.0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  7. 7. Introduction (4) <ul><li>SR: We can see that in Shape class, the two methods return zero. It’s not so useful here. </li></ul><ul><ul><li>public double getArea(){ </li></ul></ul><ul><ul><li>return 0.0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public double getPerimeter(){ </li></ul></ul><ul><ul><li>return 0.0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>CB: Why do you say that? </li></ul><ul><li>SR: You can see that we cannot do anything with zero. </li></ul><ul><li>CB: I guess not. I guess these two methods are not important here but later these are for its subclasses. </li></ul>
  8. 8. Introduction (5) <ul><li>SR: Yes you’re right. Actually, these methods was really designed not for itself but for its children (subclasses). </li></ul><ul><li>SR: Here is some codes: </li></ul><ul><ul><li>public class Circle extends Shape{ </li></ul></ul><ul><ul><li>private double radius; </li></ul></ul><ul><ul><li>public Circle(double radius){ </li></ul></ul><ul><ul><li>this.radius = radius; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public double getArea(){ </li></ul></ul><ul><ul><li>return radius*radius*Math.PI; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public double getPerimeter(){ </li></ul></ul><ul><ul><li>return 2*radius*Math.PI; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  9. 9. Introduction (6) <ul><li>CB: I think we all know it. It should not be a problem like you said. </li></ul><ul><li>SR: OK. Let’s me finish my story. </li></ul><ul><li>CB: OK. Go on... </li></ul><ul><li>SR: Can you imagine if you use polymorphism like this: </li></ul><ul><li>Shape shape = new Circle(); </li></ul><ul><li>shape.getArea(); </li></ul><ul><li>CB: Because in Circle we overrides the getArea() method, then it calls getArea() in Circle . </li></ul>
  10. 10. Introduction (7) <ul><li>SR: What about if we don’t override getArea() in Circle? </li></ul><ul><ul><li>public class Circle extends Shape{ </li></ul></ul><ul><ul><li>private double radius; </li></ul></ul><ul><ul><li>public Circle(double radius){ </li></ul></ul><ul><ul><li>this.radius = radius; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>CB: So... </li></ul><ul><li>SR: And what will we get when using polymorphism: </li></ul><ul><li>Shape shape = new Circle(); </li></ul><ul><li>shape .getArea() ; </li></ul>
  11. 11. Introduction (8) <ul><li>CB: getArea() is from Shape because Circle has no getArea() . It should not be a problem. </li></ul><ul><li>SR: Do you remember what the value that getArea() return. Here is the code: </li></ul><ul><ul><li>public double getArea(){ </li></ul></ul><ul><ul><li>return 0.0; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>CB: Yes. it returns 0. </li></ul><ul><li>SR: So can you see the problem. </li></ul><ul><li>CB: Yehh... A bit. Can you tell me more? </li></ul>
  12. 12. Introduction (9) <ul><li>SR: You know, in my experience, sometimes we expected to get a right calculation from subclass just like this: </li></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>showArea(new Circle()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public static showArea(Shape s){ </li></ul></ul><ul><ul><li>System.out.print( s.getArea() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>SR: But I never get it right because I forgot to override in my subclass, in this example, Circle class. </li></ul>
  13. 13. Introduction (9) <ul><li>CB: Oh I see. </li></ul><ul><li>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. </li></ul><ul><li>CB: Oh really? </li></ul><ul><li>SR: Yes. Also sometimes I cannot find which methods in superclass that I have to override. </li></ul><ul><li>CB: Hmmm... </li></ul><ul><li>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? </li></ul><ul><li>CB: I can tell if I can see someone’s codes. I don’t know? </li></ul>
  14. 14. Introduction (10) <ul><li>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 . </li></ul><ul><li>CB: What? Abstract? </li></ul><ul><li>SR: Yehh abstract. </li></ul><ul><li>CB: So what is abstract class? </li></ul><ul><li>SR: Let’s see it at the next slide. </li></ul>
  15. 15. What is abstract class? <ul><li>Abstract class is just like other class, but it marks with abstract keyword. </li></ul><ul><li>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. </li></ul><ul><li>However, abstract class can have normal properties, constructors, and other methods. </li></ul>
  16. 16. How to make a class to be abstract? (1) <ul><li>Here is an example: </li></ul><ul><ul><li>public abstract class Shape { </li></ul></ul><ul><ul><li>private String color; </li></ul></ul><ul><ul><li>public Shape(){} </li></ul></ul><ul><ul><li>public String getColor() { </li></ul></ul><ul><ul><li>return color; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public void setColor(String color) { </li></ul></ul><ul><ul><li>this.color = color; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public abstract double getArea(); </li></ul></ul><ul><ul><li>public abstract double getPerimeter(); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  17. 17. How to make a class to be abstract? (2) <ul><li>And then in subclass, the method that mark with abstract keyword, it will automatically request to be override without any excuse. </li></ul><ul><ul><li>public class Circle extends Shape{ </li></ul></ul><ul><ul><li>private double radius </li></ul></ul><ul><ul><li>public Circle(){} </li></ul></ul><ul><ul><li>public Circle(double radius){ </li></ul></ul><ul><ul><li>this.radius = radius; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public double getArea(){ </li></ul></ul><ul><ul><li>return radius*radius*Math.PI; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public double getPerimeter(){ </li></ul></ul><ul><ul><li>return 2*radius*Math.PI; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  18. 18. How to use abstract class? (1) <ul><li>You can use an abstract class by inheriting it using extends keyword. </li></ul><ul><li>public class Circle extends Shape { </li></ul><ul><li>} </li></ul><ul><li>Abstract class can also be a type. </li></ul><ul><li>Shape sh; //Shape is a type of sh variable </li></ul><ul><li>Because abstract class can also be a type, we can use polymorphism as well. </li></ul><ul><li>Shape sh = new Circle(); </li></ul><ul><li>sh.getArea(); </li></ul>
  19. 19. How to use abstract class? (2) <ul><li>You CANNOT create instances of abstract classes using the new operator. </li></ul><ul><li>Shape shape = new Shape();// Compile Error </li></ul><ul><li>We can make an abstract class by not making any method abstract also. There is no any error. </li></ul><ul><ul><li>public abstract class Shape { </li></ul></ul><ul><ul><li>public String getColor(){ </li></ul></ul><ul><ul><li>return “”; </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Importance of abstract class <ul><li>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. </li></ul><ul><li>Abstract class is the way to guarantee that its closed subclasses MUST override abstract methods. </li></ul><ul><li>The only reason that we have to make abstract class is because of polymorphism . </li></ul><ul><li>It makes no sense if we make abstract class, but we don’t use any polymorphism. </li></ul>