You are on page 1of 73

NAAC Accredited with ‘A” Grade

Recognised by UGC under sections 2(f) and 12(b) &


Affiliated to Bangalore University

Sub : OOPS with JAVA


Unit : III

Name : Apoorva Narendra


Department : Computer Applications
Designation : Assistant professor

Dept of COMPUTER APPLICATIONS


5/7/2021 1
Session Number
Packages
• Package in Java is a mechanism to encapsulate a group of
classes, sub packages and interfaces.
• Packages are used for:
1. Preventing naming conflicts.
2. Making searching/locating of classes, interfaces easier.
3. Providing controlled access.
• Package names and directory structure are closely related.
For example if a package name is college.staff.cse, then
there are three directories such that cse is present
.
in staff and staff is present college
Dept of COMPUTER APPLICATIONS
5/7/2021 2
Session Number
Packages
• Sub packages: Packages that are inside another package
are the sub packages. These are not imported by default,
they have to imported explicitly. 
import java.util.*;
util is a subpackage created inside java package.
• Consider following two statements :
import java.util.vector;
import java.util.*;
• First Statement is used to import Vector class
from util package.
• Second statement imports all the classes
5/7/2021 from util package.
Dept of COMPUTER APPLICATIONS
3
Session Number
Types of Packages

Dept of COMPUTER APPLICATIONS


5/7/2021 4
Session Number
Built-In Packages
• These packages consist of classes which are a part of
Java API.
• Some of the commonly used built-in packages are:
1. java.lang: Contains language support classes(e.g math
operations).
2. java.io: Contains classed for supporting input / output
operations.
3. java.util: Contains utility classes which implement data
structures, Dictionary and support for Date/Time
operations.
4.  java.applet: Contains classes for creating Applets.
5.
5/7/2021  java.awt: Contain classes for implementing the
Dept of COMPUTER APPLICATIONS
Session Number
5
User-defined Packages

Steps in creating a package :


1. Choose the name of the package
2. Include the package command as the first line of code in
your Java Source File.
3. The Source file contains the classes, interfaces etc you
want to include in the package
4. Compile to create the Java packages

Dept of COMPUTER APPLICATIONS


5/7/2021 6
Session Number
importing the package

Dept of COMPUTER APPLICATIONS


5/7/2021 7
Session Number
Nested/sub package

Dept of COMPUTER APPLICATIONS


5/7/2021 8
Session Number
Extending an imported class

Dept of COMPUTER APPLICATIONS


5/7/2021 9
Session Number
Extending an imported class

Dept of COMPUTER APPLICATIONS


5/7/2021 10
Session Number
Importing interface

Dept of COMPUTER APPLICATIONS


5/7/2021 11
Session Number
Static importing
Static import allows you to access the member of a class
directly without using the fully qualified name.

Dept of COMPUTER APPLICATIONS


5/7/2021 12
Session Number
Static importing

Dept of COMPUTER APPLICATIONS


5/7/2021 13
Session Number
Access Control
1. Private: The access level of a private modifier is only within
the class. It cannot be accessed from outside the class.

2. Default: The access level of a default modifier is only within


the package. It cannot be accessed from outside the package.
If you do not specify any access level, it will be the default.

3. Protected: The access level of a protected modifier is


within the package and outside the package through child
class. If you do not make the child class, it cannot be
accessed from outside the package.
Dept of COMPUTER APPLICATIONS
5/7/2021 14
Session Number
Access Control

4. Public: The access level of a public modifier is


everywhere. It can be accessed from within the class,
outside the class, within the package and outside the
package.

Dept of COMPUTER APPLICATIONS


5/7/2021 15
Session Number
Access Control

Dept of COMPUTER APPLICATIONS


5/7/2021 16
Session Number
Arrays
• An array is a collection of similar type of elements which
has contiguous memory location.
• Array in Java is index-based, the first element of the array
is stored at the 0th index, 2nd element is stored on 1st
index and so on.
• There are two types of array :
1. Single Dimensional Array
2. Multidimensional Array

Dept of COMPUTER APPLICATIONS


5/7/2021 17
Session Number
1. Single dimension Array

Dept of COMPUTER APPLICATIONS


5/7/2021 18
Session Number
1. Single dimension Array

Dept of COMPUTER APPLICATIONS


5/7/2021 19
Session Number
1. Single dimension Array

Dept of COMPUTER APPLICATIONS


5/7/2021 20
Session Number
Passing array to method

Dept of COMPUTER APPLICATIONS


5/7/2021 21
Session Number
2D Array

Dept of COMPUTER APPLICATIONS


5/7/2021 22
Session Number
2D Array Example

Dept of COMPUTER APPLICATIONS


