SlideShare una empresa de Scribd logo
1 de 20
Lecture-18
Instructor Name:
Object Oriented Programming
Today’s Lecture
 Abstract Methods
 Abstract Classes
 Interfaces
2
Abstract Classes
What is Abstract Class?
 A class that is declared with abstract keyword, is known as abstract class.
 An abstract class is one which is containing some defined method and some
undefined method.
 In java programming undefined methods are known as un-Implemented or
abstract method.
3
Abstract Classes
Syntax of Abstract Class
abstract class className
{
......
}
abstract class A
{
.....
}
4
Abstract Methods
What is an Abstract Method?
 An abstract method is one which contains only declaration or prototype but it
never contains body or definition.
 In order to make any undefined method as abstract, the declaration must be
predefined by abstract keyword.
Syntax
abstract ReturnType methodName(List of formal parameter)
Examples
abstract void sum();
abstract void diff(int, int);
5
Abstract Class & Methods
Example Abstract Class & Methods
abstract class Vachile {
abstract void speed(); // abstract method
}
class Bike extends Vachile {
void speed() {
System.out.println("Speed limit is 40 km/hr..");
}
public static void main(String args[]) {
Vachile obj = new Bike();
obj.speed();
}
}
6
Abstract Class & Methods
Important Points About Abstract Classes
Abstract class of java always contains common features.
 Every abstract class participate in inheritance.
 Abstract classes definitions should not be made as final because
abstract classes always participate in inheritance classes.
 An object of abstract class can not be created directly but it can be
created indirectly.
 All the abstract classes of java makes use of polymorphism along
with method overriding for business logic development and makes
use of dynamic binding for execution logic.
7
Abstract Class & Methods
Advantage of Abstract Classes
 Less memory space for the application
 Less execution time
 More performance
8
Abstract Class & Methods
When to Use Abstract Classes & Methods?
 Abstract methods are usually declared where two or more subclasses are
expected to fulfill a similar role in different ways through different
implementations
 These subclasses extend the same Abstract class and provide different
implementations for the abstract methods
 Use abstract classes to define broad types of behaviors at the top of an object-
oriented programming class hierarchy, and use its subclasses to provide
implementation details of the abstract class.
9
Interface
What is an Interface?
 Interface is similar to class which is collection of public static final variables
(constants) and abstract methods.
 The interface is a mechanism to achieve fully abstraction in java.
 There can be only abstract methods in the interface.
 It is used to achieve fully abstraction and multiple inheritance in Java.
Properties of Interface
 It is implicitly abstract. So no need to use the abstract keyword
 Each method in an interface is also implicitly abstract, so the abstract
keyword is not needed.
 Methods in an interface are implicitly public.
 All the data members of interface are implicitly public static final.
10
Interface
How Interface different from Class?
 You can not instantiate an interface.
 It does not contain any constructors.
 All methods in an interface are abstract.
 Interface can not contain instance fields. Interface only contains public static
final variables.
 Interface is can not extended by a class; it is implemented by a class.
 Interface can extend multiple interfaces. It means interface support multiple
inheritance
11
Interface
Behaviour of Compiler with Interface Program
12
Abstract vs Interface
When use Abstract & when Interface
 If we do not know about any things about implementation just we
have requirement specification then we should be go for Interface
 If we are talking about implementation but not completely (partially
implemented) then we should be go for abstract
13
Why do we use Interface?
Reason 1
 To reveal an object's programming interface (functionality of the
object) without revealing its implementation
– This is the concept of encapsulation
– The implementation can change without affecting the caller of the
interface
 The caller does not need the implementation at the compile time. It
needs only the interface at the compile time
 During runtime, actual object instance is associated with the
interface type.
14
Why do we use Interface?
Reason 2
 Interfaces are used in unrelated classes but have
implement similar methods (behaviors)
– One class is to a sub-class of another
 Example:
 – Class Line and class MyInteger
 They are not related through inheritance
 You want both to implement comparison methods
– checkIsGreater(Object x, Object y)
– checkIsLess(Object x, Object y)
– checkIsEqual(Object x, Object y)
15
Why do we use Interface?
Reason 3
 To model multiple inheritance
 A class can implement multiple interfaces while it can extend only
one class
16
Interface
Interface as Type
 When you define a new interface, you are defining a new reference
type.
 You can use interface names anywhere you can use any other type
name.
 If you define a reference variable whose type is an interface, any
object you assign to it must be an instance of a class that implements
the interface
 Let's say Person class implements PersonInterface interface
 You can do
Person p1 = new Person();
PersonInterface pi1 = p1;
PersonInterface pi2 = new Person(); 17
Problem Rewriting an Existing Interface
 Consider an interface that you have developed called DoIt:
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
}
 Suppose that, at a later time, you want to add a third method to DoIt
