SlideShare a Scribd company logo
1
C.K.PITHAWALA COLLEGE OF
ENGINEERING & TECHNOLOGY, SURAT
Branch :- computer 3nd Year/5rd SEM (Div. D)
ALA Subject :- OOPJ
Topic Name :- Inheritance and interfaces
Group No :-B4
Enrolment No Name
Submitted To
160090107051
160090107049
160090107025
160090107044
160090107059
Shubham Sharma
Pakshal Shah
Hariom Maurya
Pravin Rathod
Naitik Vajani
Dr. Ami Choksi
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
{
………….
………….
}
class C extends A
{
………….
………….
}
class D 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

ODP
OOP java
xball977
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
Java static keyword
Lovely Professional University
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
File handling
priya_trehan
 
PPTX
Java 8 lambda
Manav Prasad
 
PPS
Wrapper class
kamal kotecha
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
OOP java
xball977
 
Java exception handling
BHUVIJAYAVELU
 
Java static keyword
Lovely Professional University
 
Java I/o streams
Hamid Ghorbani
 
File handling
priya_trehan
 
Java 8 lambda
Manav Prasad
 
Wrapper class
kamal kotecha
 
Java abstract class & abstract methods
Shubham Dwivedi
 

What's hot (20)

PPTX
Applets in java
Wani Zahoor
 
PPTX
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
PPT
Inheritance and Polymorphism
BG Java EE Course
 
PPTX
Presentation on-exception-handling
Nahian Ahmed
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPT
Java interfaces
Raja Sekhar
 
PDF
Collections In Java
Binoj T E
 
PPTX
Basics of JAVA programming
Elizabeth Thomas
 
PDF
Arrays in Java
Naz Abdalla
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPTX
Inheritance In Java
Manish Sahu
 
PPTX
Inner classes in java
PhD Research Scholar
 
PDF
Inheritance In Java
Arnab Bhaumik
 
PDF
Java I/O
Jussi Pohjolainen
 
PPT
Java Arrays
Jussi Pohjolainen
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPT
Java: GUI
Tareq Hasan
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Interface in java
PhD Research Scholar
 
Applets in java
Wani Zahoor
 
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
Inheritance and Polymorphism
BG Java EE Course
 
Presentation on-exception-handling
Nahian Ahmed
 
Inheritance in c++
Vineeta Garg
 
Java interfaces
Raja Sekhar
 
Collections In Java
Binoj T E
 
Basics of JAVA programming
Elizabeth Thomas
 
Arrays in Java
Naz Abdalla
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Inheritance In Java
Manish Sahu
 
Inner classes in java
PhD Research Scholar
 
Inheritance In Java
Arnab Bhaumik
 
Java Arrays
Jussi Pohjolainen
 
Core java complete ppt(note)
arvind pandey
 
Java: GUI
Tareq Hasan
 
Interfaces in java
Abishek Purushothaman
 
Inheritance in java
Lovely Professional University
 
Interface in java
PhD Research Scholar
 
Ad

Similar to Inheritance and interface (20)

PPTX
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PPTX
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
PPTX
Chapter 3i
siragezeynu
 
PPTX
Basics to java programming and concepts of java
1747503gunavardhanre
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PPTX
Inheritance ppt
Nivegeetha
 
PDF
Unit 2
Amar Jukuntla
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PPTX
OOP-JAVA-ONLYUNIT-2-PPT_removed (1).pptx
deepayaganti1
 
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
PPTX
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
Ch5 inheritance
HarshithaAllu
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Inheritance in java
Tech_MX
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Java inheritance
BHUVIJAYAVELU
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Chapter 3i
siragezeynu
 
Basics to java programming and concepts of java
1747503gunavardhanre
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
Inheritance ppt
Nivegeetha
 
object oriented programming unit two ppt
isiagnel2
 
OOP-JAVA-ONLYUNIT-2-PPT_removed (1).pptx
deepayaganti1
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Ad

Recently uploaded (20)

PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
ternal cell structure: leadership, steering
hodeeesite4
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPT
Ppt for engineering students application on field effect
lakshmi.ec
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Hyogeun Oh
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Inventory management chapter in automation and robotics.
atisht0104
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
ternal cell structure: leadership, steering
hodeeesite4
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Ppt for engineering students application on field effect
lakshmi.ec
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 

Inheritance and interface

  • 1. 1 C.K.PITHAWALA COLLEGE OF ENGINEERING & TECHNOLOGY, SURAT Branch :- computer 3nd Year/5rd SEM (Div. D) ALA Subject :- OOPJ Topic Name :- Inheritance and interfaces Group No :-B4 Enrolment No Name Submitted To 160090107051 160090107049 160090107025 160090107044 160090107059 Shubham Sharma Pakshal Shah Hariom Maurya Pravin Rathod Naitik Vajani Dr. Ami Choksi
  • 3. Contents  Introduction to inheritance  Types of Inheritance  Method overriding using inheritance  Super keyword  Final keyword  Interfaces
  • 4. 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.
  • 5. 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”.
  • 6. Types of inheritance • Single inheritance • Multilevel inheritance • Hierarchical inheritance
  • 7. 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
  • 8. 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 { …………. …………. }
  • 9. 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 { …………. …………. } class C extends A { …………. …………. } class D extends A { …………. …………. }
  • 10. 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.
  • 11. 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.
  • 12. 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.}
  • 13. 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
  • 14. 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.
  • 15. 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
  • 16. 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;
  • 17. 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.
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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 { ……….. ……….. }
  • 23. 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
  • 24. 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