SlideShare a Scribd company logo
Abstract class in Java
Mrs.S.KAVITHA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE
SRI SARADA NIKETAN COLLEGE OF SCIENCE
FOR WOMEN,KARUR
• A class which is declared with the abstract keyword
is known as an abstract class in Java. It can have
abstract and non-abstract methods (method with
the body).
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• it shows only essential things to the user and hides
the internal details,
• for example, sending SMS where you type the text
and send the message. You don't know the internal
processing about the message delivery.
Ways to achieve Abstraction
There are two ways to achieve abstraction in
java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract class in Java
• A class which is declared as abstract is known
as an abstract class. It can have abstract and
non-abstract methods. It needs to be
extended and its method implemented. It
cannot be instantiated.
• An abstract class must be declared with an
abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the
subclass not to change the body of the method.
Example of abstract class
abstract class A{}
Abstract Method in Java
• A method which is declared as abstract and
does not have implementation is known as an
abstract method.
Example of abstract method
• abstract void printStatus();//
no method body and abstract
Example of Abstract class that has an abstract method
In this example, Bike is an abstract class that contains only one abstract
method run. Its implementation is provided by the Honda class.
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Output:
running safely
• In this example, Shape is the abstract class,
and its implementation is provided by the
Rectangle and Circle classes.
• In this example, if you create the instance of
Rectangle class, draw() method of Rectangle
class will be invoked.
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//
In a real scenario, object is provided through method, e.g., getShape() method
s.draw();
}
}
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Rate of Interest is: 7 % Rate of Interest is: 8 %

More Related Content

PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPTX
BCA Abstraction.pptx
PPT
Abstract class in java
PPT
A1869984431_21789_28_2018_Abstract Class.ppt
PPTX
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
PPTX
Abstract class
PDF
Abstraction in Java: Abstract class and Interfaces
PPTX
Abstraction in java.pptx
ABSTRACT CLASSES AND INTERFACES.ppt
BCA Abstraction.pptx
Abstract class in java
A1869984431_21789_28_2018_Abstract Class.ppt
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
Abstract class
Abstraction in Java: Abstract class and Interfaces
Abstraction in java.pptx

Similar to A class which is declared with the abstract keyword is known as an abstract class in Java (20)

PPTX
8abstact class in c#
PPTX
OOP with Java - Abstract Classes and Interfaces
PDF
Exception handling and packages.pdf
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPSX
OOP with Java - Abstract Classes and Interfaces
PPTX
abstract,final,interface (1).pptx upload
PPTX
More oop in java
DOCX
Core java notes with examples
PPT
12.2 Abstract class and Interface.ppt
PPTX
Abstract class and interface
PPTX
Abstraction in java [abstract classes and Interfaces
PDF
Java abstract Keyword.pdf
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
Abstract classes & interfaces
PPTX
Modules 333333333³3444444444444444444.pptx
PDF
Abstract classes and Methods in java
PPTX
Abstract Class and Interface for Java Intoductory course.pptx
PPTX
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
PPTX
Java 6.pptx
8abstact class in c#
OOP with Java - Abstract Classes and Interfaces
Exception handling and packages.pdf
Session 10 - OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
abstract,final,interface (1).pptx upload
More oop in java
Core java notes with examples
12.2 Abstract class and Interface.ppt
Abstract class and interface
Abstraction in java [abstract classes and Interfaces
Java abstract Keyword.pdf
Abstract Class & Abstract Method in Core Java
Abstract classes & interfaces
Modules 333333333³3444444444444444444.pptx
Abstract classes and Methods in java
Abstract Class and Interface for Java Intoductory course.pptx
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Java 6.pptx
Ad

More from Kavitha S (9)

PPTX
data link layer is the protocol layer in a program that handles the moving of...
PPTX
Java provides statements that can be used to control the flow of Java code
PPTX
When a break statement is encountered inside a loop, the loop is immediately ...
PPTX
a variable in Java must be a specified data type
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
The final keyword in java is used to restrict the user.
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
PPTX
How to create a two-dimensional array in java
data link layer is the protocol layer in a program that handles the moving of...
Java provides statements that can be used to control the flow of Java code
When a break statement is encountered inside a loop, the loop is immediately ...
a variable in Java must be a specified data type
Inheritance in Java is a mechanism in which one object acquires all the prope...
The final keyword in java is used to restrict the user.
A constructor in Java is a special method that is used to initialize objects
the array, which stores a fixed-size sequential collection of elements of the...
How to create a two-dimensional array in java
Ad

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Computing-Curriculum for Schools in Ghana
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
PDF
RMMM.pdf make it easy to upload and study
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Basic Mud Logging Guide for educational purpose
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
STATICS OF THE RIGID BODIES Hibbelers.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .
RMMM.pdf make it easy to upload and study
Sports Quiz easy sports quiz sports quiz
Microbial disease of the cardiovascular and lymphatic systems
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Basic Mud Logging Guide for educational purpose
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

A class which is declared with the abstract keyword is known as an abstract class in Java

  • 1. Abstract class in Java Mrs.S.KAVITHA ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENCE SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR WOMEN,KARUR
  • 2. • A class which is declared with the abstract keyword is known as an abstract class in Java. It can have abstract and non-abstract methods (method with the body). • Abstraction is a process of hiding the implementation details and showing only functionality to the user. • it shows only essential things to the user and hides the internal details, • for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.
  • 3. Ways to achieve Abstraction There are two ways to achieve abstraction in java • Abstract class (0 to 100%) • Interface (100%)
  • 4. Abstract class in Java • A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
  • 5. • An abstract class must be declared with an abstract keyword. • It can have abstract and non-abstract methods. • It cannot be instantiated. • It can have constructors and static methods also. • It can have final methods which will force the subclass not to change the body of the method.
  • 6. Example of abstract class abstract class A{}
  • 7. Abstract Method in Java • A method which is declared as abstract and does not have implementation is known as an abstract method. Example of abstract method • abstract void printStatus();// no method body and abstract
  • 8. Example of Abstract class that has an abstract method In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class. abstract class Bike{ abstract void run(); } class Honda4 extends Bike{ void run(){System.out.println("running safely");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); } } Output: running safely
  • 9. • In this example, Shape is the abstract class, and its implementation is provided by the Rectangle and Circle classes. • In this example, if you create the instance of Rectangle class, draw() method of Rectangle class will be invoked.
  • 10. abstract class Shape{ abstract void draw(); } //In real scenario, implementation is provided by others i.e. unknown by end user class Rectangle extends Shape{ void draw(){System.out.println("drawing rectangle");} } class Circle1 extends Shape{ void draw(){System.out.println("drawing circle");} } //In real scenario, method is called by programmer or user class TestAbstraction1{ public static void main(String args[]){ Shape s=new Circle1();// In a real scenario, object is provided through method, e.g., getShape() method s.draw(); } }
  • 11. abstract class Bank{ abstract int getRateOfInterest(); } class SBI extends Bank{ int getRateOfInterest(){return 7;} } class PNB extends Bank{ int getRateOfInterest(){return 8;} } class TestBank{ public static void main(String args[]){ Bank b; b=new SBI(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); b=new PNB(); System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %"); }} Rate of Interest is: 7 % Rate of Interest is: 8 %