SlideShare a Scribd company logo
Inheritance and
interface
Contents
🠶 Introduction to inheritance
🠶 Types of Inheritance
🠶 Method overriding using
inheritance
🠶 Super keyword
🠶 Final keyword
🠶 Interfaces
Introduction to inheritance
🠶 As the name suggests, inheritance means to take something that is already
made.
🠶 Inheritance is a unique feature of object oriented programming.
🠶 Inheritance is mainly used for code reusability.
🠶 The class which acquires the properties of the class is called as sub-class and
the class whose properties are acquired is called as superclass.
Consider the example:
As shown in example, the child inherit the properties
of father. Hence child will be subclass while father is a
superclass.
IS-A Relationship
🠶 Inheritance defines IS-A relationship between a super class and its subclass
🠶 For Example:
🠶 Car IS A vehicle
🠶 Bike IS A vehicle
🠶 EngineeringCollege IS A college
🠶 MedicalCollege IS A college
🠶 In java, inheritance is implemented with the help of keyword - “extends”.
Types of
inheritance
• Single inheritance
• Multilevel inheritance
• Hierarchical
inheritance
Single Inheritance
🠶 A structure having one and only one super class as well as
subclass.
🠶 Child class is authorized to access the property of Parent class.
Super
class
Subclass
Syntax
:
class A
{
………….
………….
}
class B extends
A
{
………….
………….
}
Sharing of
properties
Multilevel inheritance
B
C
A
🠶 When a class is derived from a class
which is also derived from a class,
then such type of inheritance is called
as multilevel inheritance.
Syntax
:
class A
{
………….
………….
}
class B extends
A
{
………….
………….
}
class C extends
B
{
………….
………….
}
Hierarchical inheritance
🠶 In hierarchical inheritance, one class
is inherited by many subclasses.
🠶 subclasses must be connected with
only
one super class.
B D
A
C
Syntax :
class A
{
…
……
….
…
……
….
}
class B
extends
A
{
…
……
….
…
……
….
}
Why multiple inheritance is not supported in java ?
🠶 To reduce the complexity and simply the language, java doesn’t
support
multiple inheritance.
🠶 Suppose there are 3 classes A,B and C. The C class inherits class A and
B.
🠶 If A and B have same method and you call it from child class object,
there will be ambiguity to call method of A or B class.
🠶 Since compile time errors are better than run time errors, java renders
compile time error if you inherit 2 classes.
Access Specifiers in java
🠶 There are four Access Specifiers in Java
🠶 1. Public: When a member of a class is declared as public specifier, it can
be accessed from any code.
🠶 2. Protected: Protected is only applicable in case of Inheritance. When a
member of a class is declared as protected, it can only be accessed by
the members of its class or subclass.
🠶 3. Private: A member of a class is declared as private specifier, can only
be accessed by the member of its class.
🠶 4. Default: When you don't specify a access specifier to a member, Java
automatically specifies a default. And the members modified by default can
only be accessed by any other code in the package, but can't be accessed
outside of a package.
A program demonstrating inheritance in Java
1. class circle{
2. double radius;
3. void radius(double a)
4. {
5. radius=a;
6. }
7. double getdata()
8. {
9. double area;
10.area=3.14*radius*radius;
11.return area;
12.}
13.}
14.class cylinder extends circle{
15.double h;
16.void height(double b,double
r)
17.{
18.radius=r;
19.h=b;
20.}
21.double getdata()
22.{
23.double volume;
24.volume=3.14*radius*radius*h;
25.return volume;
26.}
A program demonstrating inheritance in Java
27.public static void main(String[] args) {
28.cylinder c1=new cylinder();
29.c1.height(5.12,6.5);
30.double area,volume;
31.volume=c1.getdata();
32.System.out.println("Volume of
cylinder :"+volume);
33.circle ob=new circle();
34.ob.radius(4.44);
35.ob.getdata();
36.area=ob.getdata();
37.System.out.println("area of circle:"+area);
38.}
39.}
Output of program:
Volume of cylinder :679.2447999999
Area of circle:61.90070400000001
Method overriding in Java
Subclasses inherit all methods from their superclass
Sometimes, the implementation of the method in the superclass does
not
provide the functionality required by the subclass.
In these cases, the method must be overridden.
To override a method, provide an implementation in the subclass.
The method in the subclass MUST have the exact same signature as the
method it is overriding.
Program demonstrating method
overriding
1. class A{
2. void display()
3. {
4. System.out.println("This is parent
class.");
5. }
6. }
7. class B extends A{
8. void display()
9. {
10.System.out.println("This is first child
class");
11.}
12.public static void main(String[] args)
{
13.B b1=new B();
14.b1.display();
15.}
16.}
Output:
This is first child class
Super keyword
🠶 As the name suggest super is used to access the members of the super class.
🠶 It is used for two purposes in java.
1. The first use of keyword super is to access the data variables of the super class
hidden by the sub class.
🠶 e.g. Suppose class A is the super class that has two instance variables as int a
and float b.
🠶 class B is the subclass that also contains its own data members named a and
b.
🠶 Then we can access the super class (class A) variables a and b inside the
subclass class B just by calling the following command.
🠶 super.member;
Super keyword
🠶 2.Use of super to call super class constructor: The second use of the
keyword super in java is to call super class constructor in the subclass.
🠶 This functionality can be achieved just by using the following command.
super(param-list);
🠶 Here parameter list is the list of the parameter requires by the
constructor in the super class.
🠶 super must be the first statement executed inside a super class constructor.
🠶 If we want to call the default constructor then we pass the
empty parameter list.
🠶 If we dont use super then the compiler does this task implicitly to
instantiate
superclass members.
Program to demonstrate Super Keyword
1) class Animal{
2) Animal(){System.out.println("animal is created");}
3) }
4) class Dog extends Animal{
5) Dog(){
6) super();
7) System.out.println("dog is created");
8) }
9) }
10) class TestSuper3{
11) public static void main(String args[]){
12)Dog d=new
Dog(); 13)}}
Output:
Animal is created
Dog is created
Final Keyword
🠶 The final keyword in java is used to restrict the user.
🠶 The final keyword can be used in mainly 3 context.
1. Variable: If you make any variable final, you cannot change the
value of final variable.
Example:
1. class bike{
2. final int speedlimit=50;
3. void run(){
4. speedlimit=60; //Compile-
time error
5. }
6. public static void main(String[] args){
7. Bike ob=new bike();
8. ob.run();
9. }
10. }
Output:
Compile time
error
Final keyword
2. Method: If you make a method final, you cannot override
it.
Example:
1. class bike{
2. final void run(){
3. System.out.println(“Running”);
4. }
5. class honda extends run(){
6. void run(){ //compile time error
7. System.out.println(“Running at 100 kmph”);
8. }
9. }
10. public static void main(String[] args)
{
11. Bike ob=new bike();
12. ob.run();
13. }
14. }
Output:
Compile time
error
Final keyword
3. Class : If you make any class final, you cannot extend it.
Example:
1. final class bike{
2. //code here
3. }
4. class honda extends run(){ //Compile
time error
5. void run(){
6. System.out.println(“Running at 100 kmph”);
7. }
8. public static void main(String[] args){
9. honda ob=new honda();
10. ob.run();
11. }
12. }
Output:
Compile time
error
Interface in Java
🠶 Java Supports a special feature called
interface.
🠶 This feature helps to connect a class with more than
one classes (in order to achieve multiple
inheritance).
🠶 For this type of connectivity java uses
‘implements’
keyword.
🠶 A class can implements multiple interfaces at a
same time
🠶 All the variables defined in the interface are final
and
cannot be changed in subclass.
Syntax :
interface A
{
int a=10;
public int getdata();
public void display();
}
interface B
{
public void
getmarks();
}
class D implements A,B
{
………..
………..
Comparison between interface and abstract class
Features Interface Abstract class
Multiple inheritance A class may
implement multiple
interfaces
A class can extend only
one abstract class
Default implementation An interface cannot provide
any code at all
An abstract class can
provide complete code,
default code
Constants Constants are by
default Static and final
Both static and
instance constants are
allowed
Object An object of interface
is never created
An object of abstract
class can be created
Program demonstrating interfaces
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10. public static void main(String args[]){
11.A7 obj = new
A7();
12.obj.print();
13.obj.show();
14. }
15. }
Output:
Hello
Welcome
End of
presentation
thank you

