0% found this document useful (0 votes)
2 views23 pages

Java Abstract Class 5

The document provides an overview of Java Abstract Classes and Interfaces, detailing their definitions, characteristics, and differences. It explains that an abstract class cannot be instantiated and can contain both abstract and non-abstract methods, while an interface serves as a blueprint with only abstract methods. Additionally, it discusses the uses of interfaces in achieving abstraction and multiple inheritance in Java, along with sample code examples and comparisons between abstract classes and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Java Abstract Class 5

The document provides an overview of Java Abstract Classes and Interfaces, detailing their definitions, characteristics, and differences. It explains that an abstract class cannot be instantiated and can contain both abstract and non-abstract methods, while an interface serves as a blueprint with only abstract methods. Additionally, it discusses the uses of interfaces in achieving abstraction and multiple inheritance in Java, along with sample code examples and comparisons between abstract classes and interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Name of the School: School of Computer Science and

Engineering
Course Code: E2U1A307C Course Name:Java Programming

DEPARTMENT OF COMPUTER SCIENCE


& ENGINEERING
Subject Name: Java Programming

Topics Covered: Java Abstract Class and Interface

Faculty Name: Sharmistha Dey Programe Name: BCA

1
ABSTRACT CLASS

• 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)
• Java abstract class is a class that can not be initiated by itself, it
needs to be subclassed by another class to use its properties. An
abstract class is declared using the “abstract” keyword in its
class definition

2
ABSTRACT CLASS

• An instance of an abstract class can not be


created.
• Constructors are allowed.
• We can have an abstract class without any
abstract method.
• There can be a final method in abstract class but
any abstract method in class(abstract class) can
not be declared as final or in simpler terms final
method can not be abstract itself as it will yield an
error: “Illegal combination of modifiers: abstract
and final”
3
• We can define static methods in an abstract class
ABSTRACT CLASS

4
ABSTRACT CLASS

//Abstract class
abstract class Company{ // Base class
abstract void printInfo(); class Base {
} public static void main(String
args[])
// Abstraction performed using {
extends Company c= new
class Employee extends Employee();
Company { c.printInfo();
void printInfo() }
{ }
String name = "avinash";
int age = 21;
float salary = 222.2F;

System.out.println(name);
System.out.println(age);
System.out.println(salary); 5
}
ABSTRACT CLASS

// Java Program to implement


Abstract Class class IT extends Subject {
// having constructor, data member, void syllabus(){
and methods System.out.println("C , Java ,
import java.io.*; C++");
}
abstract class Subject { }
Subject(){
System.out.println("Learning public class Test{
Subject"); public static void
} main(String[] args) {
Subject x=new IT();
abstract void syllabus();
x.syllabus();
void Learn(){ x.Learn();
System.out.println("Preparing }
Right Now!"); }
} 6
}
ABSTRACT CLASS

7
// Class 2
class Derived extends Base {
// Java Program to Illustrate
Abstract Class using constructor // Constructor of class2
Derived()
// Abstract class {
abstract class Base { System.out.println("Derived
Constructor Called");
// Constructor of class 1 }
Base()
{ // Method of class2
// Print statement void fun()
System.out.println("Base {
Constructor Called"); System.out.println("Derived fun()
} called");
}
// Abstract method inside }
class1
abstract void fun(); public class Test1{
} public static void main(String args[])
{
Derived d = new Derived();
d.fun(); 8
}
9
//Example of an abstract class that has
abstract and non-abstract methods
abstract class Bike{
Bike(){System.out.println("bike is
created");}
abstract void run();
void changeGear()
{System.out.println("gear changed");}
}
//Creating a Child class which inherits
Abstract class
class Honda extends Bike{
void run(){System.out.println("running
safely..");}
}
//Creating a Test class which calls
abstract and non-abstract methods
class Test2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
10
}
The "diamond problem" is a term used in object-oriented
programming, particularly in languages like C++ that
support multiple inheritance. It refers to an issue that arises
when a class inherits from two or more classes that have a
common base class
11
INTERFACE

An interface in Java is a blueprint of a class. It has


static constants and abstract methods.
The interface in Java is a mechanism to
achieve abstraction. There can be only abstract
methods in the Java interface, not method body. It is
used to achieve abstraction and multiple inheritance in
Java.

12
13
INTERFACE

14
INTERFACE

15
INTERFACE

16
Uses of Interfaces in Java are mentioned below:
•It is used to achieve total abstraction.
•Since java does not support multiple inheritances
in the case of class, by using an interface it can
achieve multiple inheritances.
•Any class can extend only 1 class, but can any
class implement an infinite number of interfaces.
•It is also used to achieve loose coupling.
•Interfaces are used to implement abstraction

17
1.interface Bank{
2.float rateOfInterest();
3.}
4.class SBI implements Bank{
5.public float rateOfInterest()
6.{return 9.15f;}
7.}
8.class PNB implements Bank{
9.public float rateOfInterest()
10.{return 9.7f;}
11.}
12.class Test3{
13.public static void
main(String[] args){
14.Bank b=new SBI();
15.System.out.println("ROI:
"+b.rateOfInterest());
16.Bank b1=new PNB();
17.System.out.println("ROI:
"+b1.rateOfInterest());
18.}}
18
Interface Real World
Program

19
Class Interface
In an interface, you can’t
In class, you can instantiate
instantiate variables and create
variables and create an object.
an object.

The interface cannot contain


A class can contain concrete
concrete (with implementation)
(with implementation) methods
methods.

The access specifiers used with


In Interface only one specifier
classes are private, protected,
is used- Public.
and public.

20
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods.
non-abstract methods. Since Java 8, it can have default and
static methods also.
2) Abstract class doesn't support Interface supports multiple inheritance.
multiple inheritance.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
5) The abstract keyword is used to The interface keyword is used to declare
declare abstract class. interface.
6) An abstract class can extend another An interface can extend another Java
Java class and implement multiple Java interface only.
interfaces.
7) An abstract class can be extended An interface can be implemented using
using keyword "extends". keyword "implements".
8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
21
SOME SAMPLE QUESTIONS

1. How do you define method in an


interface?
2. How can you implement multiple
interfaces in a single class?
3. What are the differences between
Abstract class and interface?
4. Can an interface have a constructor?

22
Video Link:
Interface
https://youtu.be/VYhmL038G1I?si=Y2wxEawsnnP
FMBMq
Abstract Class
https://youtu.be/vqV22AszAdw?
si=AlJn9PqqWESnNygc

Thank
You
23

You might also like