public interface DoIt {
void doSomething(int i, double x);
int doSomethingElse(String s);
boolean didItWork(int i, double x, String s);
}
18
Solution of Rewriting an Existing Interface
 If you make this change, all classes that implement the old DoItinterface will
break because they don't implement all methods of the the interface anymore
 Solution:
 Create more interfaces later● For example, you could create a DoItPlus
interface thatbextends DoIt:
public interface DoItPlus extends DoIt {
boolean didItWork(int i, double x, String s);
}
 Now users of your code can choose to continue to use the old interface or to
upgrade to the new interface
19
20

Más contenido relacionado

La actualidad más candente

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaEdureka!
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classesAKANSH SINGHAL
 
Data Structure Interview Questions & Answers
Data Structure Interview Questions & AnswersData Structure Interview Questions & Answers
Data Structure Interview Questions & AnswersSatyam Jaiswal
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
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 Singhdheeraj_cse
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 

La actualidad más candente (20)

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Java interfaces
Java   interfacesJava   interfaces
Java interfaces
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
Data Structure Interview Questions & Answers
Data Structure Interview Questions & AnswersData Structure Interview Questions & Answers
Data Structure Interview Questions & Answers
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Interface in java
Interface in javaInterface in java
Interface in java
 
C#
C#C#
C#
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
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
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Vvi
VviVvi
Vvi
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Interfaces
InterfacesInterfaces
Interfaces
 
C# interview questions
C# interview questionsC# interview questions
C# interview questions
 

Destacado (17)

Preference Verbs whith to Javier
Preference Verbs whith to JavierPreference Verbs whith to Javier
Preference Verbs whith to Javier
 
003 zhoulijun sfa
003 zhoulijun sfa003 zhoulijun sfa
003 zhoulijun sfa
 
Nota kkqt1
Nota kkqt1Nota kkqt1
Nota kkqt1
 
Getting_Started_With_Docker
Getting_Started_With_DockerGetting_Started_With_Docker
Getting_Started_With_Docker
 
People's Market CSV Analysis -2
People's Market CSV Analysis -2People's Market CSV Analysis -2
People's Market CSV Analysis -2
 
Challenges of Sustainable Rattan Development in Indonesia
Challenges of Sustainable Rattan Development in Indonesia  Challenges of Sustainable Rattan Development in Indonesia
Challenges of Sustainable Rattan Development in Indonesia
 
Actividad 11
Actividad  11Actividad  11
Actividad 11
 
Office for sale
Office for sale Office for sale
Office for sale
 
Kumaranallur
KumaranallurKumaranallur
Kumaranallur
 
Kelas a manajemen
Kelas a manajemenKelas a manajemen
Kelas a manajemen
 
The (Peninsula) medical student's guide to healthy eating
The (Peninsula) medical student's guide to healthy eatingThe (Peninsula) medical student's guide to healthy eating
The (Peninsula) medical student's guide to healthy eating
 
Kemosintesis Biologi sma dan Universitas
Kemosintesis Biologi sma dan UniversitasKemosintesis Biologi sma dan Universitas
Kemosintesis Biologi sma dan Universitas
 
Certificate- Royal Enfield
Certificate- Royal EnfieldCertificate- Royal Enfield
Certificate- Royal Enfield
 
632 theories
632 theories632 theories
632 theories
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Comparatives i superlatives
Comparatives i superlativesComparatives i superlatives
Comparatives i superlatives
 
Picture quiz - chronic knee pain in an adolescent
Picture quiz - chronic knee pain in an adolescentPicture quiz - chronic knee pain in an adolescent
Picture quiz - chronic knee pain in an adolescent
 

Similar a Lecture 18

OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxssuser84e52e
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Professor Lili Saghafi
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.pptVarunP31
 
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
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
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.pptrani marri
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programmingdeffa5
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerJeba Moses
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation javaJayasankarPR2
 

Similar a Lecture 18 (20)

Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
Interface
InterfaceInterface
Interface
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
 
Java interface
Java interface Java interface
Java interface
 
Interfaces .ppt
Interfaces .pptInterfaces .ppt
Interfaces .ppt
 
Interfaces.ppt
Interfaces.pptInterfaces.ppt
Interfaces.ppt
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
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
 
Java interface
Java interfaceJava interface
Java interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Core java questions
Core java questionsCore java questions
Core java questions
 
06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming06_OOVP.pptx object oriented and visual programming
06_OOVP.pptx object oriented and visual programming
 
Java 06
Java 06Java 06
Java 06
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
EEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answerEEE oops Vth semester viva questions with answer
EEE oops Vth semester viva questions with answer
 
Oops presentation java
Oops presentation javaOops presentation java
Oops presentation java
 

Más de talha ijaz

Más de talha ijaz (15)

Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Visual perception
Visual perceptionVisual perception
Visual perception
 
Lecture 11
Lecture 11Lecture 11
Lecture 11
 
Introduction
IntroductionIntroduction
Introduction
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 

Último

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 

Último (20)

Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 