More Related Content

Similar to inheritance and interface in oops with java .pptx (20)

PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PPTX
Basics to java programming and concepts of java
1747503gunavardhanre
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
Chapter 3i
siragezeynu
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPTX
Detailed_description_on_java_ppt_final.pptx
technicaljd3
 
PDF
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
PPT
Java Programming - Inheritance
Oum Saokosal
 
PPT
Chapter 8 Inheritance
OUM SAOKOSAL
 
PPTX
OOP with Java - Part 3
RatnaJava
 
PPTX
UNIT 5.pptx
CurativeServiceDivis
 
PPTX
Inheritance ppt
Nivegeetha
 
PPTX
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
PDF
Unit 2
Amar Jukuntla
 
PPTX
Chapter 9 java
Ahmad sohail Kakar
 
PPT
Java inheritance
GaneshKumarKanthiah
 
PDF
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Basics to java programming and concepts of java
1747503gunavardhanre
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Chapter 3i
siragezeynu
 
Java inheritance
BHUVIJAYAVELU
 
Detailed_description_on_java_ppt_final.pptx
technicaljd3
 
Java OOP Programming language (Part 5) - Inheritance
OUM SAOKOSAL
 
Java Programming - Inheritance
Oum Saokosal
 
Chapter 8 Inheritance
OUM SAOKOSAL
 
