adspace


what is abstrac class? why it is use?

Answer Posted / rstv

Abstract classes are classes that contain one or more
abstract methods. An abstract method is a method that is
declared, but contains no implementation. Abstract classes
may not be instantiated, and require subclasses to provide
implementations for the abstract methods. Let's look at an
example of an abstract class, and an abstract method.

Suppose we were 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, it is a good candidate for an abstract
method (forcing subclasses to provide a custom
implementation). Let's look at a very primitive Animal base
class, which defines an abstract method for making a sound
(such as a dog barking, a cow mooing, or a pig oinking).

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 (InterruptedException ie) { /* ignore */ }
}

public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an
abstract method, and an abstract class. Now, any animal
that wants 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!"); }
}

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

sort term descripttion form, report and uery

2254


Which variable declarations within a class is invalid in php?

1047


Write a program using while loop?

1115


Explain me is it possible to destroy a cookie?

1026


How to create a web form?

1109


List some features of php that are deprecated in php7?

1021


hello all, I need some sample placement papers in lion bridge.. can anyone help me?

2175


How to calculate the difference between two dates using php?

1143


How can we extract string "pcds.co.in" from a string "https://info@pcds.co.in" using regular expression of php? More on reg can you explain

1077


What does $_files means?

1162


how to detect a mobile device using php

1173


What is difference between static and final in php?

1108


Name and explain five of the PHP error constants?

1033


Tell me what kind of things have you done on the social side?

1064


What sized websites have you worked on in the past?

1085