SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

Java basic
Java basicJava basic
Java basic
Sonam Sharma
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham 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 examples
Sunil Kumar Gunasekaran
 
Java unit2
Java unit2Java unit2
Java unit2
Abhishek Khune
 
Java Basics
Java BasicsJava Basics
Java Basics
Rajkattamuri
 
Inheritance
InheritanceInheritance
Inheritance
Sapna Sharma
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
shathika
 
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
CPD INDIA
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
Jussi Pohjolainen
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
Oum Saokosal
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
Bui 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 & Interface
OUM SAOKOSAL
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
Mahmoud Alfarra
 
Chap11
Chap11Chap11
Chap11
Terry Yoast
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Java Basics
Java BasicsJava Basics
Java Basics
F K
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham 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 examples
Sunil Kumar Gunasekaran
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
shathika
 
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
CPD INDIA
 
‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism‫‫Chapter4 Polymorphism
‫‫Chapter4 Polymorphism
Mahmoud Alfarra
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
Oum Saokosal
 
Java basic tutorial
Java basic tutorialJava basic tutorial
Java basic tutorial
Bui 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 & Interface
OUM SAOKOSAL
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Java Basics
Java BasicsJava Basics
Java Basics
F K
 

Viewers also liked (20)

11.1 Android with HTML
11.1 Android with HTML11.1 Android with HTML
11.1 Android with HTML
Oum Saokosal
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
Oum 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 Access
Oum 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, 5NF
Oum Saokosal
 
Overloading
Overloading Overloading
Overloading
gemini1420
 
Android - Introduction
Android - IntroductionAndroid - Introduction
Android - Introduction
Oum Saokosal
 
program on Function overloading in java
program on  Function overloading in javaprogram on  Function overloading in java
program on Function overloading in java
One97 Communications Limited
 
Constructor Overloading in java
Constructor Overloading in javaConstructor Overloading in java
Constructor Overloading in java
One97 Communications Limited
 
10.1. Android Audio
10.1. Android Audio10.1. Android Audio
10.1. Android Audio
Oum 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 Container
Oum 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 Complete
Oum Saokosal
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in android
Oum 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 Card
Oum Saokosal
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even Handling
Oum Saokosal
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google Map
Oum Saokosal
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
11.1 Android with HTML
11.1 Android with HTML11.1 Android with HTML
11.1 Android with HTML
Oum Saokosal
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
Oum 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 Access
Oum 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, 5NF
Oum Saokosal
 
Android - Introduction
Android - IntroductionAndroid - Introduction
Android - Introduction
Oum Saokosal
 
10.1. Android Audio
10.1. Android Audio10.1. Android Audio
10.1. Android Audio
Oum 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 Container
Oum 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 Complete
Oum Saokosal
 
Using intents in android
Using intents in androidUsing intents in android
Using intents in android
Oum 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 Card
Oum Saokosal
 
07.1. Android Even Handling
07.1. Android Even Handling07.1. Android Even Handling
07.1. Android Even Handling
Oum Saokosal
 
12. Android Basic Google Map
12. Android Basic Google Map12. Android Basic Google Map
12. Android Basic Google Map
Oum Saokosal
 
Ad

Similar to Java Programming - Introduction to Abstract Class (20)

Java assgnmt2.
Java assgnmt2.Java assgnmt2.
Java assgnmt2.
Moses Mwebaze
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM 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
 
L4
L4L4
L4
lksoo
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
OUM 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 Interfaces
Ahmed 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 Principles
Muhammad 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) - Inheritance
OUM SAOKOSAL
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
Morgan309846
 
Spring tutorial - dependency injection
Spring tutorial - dependency injectionSpring tutorial - dependency injection
Spring tutorial - dependency injection
Anup Singh
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
ANURAG SINGH
 
Inheritance
InheritanceInheritance
Inheritance
Daman Toor
 
ABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.pptABSTRACT CLASSES AND INTERFACES.ppt
ABSTRACT CLASSES AND INTERFACES.ppt
JayanthiM15
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Bhushan Mulmule
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
Hailsh
 
A class which is declared with the abstract keyword is known as an abstract c...
A class which is declared with the abstract keyword is known as an abstract c...A class which is declared with the abstract keyword is known as an abstract c...
A class which is declared with the abstract keyword is known as an abstract c...
Kavitha S
 
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.pptx
MahmoodAlashqar
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM 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 Interface
OUM 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 Interfaces
Ahmed 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 Principles
Muhammad 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) - Inheritance
OUM SAOKOSAL
 
Java Presentation.ppt
Java Presentation.pptJava Presentation.ppt
Java Presentation.ppt
Morgan309846
 
Spring tutorial - dependency injection
Spring tutorial - dependency injectionSpring tutorial - dependency injection
Spring tutorial - dependency injection
Anup 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.ppt
JayanthiM15
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
Hailsh
 
A class which is declared with the abstract keyword is known as an abstract c...
A class which is declared with the abstract keyword is known as an abstract c...A class which is declared with the abstract keyword is known as an abstract c...
A class which is declared with the abstract keyword is known as an abstract c...
Kavitha S
 
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.pptx
MahmoodAlashqar
 
Ad

Recently uploaded (20)

TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
TV Shows and web-series quiz | QUIZ CLUB OF PSGCAS | 13TH MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptxPests of Rice: Damage, Identification, Life history, and Management.pptx
Pests of Rice: Damage, Identification, Life history, and Management.pptx
Arshad Shaikh
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptxSEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
SEXUALITY , UNWANTED PREGANCY AND SEXUAL ASSAULT .pptx
PoojaSen20
 
How to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 SalesHow to Create Quotation Templates Sequence in Odoo 18 Sales
How to Create Quotation Templates Sequence in Odoo 18 Sales
Celine George
 
Unit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptxUnit 3 Poster Sketches with annotations.pptx
Unit 3 Poster Sketches with annotations.pptx
bobby205207
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
What are the benefits that dance brings?
What are the benefits that dance brings?What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big CycleRay Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
Module 4 Presentation - Enhancing Competencies and Engagement Strategies in Y...
GeorgeDiamandis11
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
Trends Spotting Strategic foresight for tomorrow’s education systems - Debora...
EduSkills OECD
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 

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: [email protected] 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