SlideShare a Scribd company logo
Java Programming
Manish Kumar
(manish87it@gmail.com)
Lecture- 8
Abstract class &
Interface
Contents
 Introduction to Abstract Class
 Upcasting/Dynamic Dispatch Method
 Importance of Abstract Class (Using real life based program)
 Abstract Class Example (with constructor, methods and variables)
 Introduction to Interface
 Interface implementations
 Marker and Nested Interface
 Abstract Class Vs. Interface
Abstract class
 A class declared with “abstract” keyword is called abstract class. An abstract class may or may not contain
an “abstract method”.
 A method that cannot have definition (i.e., method body) part is known as abstract method.
 There are some important points of an abstract class as follows:
1. An abstract class must be declared with abstract keyword.
2. It contain both or none i.e., abstract and non-abstract method.
3. We cannot create an object of an abstract class.
Declaration of an abstract class and abstract method-
abstract class Abc {}
Here abstract is a keyword that is used to make a class as an abstract class.
abstract void display();
As you see above method declaration, there is no body of the method and declared with abstract keyword.
Example-1
abstract class Polygon {
abstract void area();
}
class Tringle extends Polygon {
void area() {
int r=10;
double pi=3.14;
double ar = pi *r*r;
System.out.println("Area of tringle = "+ar);
}
public static void main(String args[]) {
Polygon p = new Tringle();
p.area();
}
}
Tringle.java
Output –
Area of tringle = 314.0
Example-2
Note: - When an abstract class is inherited, then
derived class must provide the implementations
for all the abstract method declared in base class
otherwise it gives an error at compile time.
Tringle.java
abstract class Polygon {
abstract void area();
abstract void display();
}
class Tringle extends Polygon {
void area() {
int r=10;
double pi=3.14;
double ar = pi *r*r;
System.out.println("Area of tringle = "+ar);
}
public static void main(String args[]) {
Polygon p = new Tringle(); // Upcasting
p.area();
}
}
Output-
error: Tringle is not abstract and does not override
abstract method display() in Polygon
class Tringle extends Polygon {
^
1 error
upcasting
Upcasting/Dynamic Dispatch Method
When an object of derived class is assigned to base class reference variable, then it is called Upcasting.
In previous slide’s example – p is a reference variable of the base class and it assigned by an object of
derived class.
In this case, calling method first search the existence of method in base class then it will execute the method
in derived class.
Polygon p = new Tringle(); // Upcasting
Importance of abstract class
//Bank.java
abstract class RBI {
abstract int getROI();
}
class SBI extends RBI {
int getROI() {
return 7;
}}
class PNB extends RBI {
int getROI() {
return 8;
}}
class Bank {
public static void main(String args[]) {
RBI rb;
rb = new SBI();
System.out.println("ROI of SBI(%) = "+rb.getROI());
rb = new PNB();
System.out.println("ROI of PNB(%) = "+rb.getROI());
}
}
Output –
ROI of SBI(%) = 7
ROI of PNB(%) = 8
example
AbstractClassExample.java (constructor, variables
and methods):
abstract class Polygon{
Polygon() {
System.out.println("Polygon created."); }
abstract void area();
void display() {
System.out.println("This is non-abstract method");
}}
class Rectangle extends Polygon {
void area() {
int l=10,w=7, area;
area = l*w;
System.out.println("Area of Rectangle = "+area);
}
}
class AbstractClassExample {
public static void main(String args[]) {
Polygon p =new Rectangle();
p.area();
p.display();
}
}
Output –
Polygon created.
Area of Rectangle = 70
This is non-abstract method
interface
An interface is declared with interface keyword. We can achieve 100% abstraction using interface. It can be
defined as “the collection of public, static, final data member and abstract methods.”
There are few points that need to remembered:
1. Interface declaration is just like class; only difference is that it uses the keyword interface in place of
class.
2. The method in interface is by default public and abstract but since java 9; we can have private method in
an interface and since java 8; interface can have default method.
3. The data member in interface is by default public, static and final.
4. It also represents the IS-A relationship.
5. We can support multiple inheritance using interface.
6. We cannot create an object of an interface.
declaration
Interface <interface-name> {
//declare data member and methods
}
Declaring an interface:
Relationship between class and interface
•A class can implements an interface or more than one interface.
•An interface can extends another interface.
•An interface can also implements multiple interface.
Example - 1
interface Animal {
void livingPlace();
}
class Lion implements Animal {
public void livingPlace() {
System.out.println("The living place of Lion is Forest.");
}
public static void main(String args[]) {
Animal an = new Lion();
an.livingPlace();
}
}
Output-
The living place of Lion is Forest.
Lion.java
A class can implements an interface.
Example-2
Lion.java
interface Animal {
void livingPlace();
}
interface Birds {
void live();
}
class Lion implements Animal, Birds {
public void livingPlace() {
System.out.println("The living place of Lion is Forest.");
}
public void live() {
System.out.println("The living place of birds in Nest.");
}
public static void main(String args[]) {
Animal an= new Lion();
an.livingPlace();
Birds b = new Lion();
b.live();
}
}
Output-
The living place of Lion is Forest.
The living place of birds in Nest.
A class can implements an interface
or more than one interface.
Example-3
interface Animal {
void livingPlace();
}
interface Birds extends Animal {
void live();
}
class Lion implements Birds {
public void livingPlace() {
System.out.println("The living place
of Lion is Forest.");
}
public void live() {
System.out.println("The living place
of birds in Nest.");
}
public static void main(String args[]) {
Birds b = new Lion();
b.livingPlace();
b.live();
}
}
Lion.java
Output-
The living place of Lion is Forest.
The living place of birds in Nest.
An interface can extends another interface.
Example-4
interface Polygon {
int a = 10;
void area(int x);
}
class Tringle implements Polygon {
public void area(int x) {
double pi = 3.14;
double area = pi*x*x;
System.out.println("Area of tringle =
"+area);
}
public static void main(String args[]) {
Polygon p = new Tringle();
p.area(10);
}
}
Output-
Area of tringle = 314.0
When you override the abstract method
declared in interface it should be noted that
overridden method must be public because
the method in interface is by default public.
When you declare a variable in interface then
it is mandatory to initialize because it is by
default; public, static and final.
Marker & nested interface
An empty interface is called marker interface i.e. an interface without data member and methods called
Marker Interface. For example – Cloneable, Serializable, etc. They are used to provide essential information
to the JVM.
public interface Serializable { }
Nested Interface
An interface inside another interface is known as Nested Interface.
Interface Abc {
void show();
interface Xyz {
void display(); } }
Abstract class vs interface
Abstract Class Interface
Abstract class may have abstract and non-abstract method Interface can have only abstract method
Multiple inheritance not supported by abstract class. Interface supports multiple inheritance.
Abstract class can have static, non-static, final, non-final
variables.
Interface can have only static and final variables.
abstract keyword is used to declare a class as an Abstract
class.
interface keyword is used to declare an interface.
An abstract class can be extended using “extends”
keyword.
An interface can be implemented using “implements”
keyword.
We can achieve 0 to 100% abstraction. Achieve 100% abstraction.
Abstract class can have “private, protected, etc.” class
member.
Members of an interface are public and default only.
Lecture   8 abstract class and interface
Ad

More Related Content

What's hot (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
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
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
manish kumar
 
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
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
Java basic
Java basicJava basic
Java basic
Sonam Sharma
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
suraj pandey
 
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
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
manish kumar
 
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
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
Praveen M Jigajinni
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Unit 4 exceptions and threads
Unit 4 exceptions and threadsUnit 4 exceptions and threads
Unit 4 exceptions and threads
DevaKumari Vijay
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Chapter 8 Inheritance
Chapter 8 InheritanceChapter 8 Inheritance
Chapter 8 Inheritance
OUM SAOKOSAL
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
Overloading and overriding in vb.net
Overloading and overriding in vb.netOverloading and overriding in vb.net
Overloading and overriding in vb.net
suraj pandey
 

Similar to Lecture 8 abstract class and interface (20)

Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
talha ijaz
 
Java interface
Java interfaceJava interface
Java interface
Arati Gadgil
 
Interface in java
Interface in javaInterface in java
Interface in java
Kavitha713564
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
dashpayal697
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
Java 06
Java 06Java 06
Java 06
Loida Igama
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Interfaces
InterfacesInterfaces
Interfaces
Jai Marathe
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 
java program in Abstract class . pptx
java program in Abstract class .    pptxjava program in Abstract class .    pptx
java program in Abstract class . pptx
CmDept
 
Abstract class this is for ppt of JPR.pptx
Abstract class this is for ppt of JPR.pptxAbstract class this is for ppt of JPR.pptx
Abstract class this is for ppt of JPR.pptx
CmDept
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
Robert Rico Edited
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
Kp Sharma
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
rani marri
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
OOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programmingOOPS_Unit2.inheritance and interface objected oriented programming
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
dashpayal697
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt12.2 Abstract class and Interface.ppt
12.2 Abstract class and Interface.ppt
VISHNUSHANKARSINGH3
 
Objects and classes in OO Programming concepts
Objects and classes in OO Programming conceptsObjects and classes in OO Programming concepts
Objects and classes in OO Programming concepts
researchveltech
 
java program in Abstract class . pptx
java program in Abstract class .    pptxjava program in Abstract class .    pptx
java program in Abstract class . pptx
CmDept
 
Abstract class this is for ppt of JPR.pptx
Abstract class this is for ppt of JPR.pptxAbstract class this is for ppt of JPR.pptx
Abstract class this is for ppt of JPR.pptx
CmDept
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
RatnaJava
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Ad

Recently uploaded (20)

Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Envenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptxEnvenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptx
rekhapositivity
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Unit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its typesUnit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its types
bharath321164
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Envenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptxEnvenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptx
rekhapositivity
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
Unit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its typesUnit 4: Long term- Capital budgeting and its types
Unit 4: Long term- Capital budgeting and its types
bharath321164
 
Ad

Lecture 8 abstract class and interface

  • 2. Contents  Introduction to Abstract Class  Upcasting/Dynamic Dispatch Method  Importance of Abstract Class (Using real life based program)  Abstract Class Example (with constructor, methods and variables)  Introduction to Interface  Interface implementations  Marker and Nested Interface  Abstract Class Vs. Interface
  • 3. Abstract class  A class declared with “abstract” keyword is called abstract class. An abstract class may or may not contain an “abstract method”.  A method that cannot have definition (i.e., method body) part is known as abstract method.  There are some important points of an abstract class as follows: 1. An abstract class must be declared with abstract keyword. 2. It contain both or none i.e., abstract and non-abstract method. 3. We cannot create an object of an abstract class. Declaration of an abstract class and abstract method- abstract class Abc {} Here abstract is a keyword that is used to make a class as an abstract class. abstract void display(); As you see above method declaration, there is no body of the method and declared with abstract keyword.
  • 4. Example-1 abstract class Polygon { abstract void area(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(); } } Tringle.java Output – Area of tringle = 314.0
  • 5. Example-2 Note: - When an abstract class is inherited, then derived class must provide the implementations for all the abstract method declared in base class otherwise it gives an error at compile time. Tringle.java abstract class Polygon { abstract void area(); abstract void display(); } class Tringle extends Polygon { void area() { int r=10; double pi=3.14; double ar = pi *r*r; System.out.println("Area of tringle = "+ar); } public static void main(String args[]) { Polygon p = new Tringle(); // Upcasting p.area(); } } Output- error: Tringle is not abstract and does not override abstract method display() in Polygon class Tringle extends Polygon { ^ 1 error
  • 6. upcasting Upcasting/Dynamic Dispatch Method When an object of derived class is assigned to base class reference variable, then it is called Upcasting. In previous slide’s example – p is a reference variable of the base class and it assigned by an object of derived class. In this case, calling method first search the existence of method in base class then it will execute the method in derived class. Polygon p = new Tringle(); // Upcasting
  • 7. Importance of abstract class //Bank.java abstract class RBI { abstract int getROI(); } class SBI extends RBI { int getROI() { return 7; }} class PNB extends RBI { int getROI() { return 8; }} class Bank { public static void main(String args[]) { RBI rb; rb = new SBI(); System.out.println("ROI of SBI(%) = "+rb.getROI()); rb = new PNB(); System.out.println("ROI of PNB(%) = "+rb.getROI()); } } Output – ROI of SBI(%) = 7 ROI of PNB(%) = 8
  • 8. example AbstractClassExample.java (constructor, variables and methods): abstract class Polygon{ Polygon() { System.out.println("Polygon created."); } abstract void area(); void display() { System.out.println("This is non-abstract method"); }} class Rectangle extends Polygon { void area() { int l=10,w=7, area; area = l*w; System.out.println("Area of Rectangle = "+area); } } class AbstractClassExample { public static void main(String args[]) { Polygon p =new Rectangle(); p.area(); p.display(); } } Output – Polygon created. Area of Rectangle = 70 This is non-abstract method
  • 9. interface An interface is declared with interface keyword. We can achieve 100% abstraction using interface. It can be defined as “the collection of public, static, final data member and abstract methods.” There are few points that need to remembered: 1. Interface declaration is just like class; only difference is that it uses the keyword interface in place of class. 2. The method in interface is by default public and abstract but since java 9; we can have private method in an interface and since java 8; interface can have default method. 3. The data member in interface is by default public, static and final. 4. It also represents the IS-A relationship. 5. We can support multiple inheritance using interface. 6. We cannot create an object of an interface.
  • 10. declaration Interface <interface-name> { //declare data member and methods } Declaring an interface: Relationship between class and interface •A class can implements an interface or more than one interface. •An interface can extends another interface. •An interface can also implements multiple interface.
  • 11. Example - 1 interface Animal { void livingPlace(); } class Lion implements Animal { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public static void main(String args[]) { Animal an = new Lion(); an.livingPlace(); } } Output- The living place of Lion is Forest. Lion.java A class can implements an interface.
  • 12. Example-2 Lion.java interface Animal { void livingPlace(); } interface Birds { void live(); } class Lion implements Animal, Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Animal an= new Lion(); an.livingPlace(); Birds b = new Lion(); b.live(); } } Output- The living place of Lion is Forest. The living place of birds in Nest. A class can implements an interface or more than one interface.
  • 13. Example-3 interface Animal { void livingPlace(); } interface Birds extends Animal { void live(); } class Lion implements Birds { public void livingPlace() { System.out.println("The living place of Lion is Forest."); } public void live() { System.out.println("The living place of birds in Nest."); } public static void main(String args[]) { Birds b = new Lion(); b.livingPlace(); b.live(); } } Lion.java Output- The living place of Lion is Forest. The living place of birds in Nest. An interface can extends another interface.
  • 14. Example-4 interface Polygon { int a = 10; void area(int x); } class Tringle implements Polygon { public void area(int x) { double pi = 3.14; double area = pi*x*x; System.out.println("Area of tringle = "+area); } public static void main(String args[]) { Polygon p = new Tringle(); p.area(10); } } Output- Area of tringle = 314.0 When you override the abstract method declared in interface it should be noted that overridden method must be public because the method in interface is by default public. When you declare a variable in interface then it is mandatory to initialize because it is by default; public, static and final.
  • 15. Marker & nested interface An empty interface is called marker interface i.e. an interface without data member and methods called Marker Interface. For example – Cloneable, Serializable, etc. They are used to provide essential information to the JVM. public interface Serializable { } Nested Interface An interface inside another interface is known as Nested Interface. Interface Abc { void show(); interface Xyz { void display(); } }
  • 16. Abstract class vs interface Abstract Class Interface Abstract class may have abstract and non-abstract method Interface can have only abstract method Multiple inheritance not supported by abstract class. Interface supports multiple inheritance. Abstract class can have static, non-static, final, non-final variables. Interface can have only static and final variables. abstract keyword is used to declare a class as an Abstract class. interface keyword is used to declare an interface. An abstract class can be extended using “extends” keyword. An interface can be implemented using “implements” keyword. We can achieve 0 to 100% abstraction. Achieve 100% abstraction. Abstract class can have “private, protected, etc.” class member. Members of an interface are public and default only.