abstract class in java abstract class and interface in java use of abstract class in java abstract class in java with example example of abstract class in java abstract class example in java abstract class in java example Concrete class in java Concrete class in java with example abstract class in java with example define abstract class in java abstract class and interface in java
Classes in java:
Class is collection of associated data members and methods is known as Class. We know that if we want to write any java program then it must starts with concept
called class i.e; without classes concept there is no java program.
Types Of classes
in java:
Concrete classes:
A Concrete class
is one in which contains fully defined methods or implemented or concrete
methods.Once the class is
concrete we can create its object directly.
Syntax:
Class
<class-name> // fully defined class
{
Return-type
method()
{
-----------------
------------------
}
};
For Example:
Class Demo
{
void method1()
{
System.out.println(“this is fully defined method of method1()”);
}
void method2()
{
System.out.println(“this is fully defined method of method2()”);
}
public static void main(String args[])
{
Demo D=new
Demo();// this is Demo class object “D” created directly because Demo is
//Concrete class
D.method1();
D.method2();
}
}
Here Class Demo is containing two defined methods. Hence it
is known as Concrete class, so that whose object is created directly.
Limitation of
Concrete Class:
1)
In real industry concrete classes are highly
recommended to use / to deal with specific which are requirements suitable to
only one programmer.
Industry no recommended to use concrete classes to deal with common
requirements which are suitable for all java programmers or universally.
2)
While dealing
common requirements with concrete
classes then we get limitations as
a)
More amount of memory space.
b)
Less performance.
Abstract Class:
Abstract class comes into picture when one of
the requirements are not possible with
concrete class. Requirements are to deal
with common requirements for all java programmers universally ,Concrete class
does not support this requirement.
Definition of
Abstract Method:
An abstract method
is one which contains only method declaration/prototype but not body.
In order to make any un defined method as abstract method,
that declaration of undefined method must be preceded by a keyword “Abstract”.
Syntax for
Abstract Method:
abstract
Return-type Method-Name(list of formal type if any);
For Example:
abstract void
demo_method();
Definition of
Abstract Class:
An abstract class in which it contains some
defined methods and some undefined methods / unimplemented methods / abstract
methods.
We know that every
method of java belongs to class including abstract method. If a class contains
abstract method then the class is known as abstract class.
Programmatically In
order to make a class as abstract ,whose definition must be preceded by a
keyword called Abstract.
Point To Remember:
An
abstract class object cannot create directly but we can it indirectly.
Indirect creation of object.
An object of abstract
class is equal to an object of its sub class. Or
An object of abstract class is equal to an object of that
class which extends that abstract class. Or
An object of sub class of abstract class is nothing but an
object of abstract class.
Syntax of Abstract
class:
abstract class
<class-name>
{
abstract return-type method-name(list of formal parameters if required);
abstract return-type method-name(list of formal parameters if required);
};
For Example:
abstract class Demo
{
abstract void
demo-test1();
abstract void
demo-test2();
};
Class Derived-Demo
{
void demo-test1()
{
System.out.println(“this is defined
method of Derived-Demo class”);
}
void demo-test2()
{
System.out.println(“this defined method of Derived-Demo
class”);
}
public static void main(String args[])
{
Demo D=new Demo();// INVALID
Demo D=new Derived-Demo();// VALID and
creation of object to abstract class is indirect.
D.demo-test1();
D.demo-test2();
}
}
From the above program / example is abstract class such that
we cannot create object this class
demo directly.
demo directly.
Demo D=new Demo();// Invalid
But we can create
object of this abstract class indirectly.
Demo D=new
Derived-Demo();//valid and it is indirect creation of object.
Points To Remember
Regarding Abstract class:
1)
Abstract class are always reusable because
abstract classes contains common reusable features.
2)
The definition of abstract class should not be
final because abstract class must always
participates in inheritance process.
participates in inheritance process.
3)
Abstract class concept of java makes use of
polymorphism along with method
overriding for business logic development and uses dynamic binding concept for
execution logic.
overriding for business logic development and uses dynamic binding concept for
execution logic.
4)
The advantages of abstract classes are
a)
Use less memory space for application
b)
More performance
In java programming an object of abstract can be declared but it cannot
be referenced
indirectly w.r.t concrete class.
indirectly w.r.t concrete class.
5)
An abstract class of java is not recommended to
contain parameterized constructors.
But it default contains system defined
default constructor.
6)
An object of abstract class contains details
about those features which are available
in the same class but it never contains details about those features which are specially
available in its derived class.
in the same class but it never contains details about those features which are specially
available in its derived class.
Abstract Base Class And Abstract
Derived Class:
An abstract
base class is one in which contains physical representation of abstract
methods.An abstract derived class is one in which contains logical representation of abstract
methods which are inherited from abstract base class.
methods.An abstract derived class is one in which contains logical representation of abstract
methods which are inherited from abstract base class.
Both abstract base
class and abstract derived class are reusable by the derived class.
For Example:
abstract
Class Base
{
abstract void
f1();
abstract void
f2();
};
Class Derived
extends Base
{
Void f1() // f1() method is defined here but f2()
still is abstract (not defined) but
{ //there is exists of f2() method logical
.
System.out.println(“this is f1() method”);
}
}
No comments:
Post a Comment