SlideShare a Scribd company logo
Object Oriented Programming with Java
(Subject Code: BCS-403)
Unit 1
Lecture 10
Lecture 10
• Abstraction
• Interfaces
• Abstract Class
Abstraction in Java
Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
Example to understand Abstraction:
Television remote control is an excellent example of
abstraction. It simplifies the interaction with a TV by
hiding the complexity behind simple buttons and
symbols, making it easy without needing to understand
the technical details of how the TV functions.
Abstract class in Java
• A class that is declared with abstract keyword,
is known as abstract class in java. It can have
abstract and non-abstract methods (method
with body).
Ways to achieve Abstaction
There are two ways to achieve abstraction in
java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract class in Java
• A class that is declared as abstract is known
as abstract class. It needs to be extended and its
method implemented. It cannot be instantiated.
Example
abstract class A{}
Abstract method
• A method that is declared as abstract and does
not have implementation is known as abstract
method.
Example
abstract void printStatus();//no body and abstract
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
Example of abstract class that has abstract method
In this example, Bike the abstract class that contains only one
abstract method run. It 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[]){
Honda4 obj = new Honda4();
obj.run();
}
}
abstract class Bank{
abstract double getRateOfInterest();
}
class SBI extends Bank{
double getRateOfInterest(){return 7.5;}
}
class PNB extends Bank{
double getRateOfInterest(){return 7;}
}
class TestBank{
public static void main(String args[]){
SBI b=new SBI();//if object is PNB, method of PNB will be invoked
int interest=b.getRateOfInterest();
System.out.println("Rate of Interest is: "+interest+" %");
}}
• Rate of Interest is: 7.5 %
An abstract class can have data member, abstract method, method body,
constructor and even main() method.
File: TestAbstraction2.java
//example of abstract class that have method body
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
} }
• Rule: If you are extending any abstract class
that have abstract method, you must either
provide the implementation of the method
or make this class abstract.
• Rule: If there is any abstract method in a
class, that class must be abstract.
The abstract class can also be used to provide some
implementation of the interface. In such case, the end user
may not be forced to override all the methods of the
interface.
interface A{
void a();
void b();
void c();
void d();
}
abstract class B implements A{
public void c(){System.out.println("I am C");}
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
class Test5{
public static void main(String args[]){
M a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Interface in Java
• An interface in java is a blueprint of a class. It has
static constants and abstract methods only.
• The interface in java is a mechanism to achieve
fully abstraction. There can be only abstract
methods in the java interface not method body. It
is used to achieve fully abstraction and multiple
inheritance in Java.
• Java Interface also represents IS-A relationship.
• It cannot be instantiated just like abstract class.
Why use Java interface?
There are mainly three reasons to use interface. They
are given below.
• It is used to achieve fully abstraction.
• By interface, we can support the functionality of
multiple inheritance.
• It can be used to achieve loose coupling.
The java compiler adds public and abstract keywords
before the interface method and public, static and
final keywords before data members.
In other words, Interface fields are public, static and
final by default, and methods are public and abstract.
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Multiple inheritance in Java by interface
• If a class implements multiple interfaces, or an
interface extends multiple interfaces i.e.
known as multiple inheritance.
Lecture-on-Object-Oriented-Programming-Language-Java.pptx
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print();
obj.show();
}
}
Interface inheritance
A class implements interface but one interface extends another interface .
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class Testinterface2 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
} }
Lecture-on-Object-Oriented-Programming-Language-Java.pptx

More Related Content

Similar to Lecture-on-Object-Oriented-Programming-Language-Java.pptx (20)

PDF
java-06inheritance
Arjun Shanka
 
PPTX
ABSTRACTION IN JAVA AAAAPROGRAMMING.pptx
DrNeetuSharma5
 
PDF
Basic_Java_10.pdf
KumarUtsav24
 
PPTX
Lecture 18
talha ijaz
 
PPTX
Object Orinted Programing(OOP) concepts \
Pritom Chaki
 
PPTX
Abstract Class and Interface for Java Intoductory course.pptx
DrShamimAlMamun
 
PDF
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
PDF
Java 06
Loida Igama
 
PPT
oops with java modules i & ii.ppt
rani marri
 
PPTX
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
PPTX
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
PPTX
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
PPTX
More oop in java
SAGARDAVE29
 
PPT
Java interfaces & abstract classes
Shreyans Pathak
 
PDF
Java abstract Keyword.pdf
SudhanshiBakre1
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Abstraction in java.pptx
AsifMulani17
 
PPTX
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
PPTX
Interface in java
Kavitha713564
 
java-06inheritance
Arjun Shanka
 
ABSTRACTION IN JAVA AAAAPROGRAMMING.pptx
DrNeetuSharma5
 
Basic_Java_10.pdf
KumarUtsav24
 
Lecture 18
talha ijaz
 
