SlideShare a Scribd company logo
Unit 2
Inheritance ,Packages and Interfaces
Topics Covered
• Overloading Methods
• Object as parameter
• Retuning Objects
• Static ,Nested and inner classes
• Inheritance:Basics –Types of Inheritance
• Super Keyword
• Method Overriding
Overloading Method
• If a class has multiple methods having same
name but different in parameters, it is known
as Method Overloading
Different ways to overload the method
• By changing number of arguments
• By changing the data type
1) Method Overloading: changing no. of arguments
Output
sum of a and b is 5
sum of a and b is7
2) Method Overloading: changing data type
of arguments
Object as Parameters
Output
Sum of a and b :13
Returning Objects:
A method can return any type of data, including class types that you create.
The output generated by this
program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 2
Static,Nested and Inner Class:
• We can declare a class static using the static
keyword .
• A class be declared static only if it is a nested
class.
• The property of static class is that it does not
allow us to access non-static member of the
outer class.
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Inner Class
• The classes that are non-static and nested are
called Inner Class.
• Note:
• We cannot create an instance of inner class
without creating an instance of outer class.
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Nested Class
• Java allow us to define a class within a class
known as Nested Class.
• It may be static or non-static.
• The major difference between static and non-
static class
• 1.The static and non-static members of an
outer class can be accessed by an inner class.
• 2.The static members of the outer class can be
accessed only by the static class.
Inheritance:
• Definition1:
• Inheritance in Java is a mechanism in which one
object acquires all the properties and behaviors
of a parent object. It is an important part of OOPs
(Object Oriented programming system).
• Definition2:
• The process of deriving new from existing class
by sharing the properties of existing class is
known as inheritance.
• The new class that is created is known
as subclass (child or derived class) and the
existing class from where the child class is
derived is known as superclass (parent or base
class).
Why use inheritance in java
• For Code Reusability
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Member Access and Inheritance
• Although a subclass includes all of the
members of its superclass, it cannot access
those members of the superclass that have
been declared as private.
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Types of Inheritance’
• 1.Single
• 2.Multiple
• 3.Multilevel
• 4.hierarchial
• 5.hybrid
Single Inheritance
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Multiple Inheritance
• Deriving a new class from more than one base
class
Interface:
• An interface in Java is a blueprint of a class. It has
static constants and abstract methods.
• The interface in Java is a mechanism to achieve
abstraction. There can be only abstract methods in
the Java interface, not method body. It is used to
achieve abstraction and multiple inheritance in Java.
• In other words, you can say that interfaces can have
abstract methods and variables. It cannot have a
method body.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Output:
Total Distance Travelled : 750
Average Speed maintained : 1
Multi-level Inheritance
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Output
Inside display
Inside area
Inside volume
Example 2
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Hierarchial Inheritance
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Output
Class A
Class B
Class A
Class C
Class A
Class D
Super Keyword in Java
• The super keyword in Java is a reference
variable which is used to refer immediate
parent class object.
• Whenever you create the instance of subclass,
an instance of parent class is created implicitly
which is referred by super reference variable.
Usage of Java super Keyword
• super can be used to refer immediate parent
class instance variable.
• super can be used to invoke immediate
parent class method.
• super() can be used to invoke immediate
parent class constructor.
super is used to refer immediate parent class instance variable.
• We can use super keyword to access the data
member or field of parent class. It is used if
parent class and child class have same fields.
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
2) super can be used to invoke parent class method
• The super keyword can also be used to invoke
parent class method. It should be used if
subclass contains the same method as parent
class. In other words, it is used if method is
overridden.
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
3) super is used to invoke parent class constructor.
• The super keyword can also be used to invoke
the parent class constructor. Let's see a simple
example:
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Output:
animal is created
dog is created
super example: real use
Output:
1 ankit 45000
Method Overriding in Java
• If subclass (child class) has the same method
as declared in the parent class, it is known
as method overriding in Java.
• In other words, If a subclass provides the
specific implementation of the method that
has been declared by one of its parent class, it
is known as method overriding.
Usage of Java Method Overriding
• Method overriding is used to provide the
specific implementation of a method which is
already provided by its superclass.
• Method overriding is used for runtime
polymorphism
Rules for Java Method Overriding
• The method must have the same name as in
the parent class
• The method must have the same parameter
as in the parent class.
• There must be an IS-A relationship
(inheritance).
A real example of Java Method Overriding
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Difference between Method Overloading and
Method Overriding
• Can we override static method?
No, a static method cannot be overridden. It
can be proved by runtime polymorphism, so
we will learn it later.
Why can we not override static method?
It is because the static method is bound with
class whereas instance method is bound with
an object. Static belongs to the class area, and
an instance belongs to the heap area.
Can we override java main method?
No, because the main is a static method.

More Related Content

PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
PPTX
object oriented programming unit two ppt
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Java inheritance
PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Java - Inheritance_multiple_inheritance.pptx
PPT
L7 inheritance
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
object oriented programming unit two ppt
‏‏‏‏‏‏oop lecture objectives will come.pdf
Modules 333333333³3444444444444444444.pptx
Java inheritance
Inheritance,single,multiple.access rulepptx
Java - Inheritance_multiple_inheritance.pptx
L7 inheritance

