Method Overriding &
polymorphism by Aamir jamil
OBJECTIVE
Study Method Overriding, Abstract
class and Interface.
THEORY
Method
Overriding By aamirjamil
In
a class hierarchy, when a method in a subclass has the same name and type signature
as a method in its superclass, then the method in the subclass is said to override
the method in the superclass. When an overridden method is called from within
a subclass, it will always refer to the version of that method defined by the subclass.
The version of the method defined by the superclass will be hidden. Consider the
following:
// Method
overriding by aamir jamil
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i
and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int
c) {
super(a, b);
k = c;
}
// display k – this
overrides show() in A
void show() {
System.out.println("k:
" + k);
}
}
class Override {
public static void
main(String args[]) {
B subOb = new B(1,
2, 3);
subOb.show(); //
this calls show() in B
}
}
Output:
When
show( ) is invoked on an object of type B, the version of show(
) defined within B is used. That is, the version of show( ) inside
B overrides the version declared in A.
If
you wish to access the superclass version of an overridden function, you can do
so
by using super. For example, in this version of B, the superclass
version of show( ) is invoked within the subclass’ version. This allows
all instance variables to be displayed.
class B extends A {
int k;
B(int a, int b, int
c) {
super(a, b);
k = c;
}
void show() {
super.show(); //
this calls A's show()
System.out.println("k:
" + k);
}
}
If
you substitute this version of A into the previous program, you will see
the following output:
Polymorphism By aamirjamil
Polymorphism describes a language’s ability to process objects of various
types and classes through a single, uniform interface.
Polymorphism in Java has two
types: Compile time polymorphism (static binding) and Runtime polymorphism
(dynamic binding). Method overloading is an example of static polymorphism,
while method overriding is an example of dynamic polymorphism.
Polymorphism is the ability of an object to take on
many forms. The most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
Using
Abstract Classes By aamirjamil
There
are situations in which you will want to define a superclass that declares the structure
of a given abstraction without providing a complete implementation of every
method.
That is, sometimes you will want to create a superclass that only defines a generalized
form that will be shared by all of its subclasses, leaving it to each subclass
to
fill in the details. Such a class determines the nature of the methods that the
subclasses must implement.
- An abstract method will force to make
the class abstract.
- An abstract method cannot have a body.
Only signature is define with ";".
- Abstract class can contain both abstract
and non abstract methods.
- To use the non abstract methods of
abstract class we must make a child class.
- The child class must provide the
implementation of the parent abstract methods by overriding
- If a child cannot provide implementation
of parent abstract methods the abstract methods will force to make the
child class abstract. In that case we cannot make Object of child class
- Abstract class can be used as reference
class but not as Object class.
- After providing implementations of all
abstract methods in the hierarchy we can create Objects
// By aamirjamil
abstract class A
{
abstract void add()
{ //Abstract method with body
will generate error
System.out.println("Add
of A");
}
abstract
void add();
abstract
void sub();
void
mull()
{
System.out.println("Multiply
of A");
}
void
div()
{
System.out.println("Divide
of A\n");
}
}
// Class B is abstract because of not providing the
implementation of abstract void sub() of A.
abstract class B extends A
{
void add()
{ //Class B providing
implementation of abstract void add() of class A
System.out.println("Add
of B");
}
void
div()
{
System.out.println("Divide of
B\n");
}
}
// Class C providing the implementation of abstract void
sub() of A and overriding multiply of A.
class C extends B
{
void
sub()
{
System.out.println("Subtract
of C");
}
void
mull()
{
System.out.println("Multiply
of C");
}
}
public class Abstract2
{
public static
void main (String[] args)
{
//We can
only make Object of class C
A obj1 =
new C(); //Reference class A
B obj2 =
new C(); //Reference class B
C obj3 =
new C();
obj1.add(); //Add of B
obj1.sub();
//Subtract of C
obj1.mull();
//Multiply of C
obj1.div();
//Divide of B
obj2.add(); //Add of B
obj2.sub();
//Subtract of C
obj2.mull();
//Multiply of C
obj2.div();
//Divide of B
obj3.add(); //Add of B
obj3.sub();
//Subtract of C
obj3.mull();
//Multiply of C
obj3.div();
//Divide of B
}
}
OUTPUT
Interfaces
Using
the keyword interface, you can fully abstract a class’ interface from
its implementation. That is, using interface, you can specify what a
class must do, but not how it does it. Interfaces are syntactically similar to
classes, but they lack instance variables, and their methods are declared
without any body. In practice, this means that you can define interfaces which
don’t make assumptions about how they are implemented. Once it is defined, any
number of classes can implement an interface. Also, one class can
implement any number of interfaces.
To
implement an interface, a class must create the complete set of methods defined
by the interface. However, each class is free to determine the details of its
own implementation. By providing the interface keyword, Java allows you
to fully utilize the “one interface, multiple methods” aspect of polymorphism.
Program 1: Interface1
Interface
X
(Abstract
add and sub)
Class
A Implements X
(Implements
add and sub)
//// By aamirjamil
//Methods of Interface are
by default abstract and public
//We can only define their
signatures
interface X{
void add();
void sub();
}
//A child class implements
(not extends) X interface
//A child class will provide
implementation of both abstract methods
//Methods overriding will be
used and access control must be public
class A implements X{
public void add(){
System.out.println("Add of A");
}
public void sub(){
System.out.println("Subtract of
A");
}
}
public class Interface1{
public static void main (String[] args){
A obj1 = new A();
X obj2 = new A(); //Interface reference can
be used to perform //polymorphism
System.out.println("\nAdd and Subtract
of A\n");
obj1.add ();
obj1.sub ();
System.out.println("\add
and Subtract of A on Interface
reference\n");
obj2.add ();
obj2.sub ();
}
}
No comments:
Post a Comment