OOP with Java - Part 3
RatnaJava
 
Inheritance ppt
Nivegeetha
 
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Chapter 9 java
Ahmad sohail Kakar
 
Java inheritance
GaneshKumarKanthiah
 
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
Unit3 inheritance
Kalai Selvi
 
Unit3 part2-inheritance
DevaKumari Vijay
 

More from janetvidyaanancys (16)

PPTX
Understanding Global Warming_ Causes, Effects, and Solutions.pptx
janetvidyaanancys
 
PPTX
slidesgo-unlocking-the-power-of-nodejs-a-comprehensive-introduction-202501031...
janetvidyaanancys
 
PPTX
slidesgo-unlocking-the-power-of-jdbc-connecting-java-to-databases-20241030160...
janetvidyaanancys
 
PPTX
Comprehensive Analysis of React concept.pptx
janetvidyaanancys
 
PPTX
Angular in full stack A Comprehensive Overview.pptx
janetvidyaanancys
 
PPTX
Unit IV in python libraries and data wrangling .pptx
janetvidyaanancys
 
PPTX
Distributed Computing Introduction01.pptx
janetvidyaanancys
 
PPTX
control charts regarding industrial engineering.pptx
janetvidyaanancys
 
PPTX
Presentation about modern git and github.pptx
janetvidyaanancys
 
PPTX
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
PPTX
828f536b2ee20f4b02d8f4fd5da22ed2node js.pptx
janetvidyaanancys
 
PPT
Lecture on object oriented programming.ppt
janetvidyaanancys
 
PPTX
Lecture11 on oops with java buzzwords.pptx
janetvidyaanancys
 
PPTX
Untitled presentation about object oriented.pptx
janetvidyaanancys
 
PPTX
about java buzzwords of oops concept.pptx
janetvidyaanancys
 
DOCX
work-measurement-110911140505-phpapp01.docx
janetvidyaanancys
 
Understanding Global Warming_ Causes, Effects, and Solutions.pptx
janetvidyaanancys
 
slidesgo-unlocking-the-power-of-nodejs-a-comprehensive-introduction-202501031...
janetvidyaanancys
 
slidesgo-unlocking-the-power-of-jdbc-connecting-java-to-databases-20241030160...
janetvidyaanancys
 
Comprehensive Analysis of React concept.pptx
janetvidyaanancys
 
Angular in full stack A Comprehensive Overview.pptx
janetvidyaanancys
 
Unit IV in python libraries and data wrangling .pptx
janetvidyaanancys
 
Distributed Computing Introduction01.pptx
janetvidyaanancys
 
control charts regarding industrial engineering.pptx
janetvidyaanancys
 
Presentation about modern git and github.pptx
janetvidyaanancys
 
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
828f536b2ee20f4b02d8f4fd5da22ed2node js.pptx
janetvidyaanancys
 
Lecture on object oriented programming.ppt
janetvidyaanancys
 
Lecture11 on oops with java buzzwords.pptx
janetvidyaanancys
 
Untitled presentation about object oriented.pptx
janetvidyaanancys
 
about java buzzwords of oops concept.pptx
janetvidyaanancys
 
work-measurement-110911140505-phpapp01.docx
janetvidyaanancys
 
Ad

