SlideShare ist ein Scribd-Unternehmen logo
1 von 20
CHAPTER 9
ABSTRACT CLASS & INTERFACE
Oum Saokosal, Chief of Computer Science
National Polytechnic Institute of Cambodia
Tel: (855)-12-252-752
E-mail: oum_saokosal@yahoo.com
1
ABSTRACT CLASS
2
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 PolymorphismMahmoud Alfarra
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with JavaOum Saokosal
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorialBui Kiet
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية Mahmoud Alfarra
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceOUM SAOKOSAL
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Java Basics
Java BasicsJava Basics
Java BasicsF K
 

Was ist angesagt? (20)

Java basic
Java basicJava basic
Java basic
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Java unit2
Java unit2Java unit2
Java unit2
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Inheritance
InheritanceInheritance
Inheritance
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية البرمجة الهدفية بلغة جافا - مفاهيم أساسية
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & InterfaceJava OOP Programming language (Part 6) - Abstract Class & Interface
Java OOP Programming language (Part 6) - Abstract Class & Interface
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
Chap11
Chap11Chap11
Chap11
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Java Basics
Java BasicsJava Basics
Java Basics
 

Andere mochten auch

11.1 Android with HTML
11.1 Android with HTML11.1 Android with HTML
11.1 Android with HTMLOum Saokosal
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android VideoOum Saokosal
 
Database Concept - ERD Mapping to MS Access
Database Concept - ERD Mapping to MS AccessDatabase Concept - ERD Mapping to MS Access
Database Concept - ERD Mapping to MS AccessOum Saokosal
 
09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)Oum Saokosal
 
Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Oum Saokosal
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFOum Saokosal
 
Android - Introduction
Android - IntroductionAndroid - Introduction
Android - IntroductionOum Saokosal
 
10.1. Android Audio
10.1. Android Audio10.1. Android Audio
10.1. Android AudioOum Saokosal
 
07.4. Android Basic Simple Browser (WebView)
07.4. Android Basic Simple Browser (WebView)07.4. Android Basic Simple Browser (WebView)
07.4. Android Basic Simple Browser (WebView)Oum Saokosal
 
06. Android Basic Widget and Container
06. Android Basic Widget and Container06. Android Basic Widget and Container
06. Android Basic Widget and ContainerOum Saokosal
 
07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto CompleteOum Saokosal
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in androidOum Saokosal
 
08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)Oum Saokosal
 
10.2 Android Audio with SD Card
10.2 Android Audio with SD Card10.2 Android Audio with SD Card
10.2 Android Audio with SD CardOum Saokosal
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even HandlingOum Saokosal
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google MapOum Saokosal
 

Andere mochten auch (20)

11.1 Android with HTML
11.1 Android with HTML11.1 Android with HTML
11.1 Android with HTML
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
 
Database Concept - ERD Mapping to MS Access
Database Concept - ERD Mapping to MS AccessDatabase Concept - ERD Mapping to MS Access
Database Concept - ERD Mapping to MS Access
 
09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)
 
Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)Database Concept - Normalization (1NF, 2NF, 3NF)
Database Concept - Normalization (1NF, 2NF, 3NF)
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 
Overloading
Overloading Overloading
Overloading
 
Android - Introduction
Android - IntroductionAndroid - Introduction
Android - Introduction
 
program on Function overloading in java
program on  Function overloading in javaprogram on  Function overloading in java
program on Function overloading in java
 
Constructor Overloading in java
Constructor Overloading in javaConstructor Overloading in java
Constructor Overloading in java
 
10.1. Android Audio
10.1. Android Audio10.1. Android Audio
10.1. Android Audio
 
07.4. Android Basic Simple Browser (WebView)
07.4. Android Basic Simple Browser (WebView)07.4. Android Basic Simple Browser (WebView)
07.4. Android Basic Simple Browser (WebView)
 
06. Android Basic Widget and Container
06. Android Basic Widget and Container06. Android Basic Widget and Container
06. Android Basic Widget and Container
 
07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete07.3. Android Alert message, List, Dropdown, and Auto Complete
07.3. Android Alert message, List, Dropdown, and Auto Complete
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in android
 
08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)08.1. Android How to Use Intent (explicit)
08.1. Android How to Use Intent (explicit)
 
10.2 Android Audio with SD Card
10.2 Android Audio with SD Card10.2 Android Audio with SD Card
10.2 Android Audio with SD Card
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even Handling
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google Map
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Ähnlich wie Java Programming - Introduction to Abstract Class

Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 InheritanceOUM SAOKOSAL
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Umar Farooq
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesMuhammad Raza
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceOUM SAOKOSAL
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.pptMorgan309846
 
Spring tutorial - dependency injection
Spring tutorial - dependency injectionSpring tutorial - dependency injection
Spring tutorial - dependency injectionAnup Singh
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptJayanthiM15
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptxHailsh
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#Sireesh K
 
Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxMahmoodAlashqar
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196Mahmoud Samir Fayed
 

Ähnlich wie Java Programming - Introduction to Abstract Class (20)

Java assgnmt2.
Java assgnmt2.Java assgnmt2.
Java assgnmt2.
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
 
L4
L4L4
L4
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
The maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID PrinciplesThe maze of Design Patterns & SOLID Principles
The maze of Design Patterns & SOLID Principles
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
 
Spring tutorial - dependency injection
Spring tutorial - dependency injectionSpring tutorial - dependency injection
Spring tutorial - dependency injection
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Inheritance
InheritanceInheritance
Inheritance
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
8abstact class in c#
8abstact class in c#8abstact class in c#
8abstact class in c#
 
Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptx
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196
 

Kürzlich hochgeladen

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 

Kürzlich hochgeladen (20)

Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Java Programming - Introduction to Abstract Class

  • 1. CHAPTER 9 ABSTRACT CLASS & INTERFACE Oum Saokosal, Chief of Computer Science National Polytechnic Institute of Cambodia Tel: (855)-12-252-752 E-mail: oum_saokosal@yahoo.com 1
  • 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 3
  • 4. 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.
  • 5. 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
  • 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; } } 6
  • 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. 7
  • 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; } } 8
  • 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. 9
  • 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(); 10
  • 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? 11
  • 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. 12
  • 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? 13
  • 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. 14
  • 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. 15
  • 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(); } 16
  • 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; } } 17
  • 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(); 18
  • 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 “”; } } 19
  • 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. 20