0% found this document useful (0 votes)
4 views

CSE Module 2

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CSE Module 2

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Polymorphism, Packages and Interfaces

Dr. S Sudheer Mangalampalli


Assistant Professor, Senior Grade1
School of Computer Science and Engineering
VIT-AP University

April 27, 2022

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
1 / 76
Polymorphism in java

Polymorphism in Java is a concept by which we can perform a single


action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs.
The word ”poly” means many and ”morphs” means forms. So
polymorphism means many forms.
There are two types of polymorphism in Java: compile-time
polymorphism and runtime polymorphism
We can perform polymorphism in java by method overloading and
method overriding.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
2 / 76
Method Overloading in Java

If a class has multiple methods having same name but different in


parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the
methods increases the readability of the program.
Suppose, you have to perform the addition of given numbers but
there can be any number of arguments.
In order to accomplish the task, you can create two methods
sum2num(int, int) and sum3num(int, int, int) for two and three
parameters respectively.
other programmers, as well as you in the future may get confused as
the behavior of both methods are the same but they differ by name.
The better way to accomplish this task is by overloading methods.
Depending upon the argument passed, one of the overloaded methods
is called.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
3 / 76
Method Overloading in Java

Advantage of Method Overloading


Method overloading increases the readability of the program.
Different ways to overload the method
There are two ways to overload the method in java
1 By changing number of arguments.
2 By changing the data type
In Java, Method Overloading is not possible by changing the return
type of the method only.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
4 / 76
Exmple for Method overloading

