Ch3 JPR
Ch3 JPR
PACKAGE
Marks-12
Inheritance in Java
Mechanism of deriving new class from old class.
Supports “Reusability”
The old class is known as-
Base Class / Super class / Parent Class
The new class is known as-
Derived class/ Sub Class / Child class
Sub class can add its own fields and methods.
Properties of super class are accessible to the
sub class
Inheritance in Java
Types:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
A B
C
Multilevel Inheritance
Multilevel inheritance refers to a mechanism
where one can inherit from a derived class,
thereby making this derived class as base class for
the new class.
In flow diagram, C is subclass or child class of B and
B is a child class of A. A
B
C
Inheritance In Java
Multilevel inheritance -
Syntax:
class A
{
-------
A
-------
}
class B extends A
B
{
------- C
-------
}
class C extends B
{
-------
Multilevel Inheritance example program in Java
class X
{
public void methodX()
{
System.out.println("Class X method");
}
}
class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling own method
}
Hierarchical Inheritance
In such kind of inheritance one class is inherited
by many sub classes.
In below example class B,C and D inherits the
same class A.
A is parent class (or base class) of B,C & D.
B C D
Inheritance In Java
Hierarchical inheritance -
Syntax:
class A
{
-------
A
-------
}
class B extends A
{ B C
-------
-------
}
class C extends A
{
-------
Hierarchical Inheritance example program in Java
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
Class MyClass
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
}
}
Uses of Super keyword
Used by subclass to refer or to access superclass
members
It is a reference variable that is used to refer parent
class
The keyword “super” is always used with the
concept of Inheritance.
Three uses of super Keyword :
Accessing instance variables of super class
Accessing instance methods of super class
Calling super class constructor
Uses of Super keyword
1) Accessing instance variables of super class
Used to access instance variable of super class in subclass
Also used when superclass and subclass has the same name for instance
variable
Syntax:
super.variablename;
2) Accessing instance methods of super class
Used to access instance methods of super class in subclass
Also used in the case of method overriding
Syntax:
super.methodname(para-list);
3) Calling super class constructor
Used to call super class constructor within a subclass constructor.
The call to super must appear as first statement.
Syntax:
class Primary {
int cal; //declaration1
Primary(int a)
{
cal = a;
}
void show()
{
System.out.println("Super class cal : "+cal);
}
}
class Secondary extends Primary
{
int cal; //declaration2
Secondary(int x,int y)
{
super(x); //use of super to call superclass constructor
cal = y;
}
void show()
{
super. show (); //use of super to access super class instance method
System.out.println ("Sub class cal: "+cal);
}
}
class SuperUse
{
public static void main(String args[])
{
Secondary s = new Secondary (15,22);
s.show();
}
}
Method Overloading
Defining two or more methods within the same
class that are having the same name, but their
parameter declarations are different.
the methods are said to be overloaded, and the
process is referred to as method overloading.
implementation of polymorphism
type and/or number of arguments are used to
determine which version of the overloaded method to
actually call
Program on Method Overloading
class Area
{
int area(int side) //area of a square
{
return(side * side);
}
float area(float radius) //area of circle
{
return(3.14f * radius * radius);
}
int area(int len, int wid) //area of rectangle
{
return(len * wid);
}
}
Program on Method Overloading (Continued)
class Shape
{
public static void main(String args[])
{
Area s = new Area();
int x=s.area(10);
System.out.println ("Area of square : "+x);
int y=s.area(10,20);
System.out.println ("Area of rect: "+y);
float z = s.area(5.5f);
System.out.println ("Area of circle : "+z);
}
}
Output:
final class A
{
…
}
Abstract Method
• A method that is declared as abstract and does not
have implementation is known as abstract method.
• Static, final and private methods cannot be abstract.
since they cannot be overridden
• Final class cannot contain abstract methods.
class class_name
{
...
interface nested_interface_name
{
...
}
interface Test
{
interface Message
{
void msg();
}
}
Class NestDemo implements Test.Message
{
public void msg()
{
System.out.println("Hello nested interface");
}
public static void main(String args[])
{
NestDemo nd=new NestDemo ();
nd.msg();
}
Interfaces References
61
in classes.
For eg:
1) java.awt.Color;
awt- package name, Color-class name
2) double x= java.lang.Math.sqrt(a);
lang- package name, math- class name, sqrt-
method name
Creating User defined packages