SlideShare uma empresa Scribd logo
1 de 18
Inheritance: Definition
Inheritance: A parent-child
relationship between classes allows
sharing of the behavior of the parent
class into its child classes
one of the major benefits of object-
oriented programming (OOP) is this code
sharing between classes through
inheritance
Child class can add new behavior or
override existing behavior from parent
Reusability- building new components
by utilising existing components- is yet
another important aspect of OO
paradigm.
22-03-2018SALEEMQAISAR
1
Parent
Child
Inheritance terms
subclass, derived class, child class: Terms to describe the child in
the relationship, which accepts functionality from its parent.
class SubClassName extends SuperClassName
{
fields declaration;
methods declaration;
}
The keyword “extends” signifies that the properties of super class
are extended to the subclass. That means, subclass contains its own
members as well of those of the super class. This kind of situation
occurs when we want to enhance properties of existing class
without actually modifying it.
22-03-2018SALEEMQAISAR
2
Syntax Of Inheritance
public class Class extends ParentClass
{
//new variable or methods here
}
Example:
22-03-2018SALEEMQAISAR
3
superclass, base class, parent class: terms to describe the
parent in the relationship, which shares its functionality
extend, inherit, derive: Become a subclass of another class.
22-03-2018SALEEMQAISAR
4
Types Of Inheritance
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multi-Level inheritance
Hybrid inheritance
Multi-path inheritance
22-03-2018SALEEMQAISAR
5
Single inheritance
Single Inheritance is the simple
inheritance of all, When a class
extends another class(Only one
class) then we call it as Single
inheritance. The below diagram
represents the single inheritance
in java where Class B extends
only one class Class
A. Here Class B will be the Sub
class and Class A will be one and
only Super class.
22-03-2018SALEEMQAISAR
6
Multiple inheritance
Multiple Inheritance is nothing
but one class extending more tha
n one class. Multiple Inheritance
is basically not supported by
many Object Oriented
Programming languages such
as Java, Small Talk, C# etc.. (C++
Supports Multiple Inheritance). As
the Child class has to manage the
dependency of more than
one Parent class. But you can
achieve multiple inheritance in
Java using Interfaces.
22-03-2018SALEEMQAISAR
7
Multi-Level inheritance
In Multilevel Inheritance a
derived class will be inheriting a
parent class and as well as the
derived class act as the parent
class to other class. As seen in
the below diagram. Class B
inherits the property of Class
A and again Class B act as a
parent for Class C. In Short Class
A parent for Class B and Class
B parent for Class C.
22-03-2018SALEEMQAISAR
8
Hierarchical inheritance
In Hierarchical
inheritance one parent
class will be inherited
by many sub classes.
As per the below
example Class A will be
inherited by Class B,
Class C and Class
D. Class A will be acting
as a parent class
for Class B, Class
C and Class D.
22-03-2018SALEEMQAISAR
9
Hybrid Inheritance
Hybrid Inheritance is the
combination of both Single
and Multiple Inheritance.
Again Hybrid inheritance is
also not directly supported
in Java only through
interface we can achieve
this. Flow diagram of the
Hybrid inheritance will look
like below. As you can Class
A will be acting as
the Parent class for Class B &
Class C and Class B & Class
C will be acting
as Parent for Class D.
22-03-2018SALEEMQAISAR
10
Access modifiers
Java provides a number of access modifiers to set access
levels for classes, variables, methods, and constructors. The
four access levels are:
Default Access Modifier - No Keyword
Private Access Modifier - Private
Public Access Modifier - Public
Protected Access Modifier - Protected
22-03-2018SALEEMQAISAR
11
Default - No Keyword
Default access modifier means we do not explicitly declare an
access modifier for a class, field, method, etc.
A variable or method declared without any access control
modifier is available to any other class in the same package.
The fields in an interface are implicitly public static final and
the methods in an interface are by default public.
Example:
Variables and methods can be declared without any modifiers, as
in the following examples −
String version = "1.5.1";
boolean processOrder()
{ return true;
}
22-03-2018SALEEMQAISAR
12
Access Modifier - Private
Methods, variables, and constructors that are declared private
can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Private access modifier is the most restrictive access level.
Class and interfaces cannot be private.
Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
22-03-2018SALEEMQAISAR
13
Example
public class Logger
{
private String format;
public String getFormat()
{
return this.format;
}
public void setFormat(String format)
{
this.format = format;
}
}
22-03-2018SALEEMQAISAR
14
Access Modifier - Public
A class, method, constructor, interface, etc. declared public
can be accessed from any other class. Therefore, fields,
methods, blocks declared inside a public class can be accessed
from any class belonging to the Java Universe.
However, if the public class we are trying to access is in a
different package, then the public class still needs to be
imported. Because of class inheritance, all public methods and
variables of a class are inherited by its subclasses.
Example:
public static void main(String[] arguments) {
// ...
}
22-03-2018SALEEMQAISAR
15
Access Modifier - Protected
Variables, methods, and constructors, which are declared
protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of
the protected members' class.
The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected,
however methods and fields in a interface cannot be declared
protected.
Protected access gives the subclass a chance to use the helper
method or variable, while preventing a nonrelated class from
trying to use it.
22-03-2018SALEEMQAISAR
16
Example
The following parent class uses protected access control, to
allow its child class override openSpeaker() method −
class AudioPlayer
{
protected boolean openSpeaker(Speaker sp)
{
// implementation details
}
}
class StreamingAudioPlayer { boolean openSpeaker(Speaker sp)
{ // implementation details } }
22-03-2018SALEEMQAISAR
17
THANKS
ANY Q??
22-03-2018SALEEMQAISAR
18

Mais conteúdo relacionado

Mais procurados

java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in JavaSonya Akter Rupa
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptxKrutikaWankhade1
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
Multi level inheritence
Multi level inheritenceMulti level inheritence
Multi level inheritenceRanaMOIN1
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java yash jain
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 

Mais procurados (20)

java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Multi level inheritence
Multi level inheritenceMulti level inheritence
Multi level inheritence
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 

Semelhante a Inheritance and its types In Java

Semelhante a Inheritance and its types In Java (20)

Access Modifiers .pptx
Access Modifiers .pptxAccess Modifiers .pptx
Access Modifiers .pptx
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5eEo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
 
C++ Inheritance.pptx
C++ Inheritance.pptxC++ Inheritance.pptx
C++ Inheritance.pptx
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Inheritance and its necessity in java.ppt
Inheritance and its necessity in java.pptInheritance and its necessity in java.ppt
Inheritance and its necessity in java.ppt
 

Último

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Inheritance and its types In Java

