Asked By
Ramiz Raja
0 points
N/A
Posted on - 08/08/2011
In C++ we can make an abstract class by making all the methods pure virtual so that its object can not be instantiated.
How can we make a Java class abstract so that its object can not be instantiated?
Please tell me the easiest way to do it.
Answered By
cesear
0 points
N/A
#110783
How to make abstract class in Java?
Hi,
it's very easy I'll show you two examples you will understand :
Eg1 :
we have this class
abstract public class FormeGeometric1{
double posX,posY ;
void deplacer(double x ,double Y) {
posX=x;
posY=Y;
}
void afficherPosition () {
System.out.println ("position : ("+posX+","+posY+")");
}
abstract double surface ()
abstract double perimeter
}
This class is Abstract because the two methods surface and perimeter are abstract
this class is no instantiated
Eg2 :
we have this class :
public class Cercel7 extends FormeGeometric1 {
double r;
Cercel7(Double x , double y , double ray) {
posX=x;
posY=y;
r=ray;
}
double surface () {
return Math.PI*Math.pow(rayon,2.);
}
double perimeter () {
return 2*r*Math.PI
}}
an Abstract class allow us to define the shared characteristics of different object classes
Here is a joined picture it will clear the vision
Answered By
lakshmi
0 points
N/A
#110784
How to make abstract class in Java?
The Easiest way to do it is
let us take a simple abstract class
public abstract class Abstract
{
//default constructor
public Abstract
{
System.out.println("Abstract Object is Created");
}//end of constructor
}//end of class
Now let us Take Main class
public class AbstractDemo
{
public static void main(String args[])
{
//Creating the Object to Abstract class
Abstract abs=new Abstract();
System.out.println("In the Main method");
}//end of main method
}//end of class
compile
javac Abstract.java
it will compile successfully
then
javac AbstractDemo.java
Then you will get an error
similar to this like, cannot instantiate abstract class
this is because we declared class as Abstract
then try the same example by removing the abstract in Abstract class then u will find the difference