Recently uploaded (20)

PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Thermal runway and thermal stability.pptx
godow93766
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
Design Thinking basics for Engineers.pdf
CMR University
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
GitOps_Without_K8s_Training simple one without k8s
DanialHabibi2
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Day2 B2 Best.pptx
helenjenefa1
 
Ad

inheritance and interface in oops with java .pptx

  • 2. Contents 🠶 Introduction to inheritance 🠶 Types of Inheritance 🠶 Method overriding using inheritance 🠶 Super keyword 🠶 Final keyword 🠶 Interfaces
  • 3. Introduction to inheritance 🠶 As the name suggests, inheritance means to take something that is already made. 🠶 Inheritance is a unique feature of object oriented programming. 🠶 Inheritance is mainly used for code reusability. 🠶 The class which acquires the properties of the class is called as sub-class and the class whose properties are acquired is called as superclass. Consider the example: As shown in example, the child inherit the properties of father. Hence child will be subclass while father is a superclass.
  • 4. IS-A Relationship 🠶 Inheritance defines IS-A relationship between a super class and its subclass 🠶 For Example: 🠶 Car IS A vehicle 🠶 Bike IS A vehicle 🠶 EngineeringCollege IS A college 🠶 MedicalCollege IS A college 🠶 In java, inheritance is implemented with the help of keyword - “extends”.
  • 5. Types of inheritance • Single inheritance • Multilevel inheritance • Hierarchical inheritance
  • 6. Single Inheritance 🠶 A structure having one and only one super class as well as subclass. 🠶 Child class is authorized to access the property of Parent class. Super class Subclass Syntax : class A { …………. …………. } class B extends A { …………. …………. } Sharing of properties
  • 7. Multilevel inheritance B C A 🠶 When a class is derived from a class which is also derived from a class, then such type of inheritance is called as multilevel inheritance. Syntax : class A { …………. …………. } class B extends A { …………. …………. } class C extends B { …………. …………. }
  • 8. Hierarchical inheritance 🠶 In hierarchical inheritance, one class is inherited by many subclasses. 🠶 subclasses must be connected with only one super class. B D A C Syntax : class A { … …… …. … …… …. } class B extends A { … …… …. … …… …. }
  • 9. Why multiple inheritance is not supported in java ? 🠶 To reduce the complexity and simply the language, java doesn’t support multiple inheritance. 🠶 Suppose there are 3 classes A,B and C. The C class inherits class A and B. 🠶 If A and B have same method and you call it from child class object, there will be ambiguity to call method of A or B class. 🠶 Since compile time errors are better than run time errors, java renders compile time error if you inherit 2 classes.
  • 10. Access Specifiers in java 🠶 There are four Access Specifiers in Java 🠶 1. Public: When a member of a class is declared as public specifier, it can be accessed from any code. 🠶 2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is declared as protected, it can only be accessed by the members of its class or subclass. 🠶 3. Private: A member of a class is declared as private specifier, can only be accessed by the member of its class. 🠶 4. Default: When you don't specify a access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.
  • 11. A program demonstrating inheritance in Java 1. class circle{ 2. double radius; 3. void radius(double a) 4. { 5. radius=a; 6. } 7. double getdata() 8. { 9. double area; 10.area=3.14*radius*radius; 11.return area; 12.} 13.} 14.class cylinder extends circle{ 15.double h; 16.void height(double b,double r) 17.{ 18.radius=r; 19.h=b; 20.} 21.double getdata() 22.{ 23.double volume; 24.volume=3.14*radius*radius*h; 25.return volume; 26.}
  • 12. A program demonstrating inheritance in Java 27.public static void main(String[] args) { 28.cylinder c1=new cylinder(); 29.c1.height(5.12,6.5); 30.double area,volume; 31.volume=c1.getdata(); 32.System.out.println("Volume of cylinder :"+volume); 33.circle ob=new circle(); 34.ob.radius(4.44); 35.ob.getdata(); 36.area=ob.getdata(); 37.System.out.println("area of circle:"+area); 38.} 39.} Output of program: Volume of cylinder :679.2447999999 Area of circle:61.90070400000001
  • 13. Method overriding in Java Subclasses inherit all methods from their superclass Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass. In these cases, the method must be overridden. To override a method, provide an implementation in the subclass. The method in the subclass MUST have the exact same signature as the method it is overriding.
  • 14. Program demonstrating method overriding 1. class A{ 2. void display() 3. { 4. System.out.println("This is parent class."); 5. } 6. } 7. class B extends A{ 8. void display() 9. { 10.System.out.println("This is first child class"); 11.} 12.public static void main(String[] args) { 13.B b1=new B(); 14.b1.display(); 15.} 16.} Output: This is first child class
  • 15. Super keyword 🠶 As the name suggest super is used to access the members of the super class. 🠶 It is used for two purposes in java. 1. The first use of keyword super is to access the data variables of the super class hidden by the sub class. 🠶 e.g. Suppose class A is the super class that has two instance variables as int a and float b. 🠶 class B is the subclass that also contains its own data members named a and b. 🠶 Then we can access the super class (class A) variables a and b inside the subclass class B just by calling the following command. 🠶 super.member;
  • 16. Super keyword 🠶 2.Use of super to call super class constructor: The second use of the keyword super in java is to call super class constructor in the subclass. 🠶 This functionality can be achieved just by using the following command. super(param-list); 🠶 Here parameter list is the list of the parameter requires by the constructor in the super class. 🠶 super must be the first statement executed inside a super class constructor. 🠶 If we want to call the default constructor then we pass the empty parameter list. 🠶 If we dont use super then the compiler does this task implicitly to instantiate superclass members.
  • 17. Program to demonstrate Super Keyword 1) class Animal{ 2) Animal(){System.out.println("animal is created");} 3) } 4) class Dog extends Animal{ 5) Dog(){ 6) super(); 7) System.out.println("dog is created"); 8) } 9) } 10) class TestSuper3{ 11) public static void main(String args[]){ 12)Dog d=new Dog(); 13)}} Output: Animal is created Dog is created
  • 18. Final Keyword 🠶 The final keyword in java is used to restrict the user. 🠶 The final keyword can be used in mainly 3 context. 1. Variable: If you make any variable final, you cannot change the value of final variable. Example: 1. class bike{ 2. final int speedlimit=50; 3. void run(){ 4. speedlimit=60; //Compile- time error 5. } 6. public static void main(String[] args){ 7. Bike ob=new bike(); 8. ob.run(); 9. } 10. } Output: Compile time error
  • 19. Final keyword 2. Method: If you make a method final, you cannot override it. Example: 1. class bike{ 2. final void run(){ 3. System.out.println(“Running”); 4. } 5. class honda extends run(){ 6. void run(){ //compile time error 7. System.out.println(“Running at 100 kmph”); 8. } 9. } 10. public static void main(String[] args) { 11. Bike ob=new bike(); 12. ob.run(); 13. } 14. } Output: Compile time error
  • 20. Final keyword 3. Class : If you make any class final, you cannot extend it. Example: 1. final class bike{ 2. //code here 3. } 4. class honda extends run(){ //Compile time error 5. void run(){ 6. System.out.println(“Running at 100 kmph”); 7. } 8. public static void main(String[] args){ 9. honda ob=new honda(); 10. ob.run(); 11. } 12. } Output: Compile time error
  • 21. Interface in Java 🠶 Java Supports a special feature called interface. 🠶 This feature helps to connect a class with more than one classes (in order to achieve multiple inheritance). 🠶 For this type of connectivity java uses ‘implements’ keyword. 🠶 A class can implements multiple interfaces at a same time 🠶 All the variables defined in the interface are final and cannot be changed in subclass. Syntax : interface A { int a=10; public int getdata(); public void display(); } interface B { public void getmarks(); } class D implements A,B { ……….. ………..
  • 22. Comparison between interface and abstract class Features Interface Abstract class Multiple inheritance A class may implement multiple interfaces A class can extend only one abstract class Default implementation An interface cannot provide any code at all An abstract class can provide complete code, default code Constants Constants are by default Static and final Both static and instance constants are allowed Object An object of interface is never created An object of abstract class can be created
  • 23. Program demonstrating interfaces 1. interface Printable{ 2. void print(); 3. } 4. interface Showable{ 5. void show(); 6. } 7. class A7 implements Printable,Showable{ 8. public void print(){System.out.println("Hello");} 9. public void show(){System.out.println("Welcome");} 10. public static void main(String args[]){ 11.A7 obj = new A7(); 12.obj.print(); 13.obj.show(); 14. } 15. } Output: Hello Welcome