5/7/2021 23
Session Number
3D Array
• Three – dimensional array is a complex form of a
multidimensional array. A three – dimensional array can
be seen as an array of two – dimensional array.

Dept of COMPUTER APPLICATIONS


5/7/2021 24
Session Number
3D Array

Dept of COMPUTER APPLICATIONS


5/7/2021 25
Session Number
3D Array Example

Dept of COMPUTER APPLICATIONS


5/7/2021 26
Session Number
Array of objects
• An array is a collection of the same data type .
• Java allows us to store objects in an array.
• In Java, the class is also a user-defined data type.
• An array that contains class type elements are known as
an array of objects.

Dept of COMPUTER APPLICATIONS


5/7/2021 27
Session Number
Example

Dept of COMPUTER APPLICATIONS


5/7/2021 28
Session Number
Dept of COMPUTER APPLICATIONS
5/7/2021 29
Session Number
String Class

Dept of COMPUTER APPLICATIONS


5/7/2021 30
Session Number
String Class

Dept of COMPUTER APPLICATIONS


5/7/2021 31
Session Number
Dept of COMPUTER APPLICATIONS
5/7/2021 32
Session Number
Nesting of methods

Dept of COMPUTER APPLICATIONS


5/7/2021 33
Session Number
Recursion
• Recursion in java is a process
in which a method calls itself
continuously.

Dept of COMPUTER APPLICATIONS


5/7/2021 34
Session Number
Nested Class
1. Java inner class or nested class is a class which is declared
inside the class or interface.
2. Additionally, it can access all the members of outer class
including private data members and methods.
3. Since the inner class exists within the outer class, you must
instantiate the outer class first, in order to instantiate the
inner class.
4. As a member of the outer class, a nested class can be
declared private, public or protected.

 
5/7/2021
Dept of COMPUTER APPLICATIONS
Session Number
35
Advantages of Nested Class
1. Nested classes can access all the members (data members
and methods) of outer class including private.
2. Nested classes are used to develop more readable and
maintainable code because it logically group classes and
interfaces in one place only.
3.  Code Optimization: It requires less code to write.

Dept of COMPUTER APPLICATIONS


5/7/2021 36
Session Number
Classification of Nested Class

Dept of COMPUTER APPLICATIONS


5/7/2021 37
Session Number
1. Inner Class

Dept of COMPUTER APPLICATIONS


5/7/2021 38
Session Number
2. Method local Inner Class
• A class within a method and this will be a local type.
• The scope of inner class is restricted within the method.
• A method-local inner class can be instantiated only within
the method where the inner class is defined. 

Dept of COMPUTER APPLICATIONS


5/7/2021 39
Session Number
2. Method local Inner Class

Dept of COMPUTER APPLICATIONS


5/7/2021 40
Session Number
3. Anonymous Inner Class
• A class that have no name is known as anonymous inner
class in java.
• It should be used if you have to override method of class
or interface.
• Java Anonymous inner class can be created by two ways:
1. Class (may be abstract or concrete).
2. Interface 

Dept of COMPUTER APPLICATIONS


5/7/2021 41
Session Number
3. Anonymous Inner Class

Dept of COMPUTER APPLICATIONS


5/7/2021 42
Session Number
4. Static Nested Class
• A static inner class is a nested class which is a static
member of the outer class.
• It can be accessed without instantiating the outer class.
• It can access static data members of outer class
including private but cannot access non-static data
member or method.
 

Dept of COMPUTER APPLICATIONS


5/7/2021 43
Session Number
4. Static Nested Class

Dept of COMPUTER APPLICATIONS


5/7/2021 44
Session Number
Final Members

• The final keyword in java is used to restrict the user. The


java final keyword can be used in many context. Final can
be:
1. variable
2. method
3. class
 

 
Dept of COMPUTER APPLICATIONS
5/7/2021 45
Session Number
1. Final Variable

• If you make any variable as final,


you cannot change the value of
final variable(It will be constant). 

Dept of COMPUTER APPLICATIONS


5/7/2021 46
Session Number
2. Final Method

• If you make any method as


final, you cannot override it.  

Dept of COMPUTER APPLICATIONS


5/7/2021 47
Session Number
3. Final Class

• If you make any class as


final, you cannot extend it. 

Dept of COMPUTER APPLICATIONS


5/7/2021 48
Session Number
Inheritance
• Inheritance in Java is a mechanism in which one object
acquires all the properties and behaviors of a parent
object.
• The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes.
• When you inherit from an existing class, you can reuse
methods and fields of the parent class.
• We can also add new methods and fields in your current
class also.
• Inheritance represents the IS-A relationship which is also
known as a parent-child  relationship.
 
