SlideShare a Scribd company logo
1 of 11
Download to read offline
INTERFACES
1
Multiple Inheritance
 class C has two super
classes as A & B
class A
{
int i=1;
}
class B
{
int i=2;
}
class C extends A,B
{
//two copies of i are available
}
2
A B
C
 Java does not allow such multiple inheritance
directly. So one class can not extend two
super classes
 The reason is to just keep the language
simple.
 Java allows multiple inheritance through the
use of interface concept.
3
Interfaces
 Interfaces are syntactically similar to classes, but they
lack instance variables, and their methods are declared
without any body.
 Interfaces defines only abstract methods and final
variables.
 Once it is defined, any number of classes can
implement an interface
 It is responsibility of class which implements the
interface to define the abstract methods
 Can’t create object for interface, but we can have
reference for it.
4
Defining an Interface
 interface keyword is used to define interface.
interface Interfacename {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
…………………………………..
}
 Methods end with a semicolon after the parameter
list i.e. they are abstract methods
 Variables are implicitly final and static.
5
Implementing Interfaces
 To implement an interface, the implements keyword is used
 General form
class classname implements interfacename1, interfacename2….
{
// class-body
}
or
class classname extends classname implements interfacename1,
interfacename2….
{
// class-body
}
6
Example of Interface
interface A {
void show(),
int a=100;
}
class B implements A {
public void show() {
a=20;//error
System.out.println(“B implements
method");
}
void display()
{
System.out.println(“B’s own method");
}
}
class InterfaceDemo {
public static void main(String args[]) {
B b1 = new B();
b1.display();
b1.show();
A a1 = new A(); //error
A a2;
a2=b1;
a2.show();
}
}
Write program for following
inheritance hierarchy
interface
8
STUDENT
int roll
void getroll()
TEST
int m1,m2
void getm()
SPORTS
float smarks=5.8f
void putsp()
RESULT
float total
void display()
import java.util.*;
class Student{
int roll;
Scanner sc =new Scanner(System.in);
void getroll()
{
System.out.println("Enter roll no");
roll=sc.nextInt();
}
}
class Test extends Student
{
int m1,m2;
Scanner sc =new Scanner(System.in);
void getm()
{
System.out.println("Enter m,arks of 2 subjects");
m1=sc.nextInt();
m2=sc.nextInt();
}
}
interface Sports
{
float m=5.8f;
void puts();
}
class Result extends Test implements Sports
{
float total;
public void puts()
{
System.out.println("sports marks="+m);
}
void display()
{
total=m1+m2+m;
System.out.println("Roll No="+roll);
System.out.println("Marks1="+m1);
System.out.println("Marks2="+m2);
System.out.println("sports marks="+m);
System.out.println("Total="+total);
}}
class MainClass
{
public static void main(String ar[])
{
Result r1=new Result();
r1.getroll();
r1.getm();
r1.puts();
r1.display();
}} 9
Interfaces Can Be Extended
 One interface can inherit another by use of the
keyword extends.
 The syntax is the same as for inheriting
classes.
 When a class implements an interface that
inherits another interface, it must provide
implementations for all methods defined
within the interface inheritance chain.
10
interface A {
void show();
}
interface B extends A {
void display();
}
// This class must implement all of A and B methods
class MyClass implements B {
public void show() {
System.out.println("Implement show().");
}
public void display() {
System.out.println("Implement display().");
}}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.show();
ob.display();
}}
11

More Related Content

Similar to Session 6_Java Interfaces_Details_Programs.pdf

Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Abou Bakr Ashraf
 
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.pptVISHNUSHANKARSINGH3
 
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
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploaddashpayal697
 
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
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxmigdalialyle
 
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
 
Indus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersIndus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersSushant Choudhary
 
Interface
InterfaceInterface
Interfacevvpadhu
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETjinaldesailive
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & InterfaceLinh Lê
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interfaceShubham Sharma
 

Similar to Session 6_Java Interfaces_Details_Programs.pdf (20)

Visula C# Programming Lecture 8
Visula C# Programming Lecture 8Visula C# Programming Lecture 8
Visula C# Programming Lecture 8
 
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
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
abstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx uploadabstract,final,interface (1).pptx upload
abstract,final,interface (1).pptx upload
 
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
 
Indicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docxIndicate whether each of the following statements is true or false.docx
Indicate whether each of the following statements is true or false.docx
 
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
 
Indus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answersIndus Valley Partner aptitude questions and answers
Indus Valley Partner aptitude questions and answers
 
Interface
InterfaceInterface
Interface
 
JAVA.pptx
JAVA.pptxJAVA.pptx
JAVA.pptx
 
Java Interface
Java InterfaceJava Interface
Java Interface
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Interfaces
InterfacesInterfaces
Interfaces
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
 
Abstract & Interface
Abstract & InterfaceAbstract & Interface
Abstract & Interface
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 

More from TabassumMaktum

Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptTabassumMaktum
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptTabassumMaktum
 
Introduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesIntroduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesTabassumMaktum
 
