abstract class and interface

Ads
 

abstract class and interface

View Answers

November 24, 2008 at 10:51 PM

Hi friend,


Abstract classes

In java programming language, abstract classes are those that works only as the parent class or the base class. Subclasses are derived to implement the methods inherited from the abstract class (base class). Abstract classes are not instantiated directly. First extend the base class and then instantiate (create objects). Abstract classes are generic in nature and implement to those methods that are used commonly by the subclasses with common implementation

Abstract Method

An abstract method one that have the empty implementation. All the methods in any interface are abstract by default. Abstract method provides the standardization for the " name and signature" of any method. One can extend and implement to these methods in their own classes according to the

These classes that contain one or more abstract methods.
An abstract method is a method that is declared, but contains no implementation.
It may not be instantiated, and require subclasses to provide implementations for the abstract methods.


Example of abstract class and Method

Modeling the behavior of animals, by creating a class hierachy that started with a base class called Animal.
Animals are capable of doing different things like flying, digging and walking,
but there are some common operations as well like eating and sleeping. Some common operations are performed by all animals,
but in a different way as well. When an operation is performed in a different way,


public abstract Animal
{
public void eat(Food food)
{
// do something with food....
}

public void sleep(int hours)
{
try
{
// 1000 milliseconds * 60 seconds * 60 minutes * hours
Thread.sleep ( 1000 * 60 * 60 * hours);
}
catch (Exception ie) { /* ignore */ }
}

public abstract void makeNoise();
}


Now, any animal that to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise
it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.

public Dog extends Animal
{
public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
public void makeNoise() { System.out.println ("Moo! Moo!"); }
}

Why not declare an abstract class as an interface, and have the Dog and Cow implement the interface.
Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes,
you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces -
an interface cannot provide any method implementations.

For difference between interface and abstract class visit to :

http://www.roseindia.net/help/java/a/interface-vs-abstract-class.shtml

http://www.roseindia.net/java/master-java/abstract-class.shtml

http://www.roseindia.net/java/master-java/interface.shtml

Thanks

Ads









Related Tutorials/Questions & Answers:
Interface and Abstract class
and Abstract class? Give some example   Difference Between Interface and Abstract Class 1)Main difference is methods of a Java interface are implicitly...)Members of a Java interface are public by default. A Java abstract class can have
Abstract class and interface in Java
Abstract class and interface in Java  What is the difference between abstract class and interfaces in Java?   Differences between an interface and an abstract class: At the same time multiple interfaces can
Advertisements
Interface vs abstract class
Interface vs abstract class  Hai, Is there any possible to defining abstract methods in Interface
Difference between abstract class and an interface
Difference between abstract class and an interface The difference between abstract class and interface is one of the most popular question in the interview... between abstract class and interface in java. Abstract class is a class
where exactly we use interface and where abstract class?
where exactly we use interface and where abstract class?  what is the use of interface over abstract class? and where we should use interface and where abstract class
Why does Java provide both abstract class as well as interface
Why does Java provide both abstract class as well as interface  why does java have both abstract class as well as interface? what is the need or importance of having both
Abstract and Interface
Interface and Abstract Class 1)Main difference is methods of a Java interface... declared in a Java interface is by default final. An abstract class may contain non... abstract class should be extended using keyword 'extends'. 5)An interface can
Abstract and Interface
Abstract and Interface  what is the difference between Abstract and Interface accurateatly
Abstract class
Abstract class  Can an abstract class be final
Abstract class
Abstract class  what is an Abstract class
abstract class
abstract class   Explain the concept of abstract class and it?s use with a sample program.   Java Abstract Class An abstract class is a class that is declared by using the abstract keyword. It may or may not have
Abstract class
Abstract class  j   An Abstract class is a base class which... with an abstract keyword. For more information, visit the following links: http://www.roseindia.net/help/java/a/java-abstract-class.shtml http://www.roseindia.net/java
abstract class
abstract class  abstract class AbstractClass{ } is possible and compile the class or any compile time error occur tell me the answer Blockquote
abstract class
abstract class  what is abstract class .why we use it or what is the need of this class?   Abstract class is like base class which contains abstract method and cannot instantiated.   Please go through
Abstract class
Abstract class  Calendar cal=Calendar.getInstance() We know that Calendar is an abstract class so it can't be instantiated. So how we can say that cal is an instance of Calendar??? Beginner Question I suppose. Thanks in advace
code for abstract class
code for abstract class  code for abstract class
Abstract class,Abstract methods and classes
that have the empty implementation. All the methods in any interface are abstract...(); Abstract Class In java programming language, abstract classes... to implement the methods inherited from the abstract class (base class
Why Abstract Class?
Why Abstract Class?  What is the use of Abstract Class if it has Zero Abstract methods
Why Abstract Class?
Why Abstract Class?  What is the use of Abstract Class if it has Zero Abstract methods
Java Abstract Class
Java Abstract Class       An abstract class is a class that is declared by using the abstract keyword...;  --or-- Java provides a special type of class called an abstract class
variables and methods declared in abstract class is abstract or not
variables and methods declared in abstract class is abstract or not  variables and methods declared in abstract class is abstract
why we use abstract class in java?
why we use abstract class in java?  what is the the purpose of abstract class.Give example when to use abstract and when use interface
Explain final class, abstract class and super class.
Explain final class, abstract class and super class.  Explain final class, abstract class and super class.   Explain final class, abstract.... An abstract class is a class that is declared by using the abstract keyword. It may
Write Full concept of abstract class
Write Full concept of abstract class  Abstract class
Java Abstract Class Example
Java Abstract Class Example In this section we will read about the Abstract class. Abstract class in Java is a class which is created for abstracting the behaviour of classes from the outside environment. Abstract class can be created
Abstract Class in Java
Abstract class in Java is a class that is declared using abstract keyword. It cannot be instantiated but can be extended into subclass. Abstract class cannot be instantiated means that new instances of an abstract class cannot
What is an Abstract Class and what is it's purpose?
What is an Abstract Class and what is it's purpose?   Hi, What is an Abstract Class and what is it's purpose? Thanks
Can a abstract class be declared final?
Can a abstract class be declared final?   Hi, Can a abstract class be declared final? Thanks
Can a abstract class be defined without any abstract methods?
Can a abstract class be defined without any abstract methods?   hi, Can a abstract class be defined without any abstract methods? thanks
overriding in derived class from abstract class
overriding in derived class from abstract class  why should override base class method in derived class? Instead of override abstract class method, we can make method implementation in derived class itself know
Interface in java with example
of a class. Interface is a collection of abstract methods. Interface has only static constant and abstract method. We can only implement a class and extended... all method in interface abstract and public. Multiple inheritance allows

Ads