Hi,
Can I get a brief idea as to which to prefer - abstract class or Interface?
Also, in wat scenario i would prefer abstract class and in what I woud prefer interface????
Regards
Prathima
Hi,
Can I get a brief idea as to which to prefer - abstract class or Interface?
Also, in wat scenario i would prefer abstract class and in what I woud prefer interface????
Regards
Prathima
interfaces are better ....
interfaces are good to use because we can use multiple inhertiance in it.
we cannot instantiate Abstract classes until all the methods are implemented.
You can use Abstract Class when you want to provide partial implementation of methods and leave the rest for the extending class. Multiple inheritance is not possible in Java, so you can extend only one Abstract Class and implement its methods. However, you can implement as many interfaces as possible. So, Interfaces come in handy. Also, I have seen that if you are defining many constants, they are defined in interface and that interface is implemented. Hope this helps.
Hi All,
all the methods declared in abstract and interface, In Abstract class have atleast one abstract method , others may concrete.
Abstract method have atleast one subclass,Interface can't have any sub classes.
Abstract class we must declare the abstract keyword before the method.In interface we need not to use any keyword for methods.
An Abstract can extend only one class, where as Interface can extend more than one.
regards,
rajeshwar reddy.
Hi Prathima,
Generally,abstract class is written when there are some common features shared by all the objects as they are.
Let i explain with example!
Let us take a class WholeSaler which represents a wholesale shop with textbooks and stationery like pens,papers,notebooks etc.,
class WholeSaler
{
void text_books()
{
//Text books
}
void stationery()
{
// this can be Pen,Paper,Notebooks
}
}
Explanation:-
Let us take Person 1 & person 2
Person 1 needs J2ee book and Pens.
Person 2 Needs J2ee book and Notebook.
Here, the text_book() is common feature shared by both the persons.
So in this case,The programmer designs the WholeSaler class as an abstract class.person1,person2 are subclasses.
Suppose, person1 needs j2ee book and papers
person 2 needs Dotnet book and pens means,
the text_books() and stationery() needs different implementations depending on the persons.
So in this case, the programmer desings the WholeSaler class as an interface and person 1 ,person 2 become implementation classes.
The programmer prefers to write an interface when he/she wants to leave the implementations part to the third party vendors.
The programmer prefers to write an abstract class when he/she ready to provide the subclasses .
first of all I would like to explain what is abstract method.
the method which have no body is called as abstract method, and the class which contains at least one abstract method is called abstract class. there may be another concrete classes, but incase of Interface all the methods inside it must be declared as abstract.
Hi
interface is use when you want to define the peripheral abilities of a class where as An abstract class defines the core identity of a class.
hi
good question
Case i abstract:
when u want to create new method for ur class which can return more than one function(means Abstraction) but you want one of those function, then u go and create abs method which belongs to abs class.Atlast you should override that method in your class which extends the abs class
For example
abstract class vehicle
{
abstract int wheels();
}
class sumo extends vehicle
{
public int wheels()
{
return 4;
}
}
Case ii interface:
when u want to create more than one new method for ur class , All of which can return more than one function(means Abstraction) but you want one of those function and all metods might be in different classess or different abs classes in that case you can't extents all of which from your class So, then u go and creat more than one method which belongs to one interface or more than one interface .Atlast you should override that methods into your class which implements the more than one interface.The overriden method implicitely a abstraction feature.
For example
interface vehicle
{
public static int i=5;
//Implicitely a absmethod
public int wheels();
}
interface typeHorn
{
public static int i=5;
//Implicitely a absmethod
public String Horn();
}
class sumo implements vehicle,typeHorn
{
public int wheels()
{
return 4;
}
public String Horn()
{
return "BEEP";
}
}
hi anybody tell me the diference between == and equals() in all variable