public c l a s s Sum {
public i n t sum ( i n t x , i n t y )
{
return (x + y );
}
public i n t sum ( i n t x , i n t y , i n t z )
{
return (x + y + z );
}
public d o u b l e sum ( d o u b l e x , d o u b l e y )
{
return (x + y );
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
5 / 76
Example for Method Overloading

p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
Sum s = new Sum ( ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 , 2 0 ) ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 , 2 0 , 3 0 ) ) ;
System . o u t . p r i n t l n ( s . sum ( 1 0 . 5 , 2 0 . 5 ) ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
6 / 76
Method Overloading by changing number of parameters

c l a s s MethodOverloading {
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a ){
System . o u t . p r i n t l n ( ” Arguments : ” + a ) ;
}
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a , i n t b ){
System . o u t . p r i n t l n ( ” Arguments : ” + a + ” and ” + b ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
display (1);
d i s p l a y (1 , 4);
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
7 / 76
Method Overloading by changing datatype of parameters

c l a s s MethodOverloading1 {
p r i v a t e s t a t i c v o i d d i s p l a y ( i n t a ){
System . o u t . p r i n t l n ( ” Got I n t e g e r d a t a . ” ) ;
}
p r i v a t e s t a t i c v o i d d i s p l a y ( S t r i n g a ){
System . o u t . p r i n t l n ( ” Got S t r i n g o b j e c t . ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
display (1);
d i s p l a y (” Hello ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
8 / 76
Method Overloading is not possible by changing the return type of
method.
c l a s s Adder {
s t a t i c i n t add ( i n t a , i n t b ) { r e t u r n a+b ; }
s t a t i c d o u b l e add ( i n t a , i n t b ) { r e t u r n a+b ; }
}
c l a s s TestOverloading3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( Adder . add ( 1 1 , 1 1 ) ) ; / / a m b i g u i t y
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
AprilVIT-AP
27, 2022University
9 / 76
Overloading of main method in Java
Can we overload java main() method?
Yes, by method overloading. You can have any number of main methods
in a class by method overloading.
But JVM calls main() method which receives string array as arguments
only.
c l a s s TestOverloading4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( ” main w i t h S t r i n g [ ] ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s ) {
System . o u t . p r i n t l n ( ” main w i t h S t r i n g ” ) ;
}
p u b l i c s t a t i c v o i d main ( ) {
System . o u t . p r i n t l n ( ” main w i t h o u t a r g s ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
10 / 76
Overloading of static methods in Java
We can have two or more static methods with the same name, but
differences in input parameters.
p u b l i c c l a s s Test {
p u b l i c s t a t i c void foo () {
System . o u t . p r i n t l n ( ” T e s t . f o o ( ) c a l l e d ” ) ;
}
p u b l i c s t a t i c void foo ( i n t a ) {
System . o u t . p r i n t l n ( ” T e s t . f o o ( i n t ) c a l l e d ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
foo ( ) ;
foo (10);
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
11 / 76
Can we overload methods that differ only by static
keyword?

We cannot overload two methods in Java if they differ only by static


keyword.
public class test2 {
p u b l i c s t a t i c void foo () {
System . o u t . p r i n t l n ( ” T e s t . f o o ( ) c a l l e d ” ) ;
}
public void foo () {
System . o u t . p r i n t l n ( ” T e s t . f o o ( i n t ) c a l l e d ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
Test . foo ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
12 / 76
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.
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 Method Overriding
1 The method must have the same name as in the parent class
2 The method must have the same parameter as in the parent class.
3 There must be an IS-A relationship (inheritance).

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
13 / 76
Example for Method Overriding

class Vehicle {
void run ( )
{
System . o u t . p r i n t l n ( ” V e h i c l e i s r u n n i n g ” ) ;
}
}
c l a s s Bike2 extends V e h i c l e {
void run (){
System . o u t . p r i n t l n ( ” B i k e i s r u n n i n g s a f e l y ” ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
B i k e 2 o b j = new B i k e 2 ( ) ;
obj . run ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
14 / 76
Example for Method Overriding

c l a s s Parent {
v o i d show ( )
{
System . o u t . p r i n t l n ( ” P a r e n t ’ s show ( ) ” ) ;
}
}
c l a s s Child extends Parent {
v o i d show ( )
{
System . o u t . p r i n t l n ( ” C h i l d ’ s show ( ) ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
15 / 76
Example for Method Overriding contd..
c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
// I f a P a r e n t t y p e r e f e r e n c e r e f e r s
// t o a P a r e n t o b j e c t , t h e n P a r e n t ’ s
// show i s c a l l e d
P a r e n t o b j 1 = new P a r e n t ( ) ;
o b j 1 . show ( ) ;
// I f a P a r e n t t y p e r e f e r e n c e r e f e r s
// t o a C h i l d o b j e c t C h i l d ’ s show ( )
// i s c a l l e d . T h i s i s c a l l e d RUN TIME
// POLYMORPHISM .
P a r e n t o b j 2 = new C h i l d ( ) ;
o b j 2 . show ( ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
16 / 76
Static and Dynamic Binding in Java
Association of method call to the method body is known as binding.
1

There are two types of binding: Static Binding that happens at


2

compile time and Dynamic Binding that happens at runtime.


What is reference and Object
c l a s s Human{
}
c l a s s Boy e x t e n d s Human{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
/∗ T h i s s t a t e m e n t s i m p l y c r e a t e s an o b j e c t o f c l a s s
∗Boy and a s s i g n s a r e f e r e n c e o f Boy t o i t ∗/
Boy o b j 1 = new Boy ( ) ;
/∗ S i n c e Boy e x t e n d s Human c l a s s . The o b j e c t c r e a t i o n
∗ can be done i n t h i s way . P a r e n t c l a s s r e f e r e n c e
∗ can h ave c h i l d c l a s s r e f e r e n c e a s s i g n e d t o i t ∗/
Human o b j 2 = new Boy ( ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
17 / 76
Static Binding

The binding which can be resolved at compile time by compiler is


known as static or early binding.
The binding of static, private and final methods is compile-time.
Why?
The reason is that the these method cannot be overridden and the
type of the class is determined at the compile time.

c l a s s Person
{
p u b l i c s t a t i c void speak ()
{
System . o u t . p r i n t l n ( ” P e r s o n s p e a k s ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
18 / 76
Static Binding
c l a s s Teacher extends Person
{
p u b l i c s t a t i c void speak ()
{
System . o u t . p r i n t l n ( ” T e a c h e r s p e a k s ” ) ;
}
}
public class StaticBinding
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
P e r s o n o b j = new T e a c h e r ( ) ;
obj . speak ( ) ;
P e r s o n o b j 2 = new P e r s o n ( ) ;
obj2 . speak ( ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
19 / 76
Static Binding

From the above code, we got the same output from the parent class. This
happened because:
The reference for the parent class and the child class is the
same(Person). That is, a single object refers to both of them.
Since the method is static, the compiler is aware that this method
can not be overridden in the child class and it knows which method
to call.
Therefore there is no ambiguity and the output is the same for both
cases.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
20 / 76
Dynamic Binding

c l a s s Person
{
p u b l i c void speak ()
{
System . o u t . p r i n t l n ( ” P e r s o n s p e a k s ” ) ;
}
}
c l a s s Teacher extends Person
{
p u b l i c void speak ()
{
System . o u t . p r i n t l n ( ” T e a c h e r s p e a k s ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
21 / 76
Dynamic Binding

p u b l i c c l a s s DynamicBinding
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
P e r s o n o b j 2 = new P e r s o n ( ) ;
obj2 . speak ( ) ;
P e r s o n o b j = new T e a c h e r ( ) ;
obj . speak ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
22 / 76
Dynamic Binding

From the above code, we got different output because:


We have not declared the methods as static in the code.
During compilation, the compiler has no idea of which method to call.
This happens because the compiler doesn’t go according to the type
of the object but it checks only according to the reference variable.
Therefore the binding gets delayed to runtime, so the respective
version of the speak() method will be called on the basis of the type
of the object.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
23 / 76
Abstraction

The extent to which a module hides its internal data and other
implementation details from the other modules is the most important
factor that distinguishes a well-designed Object-Oriented module from
other modules.
A well-designed module hides all of its implementation details and
cleanly separates its interface from its implementation.
These modules then communicate with each other only through the
interfaces.This concept is supported with the help of Abstraction in
Java.
The meaning of the word “Abstraction”, in general words, is the
process of working with ideas rather than their implementation.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
24 / 76
Abstraction contd..

For example, consider the example of an email, the user does not
know about the complex details such as what happens just after
sending an email, which protocol is used by the server to send the
message.
Therefore, we just need to mention the address of the receiver, type
the content and click the send button.
This is basically called Abstraction in which the complex details are
being hidden from the users.
Similarly, in Object-oriented programming, abstraction is a process of
providing functionality to the users by hiding its implementation
details from them.
In other words, the user will have just the knowledge of what an
entity is doing instead of its internal working.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
25 / 76
Advantages of Abstraction

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
26 / 76
Abstraction contd..

An Abstraction is a process of exposing all the necessary details and


hiding the rest.
Abstraction defines an object in terms of its properties (attributes),
behavior (methods), and interfaces (means of communicating with
other objects).
Abstraction can be seen as the technique of filtering out the
unnecessary details of an object so that there remain only the useful
characteristics that define it.
Abstraction focuses on the perceived behavior of the entity. It
provides an external view of the entity.
Real-life Example for Java Abstraction If we want to process something
from the real world, we have to extract the essential characteristics of that
object.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
27 / 76
Real world example for Abstraction

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
28 / 76
How to Achieve Abstraction in Java?

In Java, we can achieve Data Abstraction using Abstract classes and


interfaces.
Interfaces allow 100 percent abstraction (complete abstraction).
Interfaces allow you to abstract the implementation completely.
Abstract classes allow 0 to 100 percent abstraction (partial to
complete abstraction) because abstract classes can contain concrete
methods that have the implementation which results in a partial
abstraction.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
29 / 76
Abstract classes in Java

An Abstract class is a class whose objects can’t be created. An


Abstract class is created through the use of the abstract keyword. It
is used to represent a concept.
An abstract class can have abstract methods (methods without body)
as well as non-abstract methods or concrete methods (methods with
the body). A non-abstract class cannot have abstract methods.
The class has to be declared as abstract if it contains at least one
abstract method.
An abstract class does not allow you to create objects of its type. In
this case, we can only use the objects of its subclass.
Using an abstract class, we can achieve 0 to 100percent abstraction.
The abstract class can also contain final and static methods.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
30 / 76
Abstract methods in Java

Abstract methods are methods with no implementation and without a


method body. They do not contain any method statement.
An abstract method is declared with an abstract keyword.
The declaration of an abstract method must end with a semicolon ;
The child classes which inherit the abstract class must provide the
implementation of these inherited abstract methods.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
31 / 76
Syntax of declaring an abstract classes and Methods

Syntax of declaring an abstract class


a b s t r a c t c l a s s ClassName
{
// c l a s s body
}
Syntax of declaring an abstract method
a c c e s s − s p e c i f i e r a b s t r a c t r e t u r n −t y p e method−name ( ) ;

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
32 / 76
Example for Abstraction

abstract c l a s s BaseClass
{
a b s t r a c t p u b l i c v o i d show1 ( ) ;
p u b l i c v o i d show2 ( )
{
System . o u t . p r i n t l n ( ” C o n c r e t e method ” ) ; }
}
c l a s s ChildClass extends BaseClass
{
p u b l i c v o i d show1 ( )
{
System . o u t . p r i n t l n ( ” show1 ” ) ;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
33 / 76
Example for Abstraction

p u b l i c v o i d show2 ( )
{
System . o u t . p r i n t l n ( ” show2 ” ) ; }
}
p u b l i c c l a s s AbstractionDemo
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
C h i l d C l a s s o b j = new C h i l d C l a s s ( ) ;
o b j . show1 ( ) ;
o b j . show2 ( ) ;
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
34 / 76
Example for Abstraction

abstract class ParentClass


{
abstract public void showDetails ( ) ;
}
c l a s s ChildClass extends ParentClass
{
public void showDetails ()
{
System . o u t . p r i n t ( ” A b s t r a c t method ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
35 / 76
Example for Abstraction

p u b l i c c l a s s AbstractionDemo1
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
P a r e n t C l a s s o b j = new P a r e n t C l a s s ( ) ;
obj . showDetails ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
36 / 76
Interfaces in Java

1 An interface in Java is a blueprint of a class. It has static constants


and abstract methods.
2 The interface in Java is a mechanism to achieve abstraction.
3 There can be only abstract methods in the Java interface, not
method body.
4 It is used to achieve abstraction and multiple inheritance in Java.
5 In other words, you can say that interfaces can have abstract methods
and variables. It cannot have a method body.
6 It cannot be instantiated just like the abstract class.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
37 / 76
Why use Java Interface?

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
38 / 76
How to declare an interface?

An interface is declared by using the interface keyword.


It provides total abstraction; means all the methods in an interface
are declared with the empty body, and all the fields are public, static
and final by default.
A class that implements an interface must implement all the methods
declared in the interface.

i n t e r f a c e <i n t e r f a c e n a m e >{
// d e c l a r e c o n s t a n t f i e l d s
// d e c l a r e methods t h a t a b s t r a c t
// by d e f a u l t .
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
39 / 76
Internal addition by the compiler

The Java compiler adds public and abstract keywords before the
interface method.
Moreover, it adds public, static and final keywords before data
members.
In other words, Interface fields are public, static and final by default,
and the methods are public and abstract.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
40 / 76
The relationship between classes and interfaces

As shown in the figure given below, a class extends another class,


an interface extends another interface, but a class implements an
interface.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
41 / 76
Java Interface Example

interface printable{
void print ( ) ;
}
c l a s s A6 i m p l e m e n t s p r i n t a b l e {
p u b l i c v o i d p r i n t ( ) { System . o u t . p r i n t l n ( ” H e l l o ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A6 o b j = new A6 ( ) ;
obj . p r i n t ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
42 / 76
Java Interface Example

i n t e r f a c e Drawable {
v o i d draw ( ) ;
}
c l a s s R e c t a n g l e i m p l e m e n t s Drawable {
p u b l i c v o i d draw ( ) {
System . o u t . p r i n t l n ( ” d r a w i n g r e c t a n g l e ” ) ; }
}
c l a s s C i r c l e i m p l e m e n t s Drawable {
p u b l i c v o i d draw ( ) {
System . o u t . p r i n t l n ( ” d r a w i n g c i r c l e ” ) ; }
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
43 / 76
class TestInterface1 {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
C i r c l e d=new C i r c l e ( ) ;
d . draw ( ) ;
R e c t a n g l e d1 = new R e c t a n g l e ( ) ;
d1 . draw ( ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
44 / 76
Multiple Inheritance in Java by Interface

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
45 / 76
Multiple Inheritance in Java by Interface

interface Printable {
void print ( ) ;
}
i n t e r f a c e Showable {
v o i d show ( ) ;
}
c l a s s A7 i m p l e m e n t s P r i n t a b l e , Showable {
p u b l i c v o i d p r i n t ( ) { System . o u t . p r i n t l n ( ” H e l l o ” ) ; }
p u b l i c v o i d show ( ) { System . o u t . p r i n t l n ( ” Welcome ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
A7 o b j = new A7 ( ) ;
obj . p r i n t ( ) ;
o b j . show ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
46 / 76
Interface can extends another interface
A class implements an interface, but one interface extends another
interface.
interface Printable {
void print ( ) ;
}
i n t e r f a c e Showable e x t e n d s P r i n t a b l e {
v o i d show ( ) ;
}
c l a s s T e s t I n t e r f a c e 4 i m p l e m e n t s Showable {
p u b l i c v o i d p r i n t ( ) { System . o u t . p r i n t l n ( ” H e l l o ” ) ; }
p u b l i c v o i d show ( ) { System . o u t . p r i n t l n ( ” Welcome ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
T e s t I n t e r f a c e 4 o b j = new T e s t I n t e r f a c e 4 ( ) ;
obj . p r i n t ( ) ;
o b j . show ( ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
47 / 76
Java Packages

Packages are used in Java in order to prevent naming conflicts, to


control access, to make searching/locating and usage of classes,
interfaces easier.
A Package can be defined as a grouping of related types (classes,
interfaces) providing access protection and namespace management.
Some of the existing packages in Java are
java.lang: bundles the fundamental classes
java.io: classes for input , output functions are bundled in this package
Programmers can define their own packages to bundle group of
classes/interfaces, etc.
It is a good practice to group related classes implemented by you so
that a programmer can easily determine that the classes, interfaces
are related.
Using packages, it is easier to provide access control and it is also
easier to locate the related classes.
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
48 / 76
Creating a package

While creating a package, you should choose a name for the package
and include a package statement along with that name at the top of
every source file that contains the classes, interfaces, enumerations,
and annotation types that you want to include in the package.
The package statement should be the first line in the source file.
There can be only one package statement in each source file, and it
applies to all types in the file.
If a package statement is not used then the class, interfaces,
enumerations, and annotation types will be placed in the current
default package.
To compile the Java programs with package statements, you have to use
-d option as shown below.
j a v a c −d D e s t i n a t i o n f o l d e r f i l e n a m e . j a v a

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
49 / 76
Creating package and Example for a Package
Then a folder with the given package name is created in the specified des-
tination, and the compiled class files will be placed in that folder.
Let us look at an example that creates a package called animals. It is a
good practice to use names of packages with lower case letters to avoid any
conflicts with the names of classes and interfaces.
/∗ F i l e name : Animal . j a v a ∗/
package animals ;
i n t e r f a c e Animal {
public void eat ( ) ;
public void t r a v e l ( ) ;
}
Now, let us implement the above interface in the same package animals
package animals ;
/∗ F i l e name : MammalInt . j a v a ∗/
p u b l i c c l a s s MammalInt i m p l e m e n t s Animal {
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
50 / 76
Example for a Package contd..

public void eat () {


System . o u t . p r i n t l n ( ” Mammal e a t s ” ) ;
}
public void t r a v e l () {
System . o u t . p r i n t l n ( ” Mammal t r a v e l s ” ) ;
}
p u b l i c i n t noOfLegs ( ) {
return 0;
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
MammalInt m = new MammalInt ( ) ;
m. e a t ( ) ;
m. t r a v e l ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
51 / 76
Example for a Package contd..

j a v a c −d . Animal . j a v a
j a v a c −d . MammalInt . j a v a

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
52 / 76
Example for a Package contd..

To run a Java Package


j a v a a n i m a l s . MammalInt

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
53 / 76
Types of Packages

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
54 / 76
Built-in Packages

Built-in packages
These packages consist of a large number 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 classed which defines
primitive data types, math operations). This package is automatically
imported.
2 java.io: Contains classed for supporting input / output operations.
3 java.util: Contains utility classes which implement data structures like
Linked List, Dictionary and support ; for Date / Time operations.
4 java.applet: Contains classes for creating Applets.
5 java.awt: Contain classes for implementing the components for
graphical user interfaces (like button ,menus etc).
6 java.net: Contain classes for supporting networking operations.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
55 / 76
User defined Packages

p a c k a g e myPackage ;
p u b l i c c l a s s MyClass
{
p u b l i c v o i d getNames ( S t r i n g s )
{
System . o u t . p r i n t l n ( s ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
56 / 76
importing MyClass from myPackage
i m p o r t myPackage . MyClass ;

p u b l i c c l a s s PrintName
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
// I n i t i a l i z i n g t h e S t r i n g v a r i a b l e
// w i t h a v a l u e
S t r i n g name = ” S u d h e e r ” ;

// C r e a t i n g an i n s t a n c e o f c l a s s MyClass i n
// t h e p a c k a g e .
MyClass o b j = new MyClass ( ) ;

o b j . getNames ( name ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
57 / 76
Illustration of user-defined packages
Creating our first package: File name – ClassOne.java
p a c k a g e package name ;
p u b l i c c l a s s ClassOne {
p u b l i c v o i d methodClassOne ( ) {
System . o u t . p r i n t l n ( ” H e l l o t h e r e i t s C l a s s O n e ” ) ;
}
}
Creating our second package: File name – ClassTwo.java
package package one ;
p u b l i c c l a s s ClassTwo {
p u b l i c v o i d methodClassTwo ( ) {
System . o u t . p r i n t l n ( ” H e l l o t h e r e i am ClassTwo ” ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
58 / 76
Making use of both the created packages: File name – Testing.java
i m p o r t p a c k a g e o n e . ClassTwo ;
i m p o r t package name . C l a s s O n e ;

public c l a s s Testing {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
ClassTwo a = new ClassTwo ( ) ;
C l a s s O n e b = new C l a s s O n e ( ) ;
a . methodClassTwo ( ) ;
b . methodClassOne ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
59 / 76
Object class in Java

The Object class is the parent class of all the classes in java by
default. In other words, it is the topmost class of java.
The Object class is beneficial if you want to refer any object whose
type you don’t know.
Let’s take an example, there is getObject() method that returns an
object but it can be of any type like Employee,Student etc, we can
use Object class reference to refer that object.

O b j e c t o b j=g e t O b j e c t ( ) ;
//we don ’ t know what o b j e c t w i l l be r e t u r n e d from t h i s

The Object class provides some common behaviors to all the objects
such as object can be compared, object can be cloned, object can be
notified etc.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
60 / 76
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
61 / 76
Object Cloning in Java

The object cloning is a way to create exact copy of an object.


The clone() method of Object class is used to clone an object.
The java.lang.Cloneable interface must be implemented by the class
whose object clone we want to create.
If we don’t implement Cloneable interface, clone() method generates
CloneNotSupportedException.
The clone() method is defined in the Object class.

protected Object clone () throws CloneNotSupportedExcep

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
62 / 76
Why use clone() method ?

The clone() method saves the extra processing task for creating the
exact copy of an object.
If we perform it by using the new keyword, it will take a lot of
processing time to be performed that is why we use object cloning.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
63 / 76
Example for Object Cloning

c l a s s Student18 implements Cloneable {


int rollno ;
S t r i n g name ;
S t u d e n t 1 8 ( i n t r o l l n o , S t r i n g name ) {
t h i s . r o l l n o=r o l l n o ;
t h i s . name=name ; }
protected Object clone () throws CloneNotSupportedExcept
return super . clone ();}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) t h r o w s C l o n e N o t S
S t u d e n t 1 8 s 1=new S t u d e n t 1 8 ( 1 0 1 , ” a m i t ” ) ;
S t u d e n t 1 8 s 2 =( S t u d e n t 1 8 ) s 1 . c l o n e ( ) ;
System . o u t . p r i n t l n ( s 1 . r o l l n o +” ”+s 1 . name ) ;
System . o u t . p r i n t l n ( s 2 . r o l l n o +” ”+s 2 . name ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
64 / 76
finalize() method

This method is called just before an object is garbage collected.


It is called the Garbage Collector on an object when the garbage
collector determines that there are no more references to the object.
We should override finalize() method to dispose of system resources,
perform clean-up activities and minimize memory leaks.
For example, before destroying Servlet objects web container, always
called finalize method to perform clean-up activities of the session.

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
65 / 76
Example for finalize() method

p u b l i c c l a s s Te {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Te t = new Te ( ) ;
System . o u t . p r i n t l n ( t . hashCode ( ) ) ;
t = null ;
// c a l l i n g g a r b a g e c o l l e c t o r
System . gc ( ) ;
System . o u t . p r i n t l n ( ” end ” ) ; }
protected void f i n a l i z e ()
{
System . o u t . p r i n t l n ( ” f i n a l i z e method c a l l e d ” ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
66 / 76
Inner Classes
In Java, it is also possible to nest classes (a class within a class).
The purpose of nested classes is to group classes that belong
together, which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and then
create an object of the inner class
c l a s s OuterClass {
i n t x = 10;
class InnerClass {
int y = 5;
}}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s myOuter = new O u t e r C l a s s ( ) ;
O u t e r C l a s s . I n n e r C l a s s myIn = myOuter . new I n n e r C l a s s ( ) ;
System . o u t . p r i n t l n ( myIn . y + myOuter . x ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
67 / 76
private Inner classes

Unlike a regular class, an inner class can be private or protected. If you


don’t want outside objects to access the inner class, declare the class as
private:
c l a s s OuterClass {
i n t x = 10;
private class InnerClass {
int y = 5;
}}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s myOuter = new O u t e r C l a s s ( ) ;
O u t e r C l a s s . I n n e r C l a s s myIn = myOuter . new I n n e r C l a s s ( ) ;
System . o u t . p r i n t l n ( myIn . y + myOuter . x ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
68 / 76
Static Inner class

An inner class can also be static, which means that you can access it
without creating an object of the outer class:
c l a s s OuterClass {
i n t x = 10;
static class InnerClass {
i n t y = 5;}}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s . I n n e r C l a s s m y I n n e r = new O u t e r C l a s s . I n n e r C l
System . o u t . p r i n t l n ( m y I n n e r . y ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
69 / 76
Access outer class from Inner Class

c l a s s OuterClass {
i n t x = 10;
class InnerClass {
p u b l i c i n t myInnerMethod ( ) {
return x ;
} }}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
O u t e r C l a s s myOuter = new O u t e r C l a s s ( ) ;
O u t e r C l a s s . I n n e r C l a s s m y I n n e r = myOuter . new I n n e r C l a s s
System . o u t . p r i n t l n ( m y I n n e r . myInnerMethod ( ) ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
70 / 76
Examples for Inner Class

c l a s s Outer Demo {
i n t num ;
// i n n e r c l a s s
p r i v a t e c l a s s Inner Demo {
public void print () {
System . o u t . p r i n t l n ( ” T h i s i s an i n n e r c l a s s ” ) ;
} }
// A c c e s s i n g t h e i n n e r c l a s s from t h e method w i t h i n
void d i s p l a y I n n e r () {
Inner Demo i n n e r = new Inner Demo ( ) ;
inner . print ();
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
71 / 76
public c l a s s My class {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
// I n s t a n t i a t i n g t h e o u t e r c l a s s
Outer Demo o u t e r = new Outer Demo ( ) ;
// A c c e s s i n g t h e d i s p l a y I n n e r ( ) method .
outer . display Inner ( ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
72 / 76
Accessing Private members from Inner class

c l a s s Outer Demo {
// p r i v a t e v a r i a b l e o f t h e o u t e r c l a s s
p r i v a t e i n t num = 1 7 5 ;
// i n n e r c l a s s
p u b l i c c l a s s Inner Demo {
p u b l i c i n t getNum ( ) {
System . o u t . p r i n t l n ( ” T h i s i s t h e getnum method o f t h
r e t u r n num ;
} }}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
73 / 76
public c l a s s My class2 {
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
// I n s t a n t i a t i n g t h e o u t e r c l a s s
Outer Demo o u t e r = new Outer Demo ( ) ;
// I n s t a n t i a t i n g t h e i n n e r c l a s s
Outer Demo . Inner Demo i n n e r = o u t e r . new Inner Demo ( ) ;
System . o u t . p r i n t l n ( i n n e r . getNum ( ) ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
74 / 76
Example for Method Local Inner class

public class Outerclass {


// i n s t a n c e method o f t h e o u t e r c l a s s
v o i d my Method ( ) {
i n t num = 2 3 ;
// method−l o c a l i n n e r c l a s s
c l a s s MethodInner Demo {
public void print () {
System . o u t . p r i n t l n ( ” T h i s i s method i n n e r c l a s s ”+num )
}
} // end o f i n n e r c l a s s

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
75 / 76
// A c c e s s i n g t h e i n n e r c l a s s
MethodInner Demo i n n e r = new MethodInner Demo ( ) ;
inner . print ();
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
O u t e r c l a s s o u t e r = new O u t e r c l a s s ( ) ;
o u t e r . my Method ( ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor,


Polymorphism,
Senior Grade1
Packages
School and
of Computer
InterfacesScience and Engineering
April 27,
VIT-AP
2022 University
76 / 76

You might also like