Unit_3 Inheritence and Polymorphism KVN
Unit_3 Inheritence and Polymorphism KVN
Unit-3
Inheritance and Polymorphism
Inheritance is a mechanism in which one object acquires all the properties and
behaviors of a parent class. The new classes are created from the existing classes.
Inheritance represents a parent-child relationship.
“The process of deriving a new class from an existing class is called inheritance”. The
existing class is called super class or base class or parent class and the new class
is called sub class or derived class or child class. A child class contains all the
members (fields, methods and nested methods) of its base class and also its own
members.
Code reusability and method overriding (runtime p olymorphism) are the important oop
paradigms achieved through inheritance. .
Syntax of Java Inheritance
The extends keyword indicates that a
class Subclass-name extends Superclass-name new class that is derived from an
existing class. The meaning of
{
"extends" is to increase the
//methods and fields functionality.
}
Note: Java doesn’t directly implement multiple and hybrid inheritance. However, they
are implemented using a secondary inheritance path in the form of interface.
Single Inheritance: A class derived from an existing class. This inheritance consists
of one base class and one derived class.
Syntax: class A extends B The keyword extends signifies that the
{ properties of class A are extended to the class
Variable declaration; B. The sub class B contains its variables and
Method declaration; methods and also that of the super class A.
}
Write a program to illustrate single inheritance.
import java.io.*;
class Parent
{
public void display()
{
System.out.println("TUMKUR");
}
}
}
Hierarchical Inheritance: In hierarchical inheritance there is only one super class
and more than one subclasses.
Base class
A Class A is the base class for both B and C. Class B and
class C are subclasses of class A. Class B contains
characteristics of A and class C contains characteristics
B C of A. In other words B and C inherits the properties of A.
Derived classes
Class Interface
It contains instance variable, constants and It contains only constants and
instance method. abstract methods
It contains design & implementation of Interface contains pure design
code
Class can be instantiated Interface can’t be instantiated.
Class extends Interface implements
Keyword class is used Keyword interface is used
Constructors are used for initialization Constructor never used.
Methods are defined to perform a specific The methods in an interface are
action purely abstract
Types of polymorphism:
3.
4.
5.
6.
7.
8.
9.
10.
Overloading methods:
The process of creating methods having same name, but different parameter
list is called methods overloading. Method overloading is used when objects
are required to perform similar task using different input parameters.
11.
e.g:
class Overloading
{
void sum(int a,long b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
}
Class Example
{
public static void main(String args[])
{ 40
Overloading obj=new Overloading(); 60
obj.sum(20,20);
obj.sum(20,20,20);
}
}
Overriding methods:
By defining a method in the sub class that has the same name, same
arguments with same data type as a method in the super class. When that
method is called, the method defined in the subclass is invoked and executed
instead of super class method. This is known as method overriding.
e.g: class Xyz
{
protected void display()
{
System.out.println(“BSc”);
}
}
class Pqr extends Xyz
{
void display()
{ Here the display() method of child class
System.out.println(“BCA”); overrides the display() method of the base
} class.
}
public class Demo
{ BCA
public static void main(String args[])
{
Pqr r=new Pqr ();
r.display();
}
}
Overloading Overriding
It deals with multiple methods with the It deals with two methods, one in a parent
same name in the same class, but with class and another in child class that have
different argument list. the same name and same arguments.
Define a similar operation in different Define a similar operation in different ways
ways for different parameters. for different object types.
These methods are invoked depending These methods are invoked depending on
on the type of the arguments. the type of the objects.
Inheritance not needed It needs inheritance
Static methods can be overloaded Static methods cannot be overridden
private and final methods can be private and final methods cannot be
overloaded overridden
Packages:
A Package is a collection of related classes and interfaces. It helps to organize
the classes into a folder structure and make it easy to locate and use them.
Packages act like a container for classes and interfaces and grouped according
to their functionality. Packages are similar to folders in computer. As folder can
hold subfolders, a package can have sub packages.
Note:
When present, package must be the first non comment statement in the file.
Java packages can be stored in compressed files called JAR files, allowing
classes to download faster as a group rather than one at a time.
The access specifiers protected and default have access control on package
level. The protected member is accessible by classes within the same
package and its subclasses. The default access specifier is accessible by
classes in the same package.
Advantages of packages:
There are several advantages of using java packages:
Make easy searching or locating of classes and interfaces.
Avoids naming conflicts. If there are two classes with the name Student in
two packages, bca.Student and bsc.Student.
Implement data encapsulation.
Types of Java packages:
Java packages are classified into 2 types,
1. Inbuilt packages or Java API packages
2. User defined packages.
The java.lang package supports language classes. java.lang include classes for
primitive data types, strings, mathematical functions, threads and exceptions.
java.lang is the default package in java.
java.util contains language utility classes like Date, Stack, Calendar etc
java.io contains i/o support classes. They provide facilities for the I/O operation on
data.
java.awt for implementing GUI. They include classes for windows, buttons, list, menu
etc
java.net contains classes for networking. They include classes for communicating with
LAN and internet servers.
java.applet contains classes for creating and implementing applets.
2. import packagename . *;
e.g. : import java . util . *;
This imports all classes present in the util package.
System.out.println("INDIA");
System.out.println("m=”+m);
}
}
import Package1.Abc;
import Package2.*;
DHARANESH
class Test
INDIA
{
m=18
public static void main(String args[])
{
Abc obj1=new Abc();
Xyz obj2=new Xyz ();
obj1.display();
obj2.show();
}
}
Java Lang package: java lang package is automatically imported into all programs,
it contains classes and interfaces that are fundamental to all of java programming. It
is java’s most widely used package. It includes the following classes:
Wrapper classes: Java uses simple data types, such as int and char for performance
reason. These data types are not the part of the object hierarchy. Primitive data types
may be converted into object types by using the wrapper classes, contained in the java.lang
package. The following table shows the simple data type and their corresponding wrapper class types.
Simple type Wrapper classes
boolean Boolean The wrapper classes have a number
of unique methods for handling
char Character
primitive data types and objects.
int Integer
float Float
long Long
double Double
Note: The automatic conversion of primitive data types into its equivalent Wrapper
type is known as auto boxing and opposite operation is known as unboxing. For
example, converting an int to an Integer, a double to a Double and so on.
To import all the classes from util package, the import statement should
be import java.util.* ;
*******