Similar to OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx (20)

PPT
L7 inheritance
PPTX
Unit No 3 Inheritance annd Polymorphism.pptx
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PPTX
Unit3 inheritance
PDF
Inheritance and interface
PPTX
Unit3 part2-inheritance
PPTX
Ch5 inheritance
PPTX
PPTX
inheritance and interface in oops with java .pptx
PPTX
Inheritance Interface and Packags in java programming.pptx
PPTX
Inheritance ppt
PPTX
Basics to java programming and concepts of java
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Chapter 3i
PPTX
Detailed_description_on_java_ppt_final.pptx
PPTX
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
PDF
JAVA UNIT 2 BCA students' notes IPU university
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
Inheritance1
PPT
Ap Power Point Chpt7
L7 inheritance
Unit No 3 Inheritance annd Polymorphism.pptx
OOPS_Unit2.inheritance and interface objected oriented programming
Unit3 inheritance
Inheritance and interface
Unit3 part2-inheritance
Ch5 inheritance
inheritance and interface in oops with java .pptx
Inheritance Interface and Packags in java programming.pptx
Inheritance ppt
Basics to java programming and concepts of java
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Chapter 3i
Detailed_description_on_java_ppt_final.pptx
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
JAVA UNIT 2 BCA students' notes IPU university
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance1
Ap Power Point Chpt7
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Classroom Observation Tools for Teachers
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Sports Quiz easy sports quiz sports quiz
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Lesson notes of climatology university.
Anesthesia in Laparoscopic Surgery in India
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Classroom Observation Tools for Teachers
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Sports Quiz easy sports quiz sports quiz
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Ad

OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx

  • 2. Topics Covered • Overloading Methods • Object as parameter • Retuning Objects • Static ,Nested and inner classes • Inheritance:Basics –Types of Inheritance • Super Keyword • Method Overriding
  • 3. Overloading Method • If a class has multiple methods having same name but different in parameters, it is known as Method Overloading Different ways to overload the method • By changing number of arguments • By changing the data type
  • 4. 1) Method Overloading: changing no. of arguments Output sum of a and b is 5 sum of a and b is7
  • 5. 2) Method Overloading: changing data type of arguments
  • 7. Returning Objects: A method can return any type of data, including class types that you create. The output generated by this program is shown here: ob1.a: 2 ob2.a: 12 ob2.a after second increase: 2
  • 8. Static,Nested and Inner Class: • We can declare a class static using the static keyword . • A class be declared static only if it is a nested class. • The property of static class is that it does not allow us to access non-static member of the outer class.
  • 10. Inner Class • The classes that are non-static and nested are called Inner Class. • Note: • We cannot create an instance of inner class without creating an instance of outer class.
  • 13. Nested Class • Java allow us to define a class within a class known as Nested Class. • It may be static or non-static. • The major difference between static and non- static class • 1.The static and non-static members of an outer class can be accessed by an inner class. • 2.The static members of the outer class can be accessed only by the static class.
  • 14. Inheritance: • Definition1: • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system). • Definition2: • The process of deriving new from existing class by sharing the properties of existing class is known as inheritance.
  • 15. • The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is derived is known as superclass (parent or base class).
  • 16. Why use inheritance in java • For Code Reusability The syntax of Java Inheritance class Subclass-name extends Superclass-name { //methods and fields }
  • 20. Member Access and Inheritance • Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.
  • 22. Types of Inheritance’ • 1.Single • 2.Multiple • 3.Multilevel • 4.hierarchial • 5.hybrid
  • 26. Multiple Inheritance • Deriving a new class from more than one base class
  • 27. Interface: • An interface in Java is a blueprint of a class. It has static constants and abstract methods. • The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java. • In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
  • 28. Syntax: interface <interface_name>{ // declare constant fields // declare methods that abstract // by default. }
  • 30. Output: Total Distance Travelled : 750 Average Speed maintained : 1
  • 38. Output Class A Class B Class A Class C Class A Class D
  • 39. Super Keyword in Java • The super keyword in Java is a reference variable which is used to refer immediate parent class object. • Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
  • 40. Usage of Java super Keyword • super can be used to refer immediate parent class instance variable. • super can be used to invoke immediate parent class method. • super() can be used to invoke immediate parent class constructor.
  • 41. super is used to refer immediate parent class instance variable. • We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.
  • 43. 2) super can be used to invoke parent class method • The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.
  • 45. 3) super is used to invoke parent class constructor. • The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:
  • 49. super example: real use Output: 1 ankit 45000
  • 50. Method Overriding in Java • If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. • In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
  • 51. Usage of Java Method Overriding • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass. • Method overriding is used for runtime polymorphism
  • 52. Rules for Java Method Overriding • The method must have the same name as in the parent class • The method must have the same parameter as in the parent class. • There must be an IS-A relationship (inheritance).
  • 53. A real example of Java Method Overriding
  • 56. Difference between Method Overloading and Method Overriding
  • 57. • Can we override static method? No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later. Why can we not override static method? It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area. Can we override java main method? No, because the main is a static method.