What is an interface?

September 11th, 2006 Admin Posted in Polymorphism and Inheritance No Comments »

The variables that are declared within an interface acts like final and static variables.

The advantage of interface is that as these are placed in the top level of class hierarchy, these are available for many subclasses.

Example:

access interface interface-name
{
return-type method-name(parameter-list);


}

Here the access may be either public or non-public(default).  All the variables and methods that are declared within an interface are public if it is declared as public.

For the members of interface abstract keyword is optional.
Interfaces are to achieve multiple inheritance. Java classes doesn’t allow multiple inheritance but we  can achieve it through Java interfaces indirectly. That is we can implement more number of interfaces to a class.

To inherit an interface to a class implements key word is used.

Interface A
{
public void  getData(int x);
}
class B implements A
{int a;
public void  getData(int x)
{
x=a;
}
}

If a class implements more than one interface the interfaces are separated with a comma.

Note:

1. The methods that implement an interface must be declared public.

2. The type signature of the implementation method must match with the  type signature specified in the  interface.

Like abstract classes we can not create object but we can refer subclass objects with the references.

An interface can also extend to another interface. One interface can be inherited to another interface by using the keyword extends. When a class implements the interface it must provide the implementations for all methods in the interface inheritance chain.

AddThis Social Bookmark Button

What are Abstract methods?

September 11th, 2006 Admin Posted in Polymorphism and Inheritance No Comments »

These are the methods that are declared but not defined. Whenever we don’t want to define a method even if it is declared then such method must be declared as abstract methods with a keyword abstract.

abstract type-name(parameter-list);

abstract void list( );

Any class that has one or more abstract methods must be declared as abstract class.

To declare a class as an abstract class use abstract keyword in front of a class. An abstract class cannot be instantiated with new operator as it is not defined completely.

Static methods and constructors cannot be declared as abstract methods.
Any subclass which extends to an abstract  class must implement all the abstract methods of the super class.

If any abstract method is left undefined in the subclass then the subclass must also be declared as abstract.

Although the abstract classes cannot be used to instantiate objects they can be used to refer object instances.that is we can make the abstract class references to refer subclass objects.

An abstract methods only resides in an abstract class but the reverse is not true.

i.e. an abstract class can be without abstract methods.

Final and abstract are opposite in functionality. Final does not allow inheritance where as abstract compulses inheritance.

Using of  abstract and static together is illegal.

Abstract can be given only for methods but not for data members.

AddThis Social Bookmark Button

What is an abstract class?

September 11th, 2006 Admin Posted in Polymorphism and Inheritance No Comments »

Sometimes it is required to define a super class that declares a structure without having the complete implementation of  all methods. It is the generalized form of class that will be  shared by all of its subclasses.

AddThis Social Bookmark Button