531AlmadhorAlwageed2010.ppt
531AlmadhorAlwageed2010.ppt531AlmadhorAlwageed2010.ppt
531AlmadhorAlwageed2010.pptTabassumMaktum
 
Introduction to Lex.ppt
Introduction to Lex.pptIntroduction to Lex.ppt
Introduction to Lex.pptTabassumMaktum
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptTabassumMaktum
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.pptTabassumMaktum
 
The World of Web Development 2015 - Part2.pptx
The World of Web Development 2015 - Part2.pptxThe World of Web Development 2015 - Part2.pptx
The World of Web Development 2015 - Part2.pptxTabassumMaktum
 

More from TabassumMaktum (15)

Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Introduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologiesIntroduction to Java Object Oiented Concepts and Basic terminologies
Introduction to Java Object Oiented Concepts and Basic terminologies
 
DigiLocker-Intro.pptx
DigiLocker-Intro.pptxDigiLocker-Intro.pptx
DigiLocker-Intro.pptx
 
Chapter12.ppt
Chapter12.pptChapter12.ppt
Chapter12.ppt
 
ch13.ppt
ch13.pptch13.ppt
ch13.ppt
 
531AlmadhorAlwageed2010.ppt
531AlmadhorAlwageed2010.ppt531AlmadhorAlwageed2010.ppt
531AlmadhorAlwageed2010.ppt
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Introduction to Lex.ppt
Introduction to Lex.pptIntroduction to Lex.ppt
Introduction to Lex.ppt
 
lex.pptx
lex.pptxlex.pptx
lex.pptx
 
Cloud Computing.pptx
Cloud Computing.pptxCloud Computing.pptx
Cloud Computing.pptx
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.ppt
 
Session_15_JSTL.pdf
Session_15_JSTL.pdfSession_15_JSTL.pdf
Session_15_JSTL.pdf
 
The World of Web Development 2015 - Part2.pptx
The World of Web Development 2015 - Part2.pptxThe World of Web Development 2015 - Part2.pptx
The World of Web Development 2015 - Part2.pptx
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 

Session 6_Java Interfaces_Details_Programs.pdf

  • 2. Multiple Inheritance  class C has two super classes as A & B class A { int i=1; } class B { int i=2; } class C extends A,B { //two copies of i are available } 2 A B C
  • 3.  Java does not allow such multiple inheritance directly. So one class can not extend two super classes  The reason is to just keep the language simple.  Java allows multiple inheritance through the use of interface concept. 3
  • 4. Interfaces  Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body.  Interfaces defines only abstract methods and final variables.  Once it is defined, any number of classes can implement an interface  It is responsibility of class which implements the interface to define the abstract methods  Can’t create object for interface, but we can have reference for it. 4
  • 5. Defining an Interface  interface keyword is used to define interface. interface Interfacename { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; ………………………………….. }  Methods end with a semicolon after the parameter list i.e. they are abstract methods  Variables are implicitly final and static. 5
  • 6. Implementing Interfaces  To implement an interface, the implements keyword is used  General form class classname implements interfacename1, interfacename2…. { // class-body } or class classname extends classname implements interfacename1, interfacename2…. { // class-body } 6
  • 7. Example of Interface interface A { void show(), int a=100; } class B implements A { public void show() { a=20;//error System.out.println(“B implements method"); } void display() { System.out.println(“B’s own method"); } } class InterfaceDemo { public static void main(String args[]) { B b1 = new B(); b1.display(); b1.show(); A a1 = new A(); //error A a2; a2=b1; a2.show(); } }
  • 8. Write program for following inheritance hierarchy interface 8 STUDENT int roll void getroll() TEST int m1,m2 void getm() SPORTS float smarks=5.8f void putsp() RESULT float total void display()
  • 9. import java.util.*; class Student{ int roll; Scanner sc =new Scanner(System.in); void getroll() { System.out.println("Enter roll no"); roll=sc.nextInt(); } } class Test extends Student { int m1,m2; Scanner sc =new Scanner(System.in); void getm() { System.out.println("Enter m,arks of 2 subjects"); m1=sc.nextInt(); m2=sc.nextInt(); } } interface Sports { float m=5.8f; void puts(); } class Result extends Test implements Sports { float total; public void puts() { System.out.println("sports marks="+m); } void display() { total=m1+m2+m; System.out.println("Roll No="+roll); System.out.println("Marks1="+m1); System.out.println("Marks2="+m2); System.out.println("sports marks="+m); System.out.println("Total="+total); }} class MainClass { public static void main(String ar[]) { Result r1=new Result(); r1.getroll(); r1.getm(); r1.puts(); r1.display(); }} 9
  • 10. Interfaces Can Be Extended  One interface can inherit another by use of the keyword extends.  The syntax is the same as for inheriting classes.  When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain. 10
  • 11. interface A { void show(); } interface B extends A { void display(); } // This class must implement all of A and B methods class MyClass implements B { public void show() { System.out.println("Implement show()."); } public void display() { System.out.println("Implement display()."); }} class IFExtend { public static void main(String arg[]) { MyClass ob = new MyClass(); ob.show(); ob.display(); }} 11