SlideShare a Scribd company logo
1 of 25
Download to read offline
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

What's hot

Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Abdul Hannan
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javajayc8586
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVAKUNAL GADHIA
 
OOPS In JAVA.pptx
OOPS In JAVA.pptxOOPS In JAVA.pptx
OOPS In JAVA.pptxSachin33417
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Prashanth Kumar
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarAnimesh Sarkar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Java Methods
Java MethodsJava Methods
Java MethodsOXUS 20
 
Training on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaTraining on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaShravan Sanidhya
 

What's hot (20)

Java threads
Java threadsJava threads
Java threads
 
polymorphism
polymorphism polymorphism
polymorphism
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java seminar
Java seminarJava seminar
Java seminar
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
 
OOPS In JAVA.pptx
OOPS In JAVA.pptxOOPS In JAVA.pptx
OOPS In JAVA.pptx
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java Methods
Java MethodsJava Methods
Java Methods
 
OOP java
OOP javaOOP java
OOP java
 
Training on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaTraining on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan Sanidhya
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Packages in java
Packages in javaPackages in java
Packages in java
 

Similar to Inheritance and interface

INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING ManpreetSingh1387
 
11slide
11slide11slide
11slideIIUM
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
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
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satyaSatya Johnny
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAVINASH KUMAR
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingPurvik Rana
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxkristinatemen
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 

Similar to Inheritance and interface (20)

Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
11slide
11slide11slide
11slide
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
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
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java
JavaJava
Java
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 

Recently uploaded

ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
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
 
(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
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
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
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 

Recently uploaded (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
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
 
(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
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
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
 
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 )
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 

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