THOUSANDS OF FREE BLOGGER TEMPLATES

Thursday, April 22, 2010

INHERITANCE, POLYMORPHISM, INTERFACE

Inheritance
Inheritance can be defined as the process
where one object acquires the properties of another.
With the use of inheritance the information is made
manageable in a hierarchical order.

SUPERCLASS
A superclass allows for a generic interface to
include specialized functionality through the use of
virtual functions.The superclass mechanism is
extensively used in object oriented programming due
to the reusability that can be achieved: common features
are encapsulated in modular objects. Subclasses that
wish to implement special behavior can do so via virtual
methods, without having to duplicate (reimplement) the
superclass's behavior.

SUBCLASS
A subclass is a class that inherits some properties
from its superclass.

BENEFITS OF INHERITANCE
1. Code reusebility.
2. Minimise the amount of duplicate code.
3. Sharing common code amongst several subclasses.
4. Smaller, simpler and better organisation of code.
5. Make application code more flexible to change.

OVERRIDING METHODS
Method Overriding is achieved when a subclass
overrides non-static methods defined in the superclass,
following which the new method implementation in the
subclass that is executed.The new method definition
must have the same method signature (i.e., method name
and parameters) and return type. Only parameter types
and return type are chosen as criteria for matching
method signature. So if a subclass has its method
parameters as final it doesn’t really matter for
method overriding scenarios as it still holds true.
The new method definition cannot narrow the accessibility
of the method, but it can widen it. The new method
definition can only specify all or none, or a subset
of the exception classes (including their subclasses)
specified in the throws clause of the overridden method
in the super class.

FINAL METHODS AND CLASSES
Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class declaration somewhere before the class keyword.

/* File name : Employee.java */
public abstract class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
+ " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}


Notice that nothing is different in this Employee class. The class is now abstract, but it still has three fields, seven methods, and one constructor.

Now if you would try as follows:

/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{

/* Following is not allowed and would raise error */
Employee e = new Employee("George W.", "Houston, TX", 43);

System.out.println("\n Call mailCheck using
Employee reference--");
e.mailCheck();
}
}


When you would compile above class then you would get following error:

Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error1


Extending Abstract Class:
We can extend Employee class in normal way as follows:

/* File name : Salary.java */
public class Salary extends Employee
{
private double salary; //Annual salary
public Salary(String name, String address, int number, double
salary)
{
super(name, address, number);
setSalary(salary);
}
public void mailCheck()
{
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary()
{
return salary;
}
public void setSalary(double newSalary)
{
if(newSalary >= 0.0)
{
salary = newSalary;
}
}
public double computePay()
{
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}


Here we cannot instantiate a new Employee, but if we instantiate a new Salary object, the Salary object will inherit the three fields and seven methods from Employee.

/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP",
3, 3600.00);
Salary e = new Salary("John Adams", "Boston, MA",
2, 2400.00);

System.out.println("Call mailCheck using
Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using
Employee reference--");
e.mailCheck();
}
}


This would produce following result:

Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.



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.Any java
object that can pass more than on IS-A
test is considered to be polymorphic.
In Java, all java objects are polymorphic
since any object will pass the IS-A test
for their own type and for the class Object.
It is important to know that the only possible
way to access an object is through a reference
variable. A reference variable can be of only
one type. Once declared the type of a reference
variable cannot be changed.The reference variable
can be reassigned to other objects provided that
it is not declared final. The type of the reference
variable would determine the methods that it can
invoke on the object.A reference variable can
refer to any object of its declared type or
any subtype of its declared type. A reference
variable can be declared as a class or interface type.


INTERFACE
An interface is a collection of abstract methods.
Aclass implements an interface, thereby inheriting
the abstract methods of the interface.
An interface is not a class. Writing an interface
is similar to writing a class, but they are two
different concepts. A class describes the attributes
and behaviors of an object. An interface contains
behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

-An interface is similar to a class in the following ways:

-An interface can contain any number of methods.

-An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

-The bytecode of an interface appears in a .class file.

-Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including:

-You cannot instantiate an interface.

-An interface does not contain any constructors.

-All of the methods in an interface are abstract.

-An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

-An interface is not extended by a class; it is implemented by a class.

-An interface can extend multiple interfaces.

0 comments: