Saturday, June 13, 2015

ABSTRACT CLASSES in Java

12:14 AM

ABSTRACT CLASSES:

In JAVA we have two types of classes. They are concrete classes and abstract classes.
• A concrete class is one which contains fully defined methods. Defined methods are also
known as implemented or concrete methods. With respect to concrete class, we can create
an object of that class directly.

For example:
class C1
{
int a,b;
void f1 ()
{
…………;
…………;
}
void f2 ()
{
…………;
…………;
}
};

To call the above method:

C1 O1=new C1 ();
O1.f1 ();
O1.f2 ();

An abstract class is one which contains some defined methods and some undefined
methods. Undefined methods are also known as unimplemented or abstract methods.
Abstract method is one which does not contain any definition. To make the method as
abstract we have to use a keyword called abstract before the function declaration.

Syntax for ABSTRACT CLASS:

abstract return_type method_name (method parameters if any);

For example:

Abstract void sum ();
The abstract methods make us to understand what a method can do but it does not give
how the method can be implemented. Every abstract method belongs to a class under class is known
as abstract class, to make the class as abstract we use a keyword called abstract before the class
specification.

Syntax for ABSTRACT CLASS:

abstract class <clsname>
{
Abstract return_type method_name (method parameters if any);
};
For example:
abstract class Op
{
abstract void sum ();
};
With respect to abstract class we cannot create an object direct but we can create
indirectly. An object abstract class is equal to an object of that class which extends that abstract
class.

For example:

class CC extends AC
{
…………;
…………;
};
AC Ao=new AC (); //invalid
AC Ao=new CC ();
or
AC Ao;
Ao=new CC ();


Write a JAVA program for computing sum of two integers and floats using abstract classes?
Answer:
abstract class Op
{
abstract void sum ();
};
class isum extends Op
{
void sum ()
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println ("INT VALUE = "+c);
}
};
class fsum extends Op
{
void sum ()
{
float f1,f2,f3;
f1=10.26f;
f2=20.32f;
f3=f1+f2;
System.out.println ("FLOAT VALUE = "+f3);
}
};
class AbDemo
{
public static void main (String k [])
{
// Op o1=new Op (); invalid
Op o2;
o2=new isum ();
o2.sum ();
o2=new fsum ();
o2.sum ();
}
};

Abstract classes should not be final, since, they are always reusable. Abstract classes are
basically used to implement polymorphism; we use the concept of dynamic binding. Hence,
abstract classes, polymorphism and dynamic binding will improve the performance of JAVA J2EE
applications by reducing amount of memory space.

Whenever we inherit ‘n’ number of class, if the derived class defines all ‘n’ concrete class. If the derived class methods then the derived classwe use a keyword called abstract An abstract base class is one
An abstract derived class is one inherited from abstract base class

Implement the above diagram by using abstract class’s polymorphism and dynamic binding.


Answer:
abstract class C1
{
abstract void f1 ();
abstract void f2 ();
};
abstract class C2 extends C1
{
void f1 ()
{
System.out.println
}
};
class C3 extends C2
{
void f1 ()
{
super.f1 ();
System.out.println ("f1
}
void f2 ()

abstract methods from abstract base class number of abstract methods then the is not defining at least one abstract method out of ‘n’ abstract is known as abstract derived class and to make that class abstract
abstract.which contains physical representation of abstract methodswhich contains logical declaration of abstract methods which areclass.

An object of either concrete base class or abstract base class contains the details about
those methods which are available in that class only but this object (concrete base class or abstract
base class) does not contains details of those methods which are specially defined in derived
class’s.

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top