Object Orinted Programing(OOP) concepts \
Pritom Chaki
 
Abstract Class and Interface for Java Intoductory course.pptx
DrShamimAlMamun
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Professor Lili Saghafi
 
Java 06
Loida Igama
 
oops with java modules i & ii.ppt
rani marri
 
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
Object Oriented Programming - Polymorphism and Interfaces
Habtamu Wolde
 
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
More oop in java
SAGARDAVE29
 
Java interfaces & abstract classes
Shreyans Pathak
 
Java abstract Keyword.pdf
SudhanshiBakre1
 
Abstract class in java
Lovely Professional University
 
Abstraction in java.pptx
AsifMulani17
 
Abstraction in java [abstract classes and Interfaces
Ahmed Nobi
 
Interface in java
Kavitha713564
 

Recently uploaded (20)

PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
MRRS Strength and Durability of Concrete
CivilMythili
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Ad

Lecture-on-Object-Oriented-Programming-Language-Java.pptx

  • 1. Object Oriented Programming with Java (Subject Code: BCS-403) Unit 1 Lecture 10
  • 2. Lecture 10 • Abstraction • Interfaces • Abstract Class
  • 3. Abstraction in Java Abstraction is a process of hiding the implementation details and showing only functionality to the user. Example to understand Abstraction: Television remote control is an excellent example of abstraction. It simplifies the interaction with a TV by hiding the complexity behind simple buttons and symbols, making it easy without needing to understand the technical details of how the TV functions.
  • 4. Abstract class in Java • A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body). Ways to achieve Abstaction There are two ways to achieve abstraction in java • Abstract class (0 to 100%) • Interface (100%)
  • 5. Abstract class in Java • A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated. Example abstract class A{} Abstract method • A method that is declared as abstract and does not have implementation is known as abstract method. Example abstract void printStatus();//no body and abstract
  • 7. Example of abstract class that has abstract method In this example, Bike the abstract class that contains only one abstract method run. It 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[]){ Honda4 obj = new Honda4(); obj.run(); } }
  • 8. abstract class Bank{ abstract double getRateOfInterest(); } class SBI extends Bank{ double getRateOfInterest(){return 7.5;} } class PNB extends Bank{ double getRateOfInterest(){return 7;} } class TestBank{ public static void main(String args[]){ SBI b=new SBI();//if object is PNB, method of PNB will be invoked int interest=b.getRateOfInterest(); System.out.println("Rate of Interest is: "+interest+" %"); }} • Rate of Interest is: 7.5 %
  • 9. An abstract class can have data member, abstract method, method body, constructor and even main() method. File: TestAbstraction2.java //example of abstract class that have method body abstract class Bike{ Bike(){System.out.println("bike is created");} abstract void run(); void changeGear(){System.out.println("gear changed");} } class Honda extends Bike{ void run(){System.out.println("running safely..");} } class TestAbstraction2{ public static void main(String args[]){ Bike obj = new Honda(); obj.run(); obj.changeGear(); } }
  • 10. • Rule: If you are extending any abstract class that have abstract method, you must either provide the implementation of the method or make this class abstract. • Rule: If there is any abstract method in a class, that class must be abstract.
  • 11. The abstract class can also be used to provide some implementation of the interface. In such case, the end user may not be forced to override all the methods of the interface. interface A{ void a(); void b(); void c(); void d(); } abstract class B implements A{ public void c(){System.out.println("I am C");} }
  • 12. class M extends B{ public void a(){System.out.println("I am a");} public void b(){System.out.println("I am b");} public void d(){System.out.println("I am d");} } class Test5{ public static void main(String args[]){ M a=new M(); a.a(); a.b(); a.c(); a.d(); }}
  • 13. Interface in Java • An interface in java is a blueprint of a class. It has static constants and abstract methods only. • The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java. • Java Interface also represents IS-A relationship. • It cannot be instantiated just like abstract class.
  • 14. Why use Java interface? There are mainly three reasons to use interface. They are given below. • It is used to achieve fully abstraction. • By interface, we can support the functionality of multiple inheritance. • It can be used to achieve loose coupling. The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members. In other words, Interface fields are public, static and final by default, and methods are public and abstract.
  • 16. interface printable{ void print(); } class A6 implements printable{ public void print(){System.out.println("Hello");} public static void main(String args[]){ A6 obj = new A6(); obj.print(); } }
  • 17. Multiple inheritance in Java by interface • If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
  • 19. interface Printable{ void print(); } interface Showable{ void show(); } class A7 implements Printable,Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ A7 obj = new A7(); obj.print(); obj.show(); } }
  • 20. Interface inheritance A class implements interface but one interface extends another interface . interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class Testinterface2 implements Showable{ public void print(){System.out.println("Hello");} public void show(){System.out.println("Welcome");} public static void main(String args[]){ Testinterface2 obj = new Testinterface2(); obj.print(); obj.show(); } }