Java Abstract Class 5
Java Abstract Class 5
Engineering
Course Code: E2U1A307C Course Name:Java Programming
1
ABSTRACT CLASS
2
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
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
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.
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
22
Video Link:
Interface
https://youtu.be/VYhmL038G1I?si=Y2wxEawsnnP
FMBMq
Abstract Class
https://youtu.be/vqV22AszAdw?
si=AlJn9PqqWESnNygc
Thank
You
23