You are on page 1of 30

OBJECT ORIENTED

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

The advantages of Inheritance are:


1. The variables and methods that are common to two or
more classes can be defined in one class and used in
other classes. This is called as code reusability.
2. When there is any modification in the common
information, it will apply to all the inherited classes.
3. Inheritance avoids code redundancy.
4. The sub class can access protected data from super
class.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

There are 3 types of Inheritance. They are:


1. Single Inheritance
2. Multiple Inheritance (Java doesn’t support)
3. Multi-Level Inheritance
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

Single inheritance class A


{
-------
Super -------
Class A class
}

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.

Class B Class C Class D


class A class C extends A
{ {
------- -------
------- -------
} }
class B extends A class D extends A
{ {
------- -------
------- -------
} }
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

Multiple inheritance (Java doesn’t support)


• When a class is inherited from more than one
existing class, then it is Multiple inheritance.
• The inherited class is a sub class and all the
existing classes from which the sub class is created
are super classes.
• Java supports Multiple Inheritance through a
concept called Interface.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

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

Open file: Class C


Sample35c.java Sub
Compile and run.
class
Observe the
output.
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

Multi-Level Inheritance - Code

class A class B extends A class C extends B


{ { {
------- ------- -------
-------
------- -------
}
} }
Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

Super class
• A new class can be inherited from an existing class.

• The existing class from which a new class is


created is known as the Super class.

• It is also called as Base class.


Topic 4
Apply Inheritance in Java programs Inheritance in Java programs

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>
{
}

Creating sub class : Example


class Cylinder extends Sphere
{
}

• 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

Accessing objects of super class


• An object of the subclass can access the variables and
methods of the super class using the dot operator.
• Syntax for accessing a super class variable and method:

Object.variable
Object.method()
Topic 4
Apply Inheritance in Java programs Keyword super

Super class and sub class


• When an object of a super class is created, it has
only the variables declared in its own class.
• When an object of a subclass is created, the
subclass object has a copy of the variables defined
in its super class along with the variables of its own
class.

Open file: Sample36a.java. Compile and run. Observe the output.


Topic 4
Implement Polymorphism in Java
Overriding method in Java programs
programs

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

Open file: Sample37.java. Compile and run. Observe the output.

Open file: Sample38.java. Compile and run. Observe the output.

Open file: Sample39.java. Compile and run. Observe the output.


Topic 4
Implement Polymorphism in Java
Polymorphism in Java programs
programs

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

• Consider a situation, where all the sub classes of a


class must implement a method of its super class.
• In such cases, you can declare the super class and
the method in the super class as abstract.
• Now, a compiler error occurs if you forget to
implement all the abstract methods of the super
class in your sub class.
Topic 4
Implement Abstract Classes in Java
Abstract classes in Java programs
programs

Abstract method

abstract class Arithmetic


{
int a,b;
abstract void calc();
}
Topic 4
Implement Abstract Classes in Java
Abstract classes in Java programs
programs

abstract class Arithmetic {


int a,b;
abstract void calc();
//Abstract method in an Abstract class arithmetic
}

class Add extends Arithmetic{


void calc(){
System.out.println(“The sum is “ + (a+b));
//Abstract method implemented in a subclass for adding 2 numbers
}}
Topic 4
Implement Abstract Classes in Java
Abstract classes in Java programs
programs

class Sub extends Arithmetic


{
void calc()
{
System.out.println(“The difference is “ + (a-b));
//Abstract method implemented in a subclass for subtracting 2
numbers
}
}

Open file: Sample40.java. Compile and run. Observe the output.


Topic 4
Apply Interface in Java programs Interface in Java programs

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

An interface is similar to a class in the following ways:


• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with
the name of the interface matching the name of the file.
• The byte code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding byte
code file must be in a directory structure that matches the
package name
Topic 4
Apply Interface in Java programs Interface in Java programs

However, an interface is different from a class in several ways,


including:
• You cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields
that can appear in an interface must be declared both static
and final.
• An interface is not extended by a class; it is implemented by
a class.
• An interface can extend multiple interfaces.
Topic 4
Apply Interface in Java programs Interface in Java programs

Relationship between classes and interfaces

Open file: Sample41.java. Compile and run. Observe the output.

You might also like