Professional Documents
Culture Documents
PROGRAMMING
TOPICS
1. INTRODUCTION 2. FUNDAMENTALS
TO OBJECT- OF THE JAVA 3. CLASSES AND
ORIENTED PROGRAMMING OBJECTS
PROGRAMMING LANGUAGE
5. MULTI-
4. INHERITANCE
THREADING AND
AND
EXCEPTION
POLYMORPHISM
HANDLING
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Inheritance
• Inheritance is an effective way to reuse existing
codes from the existing class.
• Inheritance concept is similar to the biology
concept where children usually inherit their parent’s
characteristics.
• Normally in inheritance concept, there are two
types of class, which are the super class and the
sub class.
• The sub class can access protected data from
super class.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Single inheritance
• When a class is inherited from only one existing
class, then it is Single inheritance.
• The inherited class is a sub class and the existing
class from which a sub class is created is the super
class.
• In single inheritance a super class can have any
number of sub classes but a sub class can have
only one super class.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
class B extends A
Class B Sub {
class -------
-------
Open file:
Sample35a.java & Sample35.java }
Compile and run. Observe the output.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Single Open file:
Inheritance Class A Sample35b.java
Compile and run.
(Hierarchical) Observe the
output.
Multiple Inheritance
Class A Class B
Class C
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Multi-level inheritance
• Multi-Level Inheritance is the extension of Single
Inheritance.
• When a class is inherited from another sub class,
then it is Multi-Level Inheritance.
• The sub class at the lowest level can access the
member variables and methods of all the super
classes at the higher level.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Class A Super
class
Class B
Sub class Super class
for Class A for Class C
Super class
• A new class can be inherited from an existing class.
Sub class
• The new class that is inherited from an existing class is
known as Subclass.
• This class is also called as Derived class.
• The subclass inherits all the accessible methods and
member variables of the super class.
• A subclass is created using the extends keyword.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs
Creating sub class : Syntax
class <class_name> extends <existing_class_name>
{
}
• An object of the sub class can access the variables and methods of the
super class.
Topic 4
Apply Inheritance in Java programs Keyword super
Object.variable
Object.method()
Topic 4
Apply Inheritance in Java programs Keyword super
Method overriding
• Method Overriding refers to the concept of the methods
in the sub class and the super class using the same
method name with identical signature.
• The method in the sub class hides or overrides the
method definition in the super class. This is known as
Method Overriding.
• Method overriding can be used in situations, where you
want a method in the super class to be implemented
differently in the sub class but with same name and
parameters
Open file: Sample36b.java. Compile and run. Observe the output.
Topic 4
Implement Polymorphism in Java
Polymorphism in Java programs
programs
Polymorphism
• Polymorphism is the existence of an entity in many forms.
• In object oriented programming language it refers to the ability of the
objects to behave differently based on the input given.
• Polymorphism is implemented in Java using techniques:
➢ Method Overriding
Method overloading
• Methods which have same name but different parameters
are called as overloaded methods.
• The parameters in overloaded methods should differ in at
least one of the following
➢ Number of parameters
➢ Data type of the parameters
Method Overriding
• The method in the sub class hides or overrides the method
definition in the super class with identical signature.
Topic 4
Implement Abstract Classes in Java
Abstract classes in Java programs
programs
Abstract class
• A class that cannot be instantiated and contains one or
more abstract methods is called as Abstract class.
• A class can be instantiated, which means an object can
be created for a class. Such classes are called as
Concrete classes.
• The classes that cannot be instantiated are called as
Abstract classes.
• The abstract class gives a general structure for a class.
• This structure can be implemented by their sub classes
in different ways.
Topic 4
Implement Abstract Classes in Java
Abstract classes in Java programs
programs
Abstract method
Interface
• Interface is a collection of method declarations and
constants that one or more classes of object will use.
• Java does not allow multiple inheritance but the concept
of multiple inheritance can be used using interface.
• It is used to achieve fully abstraction and multiple
inheritance in Java.
Topic 4
Apply Interface in Java programs Interface in Java programs