  • 1. Inheritance: Definition Inheritance: A parent-child relationship between classes allows sharing of the behavior of the parent class into its child classes one of the major benefits of object- oriented programming (OOP) is this code sharing between classes through inheritance Child class can add new behavior or override existing behavior from parent Reusability- building new components by utilising existing components- is yet another important aspect of OO paradigm. 22-03-2018SALEEMQAISAR 1 Parent Child
  • 2. Inheritance terms subclass, derived class, child class: Terms to describe the child in the relationship, which accepts functionality from its parent. class SubClassName extends SuperClassName { fields declaration; methods declaration; } The keyword “extends” signifies that the properties of super class are extended to the subclass. That means, subclass contains its own members as well of those of the super class. This kind of situation occurs when we want to enhance properties of existing class without actually modifying it. 22-03-2018SALEEMQAISAR 2
  • 3. Syntax Of Inheritance public class Class extends ParentClass { //new variable or methods here } Example: 22-03-2018SALEEMQAISAR 3
  • 4. superclass, base class, parent class: terms to describe the parent in the relationship, which shares its functionality extend, inherit, derive: Become a subclass of another class. 22-03-2018SALEEMQAISAR 4
  • 5. Types Of Inheritance Single inheritance Multiple inheritance Hierarchical inheritance Multi-Level inheritance Hybrid inheritance Multi-path inheritance 22-03-2018SALEEMQAISAR 5
  • 6. Single inheritance Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class. 22-03-2018SALEEMQAISAR 6
  • 7. Multiple inheritance Multiple Inheritance is nothing but one class extending more tha n one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces. 22-03-2018SALEEMQAISAR 7
  • 8. Multi-Level inheritance In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. Class B inherits the property of Class A and again Class B act as a parent for Class C. In Short Class A parent for Class B and Class B parent for Class C. 22-03-2018SALEEMQAISAR 8
  • 9. Hierarchical inheritance In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example Class A will be inherited by Class B, Class C and Class D. Class A will be acting as a parent class for Class B, Class C and Class D. 22-03-2018SALEEMQAISAR 9
  • 10. Hybrid Inheritance Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can Class A will be acting as the Parent class for Class B & Class C and Class B & Class C will be acting as Parent for Class D. 22-03-2018SALEEMQAISAR 10
  • 11. Access modifiers Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are: Default Access Modifier - No Keyword Private Access Modifier - Private Public Access Modifier - Public Protected Access Modifier - Protected 22-03-2018SALEEMQAISAR 11
  • 12. Default - No Keyword Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public. Example: Variables and methods can be declared without any modifiers, as in the following examples − String version = "1.5.1"; boolean processOrder() { return true; } 22-03-2018SALEEMQAISAR 12
  • 13. Access Modifier - Private Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world. 22-03-2018SALEEMQAISAR 13
  • 14. Example public class Logger { private String format; public String getFormat() { return this.format; } public void setFormat(String format) { this.format = format; } } 22-03-2018SALEEMQAISAR 14
  • 15. Access Modifier - Public A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. Example: public static void main(String[] arguments) { // ... } 22-03-2018SALEEMQAISAR 15
  • 16. Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected. Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it. 22-03-2018SALEEMQAISAR 16
  • 17. Example The following parent class uses protected access control, to allow its child class override openSpeaker() method − class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer { boolean openSpeaker(Speaker sp) { // implementation details } } 22-03-2018SALEEMQAISAR 17