5/7/2021
Dept of COMPUTER APPLICATIONS
Session Number
49
Inheritance
Important terminologies: 
1. Super Class: The class whose features are inherited is
known as super class(or a base class or a parent class).
2. Sub Class: The class that inherits the other class is known
as subclass(or a derived class, extended class, or child
class
3. Reusability: Inheritance supports the concept of
“reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code
that we want, we can derive our new class from the
existing class. By doing this, we are reusing the fields and
5/7/2021 methods of the existing class.
Dept of COMPUTER APPLICATIONS
50
Session Number
Types of Inheritance

Dept of COMPUTER APPLICATIONS


5/7/2021 51
Session Number
Types of Inheritance

Dept of COMPUTER APPLICATIONS


5/7/2021 52
Session Number
Why multiple Inheritance not
supported in Java

Dept of COMPUTER APPLICATIONS


5/7/2021 53
Session Number
Abstract Class
• A class which is declared as abstract is known as
an abstract class.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructor and static methods also.
• It can have final methods which will force the subclass not
to change the body of the method.
 

 
Dept of COMPUTER APPLICATIONS
5/7/2021 54
Session Number
Abstract Method
A method which is declared as abstract and does not have
implementation is known as an abstract method.

Example :

abstract void printStatus(); //no method body and abstract 
 

Dept of COMPUTER APPLICATIONS


5/7/2021 55
Session Number
Dept of COMPUTER APPLICATIONS
5/7/2021 56
Session Number
Dynamic Method Dispatch
• Method overriding is one of the ways in which Java
supports Runtime Polymorphism.
• Dynamic method dispatch is the mechanism by which a
call to an overridden method is resolved at run time, rather
than compile time.
• When an overridden method is called through a superclass
reference, Java determines which version(superclass/
subclasses) of that method is to be executed based upon
the type of the object being referred.
 

 
Dept of COMPUTER APPLICATIONS
5/7/2021 57
Session Number
Dynamic Method Dispatch
• A superclass reference variable can refer to a subclass
object. This is also known as upcasting.
• Child c = new Child(): The use of this initialization is to
access all the members present in both parent and child
classes, as we are inheriting the properties.
• Parent p = new Child(): This type of initialization is used to
access only the members present in the parent class and
the methods which are overridden in the child class.

 
Dept of COMPUTER APPLICATIONS
 
5/7/2021
Session Number
58
Dynamic Method Dispatch

Dept of COMPUTER APPLICATIONS


5/7/2021 59
Session Number
Object Slicing

Dept of COMPUTER APPLICATIONS


5/7/2021 60
Session Number
Object typecasting
• Typecasting is one of the most important concepts which
basically deals with the conversion of one data type to
another datatype.
• Just like the datatypes, the objects can also be typecasted.
However, in objects, there are only two types of objects (i.
e.) parent object and child object. Therefore, typecasting
of objects basically mean that one type of object (i.e.)
child or parent is typecasted to another.
• There are two types of typecasting :1)Upcasting
2)Downcasting
 
Dept of COMPUTER APPLICATIONS
5/7/2021 61
Session Number
Object typecasting
1. Upcasting: 
• Upcasting is the typecasting of a child object to a parent
object.
• Upcasting gives us the flexibility to access the parent
class members but it is not possible to access all the
child class members except the overridden methods.
2. Downcasting: 
• Downcasting means the typecasting of a parent object to
a child object. 
 
Dept of COMPUTER APPLICATIONS
 
5/7/2021
Session Number
62
Object typecasting

Dept of COMPUTER APPLICATIONS


5/7/2021 63
Session Number
Object typecasting

Dept of COMPUTER APPLICATIONS


5/7/2021 64
Session Number
Interface
• An interface in Java is a blueprint of a class. 
• It is used to achieve abstraction and multiple inheritance
in Java.
• It is a collection of abstract methods. A class implements
an interface, thereby inheriting the abstract methods of
the interface.
• Along with abstract methods, an interface may also
contain constants, default methods, static methods.
• Method bodies exist only for default methods and static
methods.
Dept of COMPUTER APPLICATIONS
5/7/2021 65
Session Number
Interface
• interface <interface_name> { }
• Abstract class can have abstract & non-abstract methods
(partial abstraction) whereas the interface can have only
abstract methods there by achieving full abstraction.
• All the methods in an interface are abstract and all the
fields are public, static and final by default.

Dept of COMPUTER APPLICATIONS


5/7/2021 66
Session Number
Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 67
Session Number
Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 68
Session Number
Multiple Inheritance by Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 69
Session Number
Multiple Inheritance by Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 70
Session Number
Extending Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 71
Session Number
Partial Interface Implementation

Dept of COMPUTER APPLICATIONS


5/7/2021 72
Session Number
Polymorphism through Interface

Dept of COMPUTER APPLICATIONS


5/7/2021 73
Session Number

You might also like