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
Ad

More Related Content

What's hot (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Type casting
Type castingType casting
Type casting
Ruchika Sinha
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
sharma230399
 
Java features
Java featuresJava features
Java features
Prashant Gajendra
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
Home
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
harsh kothari
 
Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
Youssef Mohammed Abohaty
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

Similar to Inheritance and interface (20)

inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptxinheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.pptweb program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
chetanpatilcp783
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
ManpreetSingh1387
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
MohammedNouh7
 
11slide
11slide11slide
11slide
IIUM
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish 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.ppt
rani marri
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
Jyothsna Sree
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya 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.pptx
RudranilDas11
 
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
AVINASH KUMAR
 
Basics to java programming and concepts of java
Basics to java programming and concepts of javaBasics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
Mahmoud Alfarra
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
inheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptxinheritance and interface in oops with java .pptx
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.pptweb program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
ManpreetSingh1387
 
11slide
11slide11slide
11slide
IIUM
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish 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.ppt
rani marri
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
Satya 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.pptx
RudranilDas11
 
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
AVINASH KUMAR
 
Basics to java programming and concepts of java
Basics to java programming and concepts of javaBasics to java programming and concepts of java
Basics to java programming and concepts of java
1747503gunavardhanre
 
OOPs Concepts - Android Programming
OOPs Concepts - Android ProgrammingOOPs Concepts - Android Programming
OOPs Concepts - Android Programming
Purvik Rana
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Ad

Recently uploaded (20)

International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Ad

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