SlideShare a Scribd company logo
1 of 19
CAP615
PROGRAMMING IN JAVA
INHERITANCE & INTERFACES
Syllabus:
• overview of inheritance, working with
subclasses and superclasses,
• overriding methods in the super class,
introducing polymorphism,
• creating and extending abstract classes,
• polymorphism in the JDK foundation classes,
• using Interfaces,
• using the List Interface,
• introducing Lambda expressions
overview of inheritance
One class is hiring properties from another class is
called inheritance.
Advantages: Reusability
Class ClassA
{
}
Class ClassB extends ClassA
{
}
subclasses and superclasses
A class whose features are inherited is known as
super class(or a base class or a parent class).
here ClassA is called super class or base class or
parent class
A class that inherits the other class is known as
sub class (or derived class or a child class)
here ClassB is called sub class or derived class or
child class.
Inheritance Types:
• Single (Example: SingleInheritanceEx.java)
• Multilevel(Example:multilevel.java)
• Hierarchical(Example:hierarchical.java)
Using interface it is possible:
• Hybrid
• Multiple (using Interface:
Example:interfaceEx.java)
Single inheritance:
• One base class and one derived class
• One-to-one relationship
Base Class
Derived Class
Bank
protected int num1
void disp1()
Customer
int num2
void disp2(){
num1+num2}
Multilevel inheritance:
• One class is going to become base class for other derived class
Science
private bonus=2000
void disp1(){}
Computer
private bonus=3000
void disp2(){}
Faculty
double salary,totalSalary=0;
Faculty(){ salary=25000}
Base Class for class Science
Derived Class from Faculty and
Base Class for Computer
Derived Class from Science
Example
Hierarchical inheritance:
• One base class and multiple derived class, one –to-many relationship
SBI
Private IFSCCode
Bank(){rateofInterest=4.5}
PNB
Private IFSCCode
Bank(){rateofInterest=5.5}
Bank
rateofInterest
Bank(){rateofInterest=0} Example
Important Notes:-
• Inheritance Example(SingleInheritanceEx.java)(Single
Inheritance) or Relationship between one to one class.
• Default parent class constructor automatically called by child class but
for calling parameterized constructor of base class we need to use super
keyword.
• Example(inheritDefaultConstructorEx.java)-Default parent
class constructor calling in child class
• Example(inheritParameterizedConstructorEx.java)- parent
class parameterized constructor calling in child class(used
super)
• Example(callingMemberMethodUsingSuperEx.java)-calling
datamember and method using Super
Super keyword
• super keyword can be used to access the
immediate parent class constructor.
• super keyword can be used to invoke
immediate parent class members and
methods.
• In some scenario when a derived class and
base class has same data members or
methods, in that case super keyword also
used.
• Example: superEx.java
Method overriding
• Overriding Example:OverridenEx.java
• Difference between method overloding and
method overriding
Next Slide
Method Overloading Method Overriding
This is the example of compile time
polymorphism
This is the example of run time
polymorphism
Method overloading is performed within same
class.
Method overriding is performed within
different classes.
In case of method overloading, method
signature must be different.
In case of method overriding, method
signature must be same.
You can perform overloading on static
methods.
You cannot perform overriding on static
methods.
Example: Example:
interface
• Collection of abstract methods.
• Use keyword interface
• Achieve multiple inheritance
• Use implements keyword for calling in derive class
Syntax:
interfcae Bank
{
int rateOfInterest();
}
class SBI implements Bank {
int rateOfInterest()
{
retutn 5;
}
Abstract Class (Example: abstractEx.java)
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• A class which is declared with the abstract keyword
is known as an abstract class in Java.
• It holds both abstract and non-abstract methods.
• It can have constructor also
• We can achieve abstraction by using either abstract
class or interface.
• It can’t be instantiated
• The abstract class can also be used to provide some
implementation of the interface.
Q: Difference between abstract class and
interface
• Ans:Abstract class vs Interface in Java
• 1) First and the major difference between abstract class and an interface is
that an abstract class is a class while the interface is an interface, means by
extending the abstract class you cannot extend another class because Java
does not support multiple inheritances but you can implement multiple
inheritance in Java.
2) The second difference between interface and abstract class in Java is
that you cannot create a non-abstract method in an interface, every
method in an interface is by default abstract, but you can create a non-
abstract method in abstract class. Even a class which doesn't contain
any abstract method can be made abstract by using the abstract keyword.
3) The third difference between abstract class vs interface in Java is that
interface is better suited for Type declaration and abstract class is more
suited for code reuse and evolution perspective. The Effective Java has one
item dedicated to explaining about why you should be using interface for
type declaration. You should check that out as well.
4) The fourth difference between abstract class and interface in Java is that
abstract class are slightly faster than interface because interface involves a
search before calling any overridden method in Java. This is not a
significant difference in most of the cases but if you are writing a time critical
application then you may not want to leave any stone unturned.
5) Another notable difference between interface and abstract class is
that when you add a new method in existing interface it breaks all its
implementation and you need to provide an implementation in all clients
which is not good. By using an abstract class, you can provide a default
implementation for a new method in the superclass without breaking existing
clients.
List interface
• List Interface is implemented
by ArrayList, LinkedList, Vector and Stack class
es.
Creating List Objects:
List is an interface, and the instances of List
can be created in the following ways:
List a = new ArrayList(); List b = new
LinkedList(); List c = new Vector(); List d = new
Stack();
Working with List Interface Example
introducing Lambda expressions
• Lambda expression is a new feature of Java
which was included in Java SE 8.
• In programming, a Lambda expression (or
function) is just an anonymous function, i.e., a
function with no name
• The Lambda expression is used to provide the
implementation of an functional interface ,we
don't need to define the method again for
providing the implementation.
• Note: An interface which has only one abstract
method is called functional interface.
Java Lambda Expression Syntax
(argument-list) -> {body};
1) Argument-list: It can be empty or non-empty
2) Arrow-token: It is used to link arguments-list
and body of expression.
3) Body: It contains expressions and statements
for lambda expression.
Example: lambdaEx.java
lambdaEx2.java

