SlideShare a Scribd company logo
Chapter Nine Java
Inheritance
It is always nice if we could reuse something that
already exist rather than creating the same all over
again, Java supports this concept, Java classes can
be used in several ways, this is basically done by
creating new classes, reusing the properties of
existing once
Continue..
The mechanism of deriving a new class from an old
one is called inheritance, the old class is known as
the base class, superclass or parent class and the
new one is called the sub class, derived class or
child class.
To inherit a class, you simply incorporate the
definition of one class into another by using the
extends keyword.
Continue..
You can only specify one superclass for any subclass
that you create. Java does not support the
inheritance of multiple superclasses into a single
subclass. You can, as stated, create a hierarchy of
inheritance in which a subclass becomes a
superclass of another subclass. However, no class
can be a superclass of itself.
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.
Using super to Call Superclass Constructors
A subclass can call a constructor defined by its
superclass by use of the following form of super:
super(arg-list);
Here, arg-list specifies any arguments needed by the
constructor in the superclass. super( ) must always
be the first statement executed inside a subclass’
constructor.
Continue..
The second form of super acts somewhat like this,
except that it always refers to the superclass of the
subclass in which it is used.
This usage has the following general form:
super.member
Here, member can be either a method or an
instance variable.
Continue..
This second form of super is most applicable to
situations in which member names of a subclass
hide members by the same name in the superclass.
Continue..
Note :
A Superclass Variable Can Reference a Subclass
Object
A reference variable of a superclass can be assigned
a reference to any subclass derived from that
superclass.
You will find this aspect of inheritance quite useful
in a variety of situations
Types of Inheritance
1. Single Inheritance (one super class)
2. Multiple Inheritance (Several Super classes)
3. Multi-level Inheritance (Derived from a derived
class.
4. Hierarchical Inheritance (one super class many
sub class.
Method Overriding
In a class hierarchy, when a method in a subclass
has the same name and type signature as a method
in its superclass, then the method in the subclass is
said to override the method in the superclass.
Continue..
When an overridden method is called from within
its subclass, it will always refer to the version of that
method defined by the subclass.
The version of the method defined by the
superclass will be hidden.
Abstract Class
There are situations in which you will want to define
a superclass that declares the structure of a given
abstraction without providing a complete
implementation of every method. That is,
sometimes you will want to create a superclass that
only defines a generalized form that will be shared
by all of its subclasses, leaving it to each subclass to
fill in the details.
Continue..
Such a class determines the nature of the methods
that the subclasses must implement.
You can require that certain methods be overridden
by subclasses by specifying the abstract type
modifier. These methods are sometimes referred to
as subclasser responsibility because they have no
implementation specified in the superclass.
Continue..
Thus, a subclass must override them—it cannot
simply use the version defined in the superclass.
To declare an abstract method, use this general
form:
abstract type name(parameter-list);
As you can see, no method body is present.
Any class that contains one or more abstract
methods must also be declared abstract.
Continue..
To declare a class abstract, you simply use the
abstract keyword in front of the class keyword at
the beginning of the class declaration. There can be
no objects of an abstract class. That is, an abstract
class cannot be directly instantiated with the new
operator.
Such objects would be useless, because an abstract
class is not fully defined.
Continue..
Also, you cannot declare abstract constructors, or
abstract static methods.
Any subclass of an abstract class must either
implement all of the abstract methods in the
superclass, or be declared abstract itself.
Using Final to Prevent Overriding
While method overriding is one of Java’s most
powerful features, there will be times when
you will want to prevent it from occurring.
To disallow a method from being overridden,
specify final as a modifier at the start of its
declaration. Methods declared as final cannot
be overridden.
Using Final to prevent Inheritance
Sometimes you will want to prevent a class from
being inherited. To do this, precede the class
declaration with final.
Declaring a class as final implicitly declares all of its
methods as final, too.
Thank you

More Related Content

PPT
Inheritance polymorphism-in-java
DOC
Java classes and objects interview questions
PPTX
Computer programming 2 Lesson 6
DOC
116824015 java-j2 ee
PDF
Review oop and ood
PPTX
Java interview-quetions
PPTX
Chapter 8.2
PDF
Java inheritance
Inheritance polymorphism-in-java
Java classes and objects interview questions
Computer programming 2 Lesson 6
116824015 java-j2 ee
Review oop and ood
Java interview-quetions
Chapter 8.2
Java inheritance

What's hot (20)

PPTX
25 java interview questions
PDF
Lecture 10
PPTX
15reflection in c#
PPT
java tutorial 4
PPTX
Mca 2nd sem u-3 inheritance
PPTX
PPTX
Bca 2nd sem u-3 inheritance
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
DOC
Week3 dq1
PPT
9781439035665 ppt ch10
PPT
Chap11
PPSX
Dr. Rajeshree Khande : Java Inheritance
PDF
Java - Inheritance Concepts
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PPTX
Unit3 part1-class
PPTX
Chapter 3i
PPTX
Polymorphism in java
PPT
Java block structure
PPTX
C# Basics
25 java interview questions
Lecture 10
15reflection in c#
java tutorial 4
Mca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
JAVA PROGRAMMING – Packages - Stream based I/O
Week3 dq1
9781439035665 ppt ch10
Chap11
Dr. Rajeshree Khande : Java Inheritance
Java - Inheritance Concepts
Interface in java ,multiple inheritance in java, interface implementation
Unit3 part1-class
Chapter 3i
Polymorphism in java
Java block structure
C# Basics
Ad

Similar to Chapter 9 java (20)

DOCX
Core java by amit
PPTX
Chap3 inheritance
PDF
JAVA UNIT 2 BCA students' notes IPU university
PDF
Java/J2EE interview Qestions
PPT
Java inheritance
PPT
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
PPTX
Chapter 8 java
PPTX
Inheritance ppt
PPTX
Java session2
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Object Oriented Programming - Polymorphism and Interfaces
PPTX
Abstraction in java [abstract classes and Interfaces
DOCX
Java Core Parctical
PPTX
object oriented programming unit two ppt
PPTX
OCA Java SE 8 Exam Chapter 5 Class Design
PDF
java_inheritance.pdf
PPTX
Chapter 05 polymorphism extra
PPTX
Inheritance in Java
PPTX
Inheritance and Polymorphism
Core java by amit
Chap3 inheritance
JAVA UNIT 2 BCA students' notes IPU university
Java/J2EE interview Qestions
Java inheritance
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
Chapter 8 java
Inheritance ppt
Java session2
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Object Oriented Programming - Polymorphism and Interfaces
Abstraction in java [abstract classes and Interfaces
Java Core Parctical
object oriented programming unit two ppt
OCA Java SE 8 Exam Chapter 5 Class Design
java_inheritance.pdf
Chapter 05 polymorphism extra
Inheritance in Java
Inheritance and Polymorphism
Ad

More from Ahmad sohail Kakar (20)

PPTX
Lec 1 network types
PPTX
Lec 1 introduction
PPTX
Active directory restoration
PPTX
Active directory backup
PPT
Seii unit7 component-level-design
PPT
Seii unit6 software-testing-techniques
PPT
Seii unit5 ui_design
PPT
Seii unit4 software_process
PPT
Se ii unit3-architectural-design
PPT
Se ii unit2-software_design_principles
PPT
Se ii unit1-se_ii_intorduction
PDF
Second chapter-java
DOCX
Second chapter-java
PPTX
Chapter 7 java
PPTX
Chapter 6 java
PPTX
Chapter 5 java
PPTX
Chapter 4 java
PPTX
Chapter 3 java
PPTX
Chapter 2 java
PPTX
Chapter 1 java
Lec 1 network types
Lec 1 introduction
Active directory restoration
Active directory backup
Seii unit7 component-level-design
Seii unit6 software-testing-techniques
Seii unit5 ui_design
Seii unit4 software_process
Se ii unit3-architectural-design
Se ii unit2-software_design_principles
Se ii unit1-se_ii_intorduction
Second chapter-java
Second chapter-java
Chapter 7 java
Chapter 6 java
Chapter 5 java
Chapter 4 java
Chapter 3 java
Chapter 2 java
Chapter 1 java

Recently uploaded (20)

PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
A systematic review of self-coping strategies used by university students to ...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
LDMMIA Reiki Yoga Finals Review Spring Summer
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf

Chapter 9 java

  • 2. Inheritance It is always nice if we could reuse something that already exist rather than creating the same all over again, Java supports this concept, Java classes can be used in several ways, this is basically done by creating new classes, reusing the properties of existing once
  • 3. Continue.. The mechanism of deriving a new class from an old one is called inheritance, the old class is known as the base class, superclass or parent class and the new one is called the sub class, derived class or child class. To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.
  • 4. Continue.. You can only specify one superclass for any subclass that you create. Java does not support the inheritance of multiple superclasses into a single subclass. You can, as stated, create a hierarchy of inheritance in which a subclass becomes a superclass of another subclass. However, no class can be a superclass of itself.
  • 5. 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.
  • 6. Using super to Call Superclass Constructors A subclass can call a constructor defined by its superclass by use of the following form of super: super(arg-list); Here, arg-list specifies any arguments needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass’ constructor.
  • 7. Continue.. The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form: super.member Here, member can be either a method or an instance variable.
  • 8. Continue.. This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.
  • 9. Continue.. Note : A Superclass Variable Can Reference a Subclass Object A reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. You will find this aspect of inheritance quite useful in a variety of situations
  • 10. Types of Inheritance 1. Single Inheritance (one super class) 2. Multiple Inheritance (Several Super classes) 3. Multi-level Inheritance (Derived from a derived class. 4. Hierarchical Inheritance (one super class many sub class.
  • 11. Method Overriding In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
  • 12. Continue.. When an overridden method is called from within its subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.
  • 13. Abstract Class There are situations in which you will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes you will want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
  • 14. Continue.. Such a class determines the nature of the methods that the subclasses must implement. You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the superclass.
  • 15. Continue.. Thus, a subclass must override them—it cannot simply use the version defined in the superclass. To declare an abstract method, use this general form: abstract type name(parameter-list); As you can see, no method body is present. Any class that contains one or more abstract methods must also be declared abstract.
  • 16. Continue.. To declare a class abstract, you simply use the abstract keyword in front of the class keyword at the beginning of the class declaration. There can be no objects of an abstract class. That is, an abstract class cannot be directly instantiated with the new operator. Such objects would be useless, because an abstract class is not fully defined.
  • 17. Continue.. Also, you cannot declare abstract constructors, or abstract static methods. Any subclass of an abstract class must either implement all of the abstract methods in the superclass, or be declared abstract itself.
  • 18. Using Final to Prevent Overriding While method overriding is one of Java’s most powerful features, there will be times when you will want to prevent it from occurring. To disallow a method from being overridden, specify final as a modifier at the start of its declaration. Methods declared as final cannot be overridden.
  • 19. Using Final to prevent Inheritance Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final, too.