Unit 3
Unit 3
Unit 3
Interfaces
Syllabus
• Inheritance Basic ,Types of inheritance, Member access rules, Usage of
super key word, Method overriding, Using final,
• Abstract classes, Differences between abstract classes and interfaces,
Defining an interface, Implementing interface, Applying interfaces,
Variables in interface and Extending interfaces.
• Defining , creating and accessing a package ,Importing packages ,Access
control in package
Inheritance-Introduction
• Inheritance is an important feature of object oriented programming.
subclass/derivedclass/child class
superclass/baseclass/parentclass.
class P{
protected int a=10;
void dis( ){ System.out.println("this is parent class variable"+a); }
}
class C extends P{
void dis1( ){ System.out.println("this is child class variable"+a); }
}
class Main{
public static void main(String[] args){
C o=new C();
o.dis();
o.a=20;
o.dis1();
}}
Method Overriding
• In method overriding, a base class method is
overridden in the derived class.
• That is, the same method is written with the
same signature as of the base class method but
different implementation.
• In method overloading, arguments and type of
arguments are used to distinguish between two
functions, but in method overriding no such
distinction can be made.
• When a method in a subclass has the same name, same
parameters or signature and same return type(or sub-type)
as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
• Method overriding is one of the way by which java
achieve Run Time Polymorphism
• The version of a method that is executed will be
determined by the object that is used to invoke it.
• This process in which call to the overridden method is
resolved at runtime is known as Dynamic method dispatch
• In method overriding, the signature of the two methods
must match. This is shown in the program given below.
Class A{
void dis(){
System.out.println(“this is class A”);
}}
Class B extends A{
void dis(){
System.out.println(“this is class B”);
}}
Class Demo{
P.S.V.main(String args[]){
B b=new B();
b.dis();
}}
Class A{
void dis(){
System.out.println(“this is class A”);
}}
Class B{
void dis(){
System.out.println(“this is class B”);
}}
Class Demo{
P.S.V.main(String args[]){
B b=new B();
b.dis();
A a=new A();
a.dis();
}}
o/p: this is class B
this is class A
Usage of Java Method Overriding
• 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 Java Method Overriding
• The method must have the same name as in the parent
class
• The method must have the same parameter as in the
parent class.
• There must be an IS-A relationship (inheritance).
Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism by which a
call to an overridden method is resolved at run time,
rather than compile time.
• Binding is the process of linking the function call with the
place where function definition is actually written so that
when a function call is made, it can be ascertained where
the control has to be transferred.
• Binding is also called linking, and is of two types:
1. Static binding
2. Dynamic binding.
1)Static binding :When at compile time, it is known which
function will be called in response to a function call, the
binding is said to be static binding, compile time binding or
early binding
2)Dynamic Binding:At run time, the decision is taken as to which
function is to be called in response to a function call. This type
of binding is known as late binding, runtime binding or
dynamic binding.
• 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 to
at the time the call occurs. Thus, this determination is made
at run time.
• At run-time, it depends on the type of the object being
referred to (not the type of the reference variable) that
determines which version of an overridden method will be
executed
• A superclass reference variable can refer to a subclass object.
This is also known as upcasting. Java uses this fact to resolve
calls to overridden methods at run time.
class A{
void dis(){
System.out.println(“class A");}
}
class B extends A{
void dis(){
System.out.println(“class B");}
A a1=new A();
a1.dis();
}
}
o/p:class B
class A {
void dis() {
System.out.println("Inside A's method");
}
}
class B extends A {
void dis() {
System.out.println("Inside B's method");
}
}
class C extends A
{
void dis() {
System.out.println("Inside C's method");
}
}
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A ref_var; // obtain a reference of type A
ref_var = a; // ref refers to an A object
ref_var.dis(); // calling A's dis()
ref_var = b; // now ref refers to a B object
ref_var.dis(); // calling B's dis()
ref_var = c; // now ref refers to a C object
interface B extends A{
void show(); }
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
3)Using fully qualified name
• If you use fully qualified name then only
declared class of this package will be
accessible. Now there is no need to import. But
you need to use fully qualified name every time
when you are accessing the class or interface.
• It is generally used when two packages have
same class name e.g. java.util and java.sql
packages contain Date class.
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}
Creating Sub packages
• create a sub package p2 within our existing
java package p1. Then we will modify our code
as