More Related Content

What's hot

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
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
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In JavaMD SALEEM QAISAR
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NETSabith Byari
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Simplilearn
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingPurvik Rana
 
Java interfaces
Java interfacesJava interfaces
Java interfacesjehan1987
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
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 implementationHoneyChintal
 
Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaRavi_Kant_Sahu
 

What's hot (17)

Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Basic syntax
Basic syntaxBasic syntax
Basic syntax
 
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
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
OOPS – General Understanding in .NET
OOPS – General Understanding in .NETOOPS – General Understanding in .NET
OOPS – General Understanding in .NET
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Packages
PackagesPackages
Packages
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
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
 
Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 

Similar to Unit 4

Similar to Unit 4 (20)

PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Suga java training_with_footer
Suga java training_with_footerSuga java training_with_footer
Suga java training_with_footer
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Java 6.pptx
Java 6.pptxJava 6.pptx
Java 6.pptx
 
Java Interview Questions For Freshers
Java Interview Questions For FreshersJava Interview Questions For Freshers
Java Interview Questions For Freshers
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
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
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
Java Interview Questions
Java Interview QuestionsJava Interview Questions
Java Interview Questions
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
Java 06
Java 06Java 06
Java 06
 
Oop
OopOop
Oop
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
Viva file
Viva fileViva file
Viva file
 

More from LOVELY PROFESSIONAL UNIVERSITY

More from LOVELY PROFESSIONAL UNIVERSITY (19)

Enumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFTEnumerations, structure and class IN SWIFT
Enumerations, structure and class IN SWIFT
 
Dictionaries IN SWIFT
Dictionaries IN SWIFTDictionaries IN SWIFT
Dictionaries IN SWIFT
 
Control structures IN SWIFT
Control structures IN SWIFTControl structures IN SWIFT
Control structures IN SWIFT
 
Arrays and its properties IN SWIFT
Arrays and its properties IN SWIFTArrays and its properties IN SWIFT
Arrays and its properties IN SWIFT
 
Array and its functionsI SWIFT
Array and its functionsI SWIFTArray and its functionsI SWIFT
Array and its functionsI SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array  IN SWIFTpractice problems on array  IN SWIFT
practice problems on array IN SWIFT
 
practice problems on array IN SWIFT
practice problems on array IN SWIFTpractice problems on array IN SWIFT
practice problems on array IN SWIFT
 
practice problems on functions IN SWIFT
practice problems on functions IN SWIFTpractice problems on functions IN SWIFT
practice problems on functions IN SWIFT
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
 
Soft skills. pptx
Soft skills. pptxSoft skills. pptx
Soft skills. pptx
 
JAVA
JAVAJAVA
JAVA
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 3
Unit 3Unit 3
Unit 3
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Unit 1
Unit 1Unit 1
Unit 1
 
COMPLETE CORE JAVA
COMPLETE CORE JAVACOMPLETE CORE JAVA
COMPLETE CORE JAVA
 
Data wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGEData wrangling IN R LANGUAGE
Data wrangling IN R LANGUAGE
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