Lecture 18

  • 2. Today’s Lecture  Abstract Methods  Abstract Classes  Interfaces 2
  • 3. Abstract Classes What is Abstract Class?  A class that is declared with abstract keyword, is known as abstract class.  An abstract class is one which is containing some defined method and some undefined method.  In java programming undefined methods are known as un-Implemented or abstract method. 3
  • 4. Abstract Classes Syntax of Abstract Class abstract class className { ...... } abstract class A { ..... } 4
  • 5. Abstract Methods What is an Abstract Method?  An abstract method is one which contains only declaration or prototype but it never contains body or definition.  In order to make any undefined method as abstract, the declaration must be predefined by abstract keyword. Syntax abstract ReturnType methodName(List of formal parameter) Examples abstract void sum(); abstract void diff(int, int); 5
  • 6. Abstract Class & Methods Example Abstract Class & Methods abstract class Vachile { abstract void speed(); // abstract method } class Bike extends Vachile { void speed() { System.out.println("Speed limit is 40 km/hr.."); } public static void main(String args[]) { Vachile obj = new Bike(); obj.speed(); } } 6
  • 7. Abstract Class & Methods Important Points About Abstract Classes Abstract class of java always contains common features.  Every abstract class participate in inheritance.  Abstract classes definitions should not be made as final because abstract classes always participate in inheritance classes.  An object of abstract class can not be created directly but it can be created indirectly.  All the abstract classes of java makes use of polymorphism along with method overriding for business logic development and makes use of dynamic binding for execution logic. 7
  • 8. Abstract Class & Methods Advantage of Abstract Classes  Less memory space for the application  Less execution time  More performance 8
  • 9. Abstract Class & Methods When to Use Abstract Classes & Methods?  Abstract methods are usually declared where two or more subclasses are expected to fulfill a similar role in different ways through different implementations  These subclasses extend the same Abstract class and provide different implementations for the abstract methods  Use abstract classes to define broad types of behaviors at the top of an object- oriented programming class hierarchy, and use its subclasses to provide implementation details of the abstract class. 9
  • 10. Interface What is an Interface?  Interface is similar to class which is collection of public static final variables (constants) and abstract methods.  The interface is a mechanism to achieve fully abstraction in java.  There can be only abstract methods in the interface.  It is used to achieve fully abstraction and multiple inheritance in Java. Properties of Interface  It is implicitly abstract. So no need to use the abstract keyword  Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.  Methods in an interface are implicitly public.  All the data members of interface are implicitly public static final. 10
  • 11. Interface How Interface different from Class?  You can not instantiate an interface.  It does not contain any constructors.  All methods in an interface are abstract.  Interface can not contain instance fields. Interface only contains public static final variables.  Interface is can not extended by a class; it is implemented by a class.  Interface can extend multiple interfaces. It means interface support multiple inheritance 11
  • 12. Interface Behaviour of Compiler with Interface Program 12
  • 13. Abstract vs Interface When use Abstract & when Interface  If we do not know about any things about implementation just we have requirement specification then we should be go for Interface  If we are talking about implementation but not completely (partially implemented) then we should be go for abstract 13
  • 14. Why do we use Interface? Reason 1  To reveal an object's programming interface (functionality of the object) without revealing its implementation – This is the concept of encapsulation – The implementation can change without affecting the caller of the interface  The caller does not need the implementation at the compile time. It needs only the interface at the compile time  During runtime, actual object instance is associated with the interface type. 14
  • 15. Why do we use Interface? Reason 2  Interfaces are used in unrelated classes but have implement similar methods (behaviors) – One class is to a sub-class of another  Example:  – Class Line and class MyInteger  They are not related through inheritance  You want both to implement comparison methods – checkIsGreater(Object x, Object y) – checkIsLess(Object x, Object y) – checkIsEqual(Object x, Object y) 15
  • 16. Why do we use Interface? Reason 3  To model multiple inheritance  A class can implement multiple interfaces while it can extend only one class 16
  • 17. Interface Interface as Type  When you define a new interface, you are defining a new reference type.  You can use interface names anywhere you can use any other type name.  If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface  Let's say Person class implements PersonInterface interface  You can do Person p1 = new Person(); PersonInterface pi1 = p1; PersonInterface pi2 = new Person(); 17
  • 18. Problem Rewriting an Existing Interface  Consider an interface that you have developed called DoIt: public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); }  Suppose that, at a later time, you want to add a third method to DoIt public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); boolean didItWork(int i, double x, String s); } 18
  • 19. Solution of Rewriting an Existing Interface  If you make this change, all classes that implement the old DoItinterface will break because they don't implement all methods of the the interface anymore  Solution:  Create more interfaces later● For example, you could create a DoItPlus interface thatbextends DoIt: public interface DoItPlus extends DoIt { boolean didItWork(int i, double x, String s); }  Now users of your code can choose to continue to use the old interface or to upgrade to the new interface 19
  • 20. 20