Unit 4

  • 2. INHERITANCE & INTERFACES Syllabus: • overview of inheritance, working with subclasses and superclasses, • overriding methods in the super class, introducing polymorphism, • creating and extending abstract classes, • polymorphism in the JDK foundation classes, • using Interfaces, • using the List Interface, • introducing Lambda expressions
  • 3. overview of inheritance One class is hiring properties from another class is called inheritance. Advantages: Reusability Class ClassA { } Class ClassB extends ClassA { }
  • 4. subclasses and superclasses A class whose features are inherited is known as super class(or a base class or a parent class). here ClassA is called super class or base class or parent class A class that inherits the other class is known as sub class (or derived class or a child class) here ClassB is called sub class or derived class or child class.
  • 5. Inheritance Types: • Single (Example: SingleInheritanceEx.java) • Multilevel(Example:multilevel.java) • Hierarchical(Example:hierarchical.java) Using interface it is possible: • Hybrid • Multiple (using Interface: Example:interfaceEx.java)
  • 6. Single inheritance: • One base class and one derived class • One-to-one relationship Base Class Derived Class Bank protected int num1 void disp1() Customer int num2 void disp2(){ num1+num2}
  • 7. Multilevel inheritance: • One class is going to become base class for other derived class Science private bonus=2000 void disp1(){} Computer private bonus=3000 void disp2(){} Faculty double salary,totalSalary=0; Faculty(){ salary=25000} Base Class for class Science Derived Class from Faculty and Base Class for Computer Derived Class from Science Example
  • 8. Hierarchical inheritance: • One base class and multiple derived class, one –to-many relationship SBI Private IFSCCode Bank(){rateofInterest=4.5} PNB Private IFSCCode Bank(){rateofInterest=5.5} Bank rateofInterest Bank(){rateofInterest=0} Example
  • 9. Important Notes:- • Inheritance Example(SingleInheritanceEx.java)(Single Inheritance) or Relationship between one to one class. • Default parent class constructor automatically called by child class but for calling parameterized constructor of base class we need to use super keyword. • Example(inheritDefaultConstructorEx.java)-Default parent class constructor calling in child class • Example(inheritParameterizedConstructorEx.java)- parent class parameterized constructor calling in child class(used super) • Example(callingMemberMethodUsingSuperEx.java)-calling datamember and method using Super
  • 10. Super keyword • super keyword can be used to access the immediate parent class constructor. • super keyword can be used to invoke immediate parent class members and methods. • In some scenario when a derived class and base class has same data members or methods, in that case super keyword also used. • Example: superEx.java
  • 11. Method overriding • Overriding Example:OverridenEx.java • Difference between method overloding and method overriding Next Slide
  • 12. Method Overloading Method Overriding This is the example of compile time polymorphism This is the example of run time polymorphism Method overloading is performed within same class. Method overriding is performed within different classes. In case of method overloading, method signature must be different. In case of method overriding, method signature must be same. You can perform overloading on static methods. You cannot perform overriding on static methods. Example: Example:
  • 13. interface • Collection of abstract methods. • Use keyword interface • Achieve multiple inheritance • Use implements keyword for calling in derive class Syntax: interfcae Bank { int rateOfInterest(); } class SBI implements Bank { int rateOfInterest() { retutn 5; }
  • 14. Abstract Class (Example: abstractEx.java) • Abstraction is a process of hiding the implementation details and showing only functionality to the user. • A class which is declared with the abstract keyword is known as an abstract class in Java. • It holds both abstract and non-abstract methods. • It can have constructor also • We can achieve abstraction by using either abstract class or interface. • It can’t be instantiated • The abstract class can also be used to provide some implementation of the interface.
  • 15. Q: Difference between abstract class and interface • Ans:Abstract class vs Interface in Java • 1) First and the major difference between abstract class and an interface is that an abstract class is a class while the interface is an interface, means by extending the abstract class you cannot extend another class because Java does not support multiple inheritances but you can implement multiple inheritance in Java. 2) The second difference between interface and abstract class in Java is that you cannot create a non-abstract method in an interface, every method in an interface is by default abstract, but you can create a non- abstract method in abstract class. Even a class which doesn't contain any abstract method can be made abstract by using the abstract keyword.
  • 16. 3) The third difference between abstract class vs interface in Java is that interface is better suited for Type declaration and abstract class is more suited for code reuse and evolution perspective. The Effective Java has one item dedicated to explaining about why you should be using interface for type declaration. You should check that out as well. 4) The fourth difference between abstract class and interface in Java is that abstract class are slightly faster than interface because interface involves a search before calling any overridden method in Java. This is not a significant difference in most of the cases but if you are writing a time critical application then you may not want to leave any stone unturned. 5) Another notable difference between interface and abstract class is that when you add a new method in existing interface it breaks all its implementation and you need to provide an implementation in all clients which is not good. By using an abstract class, you can provide a default implementation for a new method in the superclass without breaking existing clients.
  • 17. List interface • List Interface is implemented by ArrayList, LinkedList, Vector and Stack class es. Creating List Objects: List is an interface, and the instances of List can be created in the following ways: List a = new ArrayList(); List b = new LinkedList(); List c = new Vector(); List d = new Stack(); Working with List Interface Example
  • 18. introducing Lambda expressions • Lambda expression is a new feature of Java which was included in Java SE 8. • In programming, a Lambda expression (or function) is just an anonymous function, i.e., a function with no name • The Lambda expression is used to provide the implementation of an functional interface ,we don't need to define the method again for providing the implementation. • Note: An interface which has only one abstract method is called functional interface.
  • 19. Java Lambda Expression Syntax (argument-list) -> {body}; 1) Argument-list: It can be empty or non-empty 2) Arrow-token: It is used to link arguments-list and body of expression. 3) Body: It contains expressions and statements for lambda expression. Example: lambdaEx.java lambdaEx2.java