Abstract Classes and Abstract Methods.

In this article,Abstract Classes and Abstract Methods is explained in detail with examples.
An abstract is a keyword,use as a modifier in java programming in two situations.
  • Abstract methods.
  • Abstract classes.
Abstract Methods
A method in a class which is incomplete without a body is known as an abstract method or incomplete method or non-concrete method.
Ex
public void method1();
public void display();
such methods are designated with the keyword "Abstract".
Ex
public abstract void method1();      (From the topic Abstract Classes and Abstract Methods)
public abstract void display();
Abstract Class
If a class contains at least one method as an incomplete method or abstract method then such class is known as "abstract class".
Such classes are designated with abstract keyword while declaring.
Ex
abstract class A
{
int a,b;
abstract void m1(); //incomplete method.
void m2() //complete-method
{
----
}
}
So here in class A we have m1() method as incomplete hence class A becomes incomplete class or abstract class.
**since abstract-classes are incomplete-classes, we cannot create objects for such classes.
Ex 
A obj=new A();  //Error--- Can't create object

How to use Abstract classes
As abstract-classes cannot be instantiated,we inherit such classes and redefine all abstract-methods in sub-class.
Now sub-class become complete class and object of such class can be created. (From topic Abstract Classes and Abstract Methods)
Ex
class B extends A
{
int x,y;
void m1() //redefining abstract method of base-class.
{
----
}
void m3()
{
--
}
}
B b=new B(); //object can be created
**If in sub-class,base-class abstract methods are not redefined then sub-class also becomes abstract class and this can't be instantiated.
Abstract keyword is compulsory while using abstract methods as well as abstract classes.
(From topic Abstract Classes and Abstract Methods.)
Ex
//program related to Abstract Classes and Abstract Methods--abstract class
abstract class A
{
int a,b;
abstract void m1();
void m2()
{
System.out.println("A=="+a);
System.out.println("B=="+b);
}
}
class B extends A
{
int x,y;
void m1()
{
System.out.println("Abstract method redefined");
}
void m3()
{
System.out.println("X=="+x);
System.out.println("Y=="+y);
}
}
class AbstractExample
{
public static void main(String args[])
{
//A obj=new A(); //Error cannot create object
B b=new B();
b.x=11;
b.y=22;
b.m1();
b.m3();
A a;
a=new B();
a.a=10;
a.b=20;
a.m1();
a.m2();
//a.m3(); // Error because m3() belongs to subclass.
}
}
Output
Abstract class-Javaform
                              To get the code file use me.
Ex
//Program related to Abstract Classes and Abstract Methods--Abstract class.
abstract class A
{
int x=10,y=20;
static int z=30;
void method1()
{
System.out.println("X:::"+x);
System.out.println("Y:::"+y);
System.out.println("Z:::"+z);
}
//final abstract void method2();
//abstract static void method2();
//private abstract void method2();       //(From the topic Abstract Classes and Abstract Methods)
abstract void method2();
}
class B extends A
{
int i=40,j=50;
void method1()
{
System.out.println("X:::"+x);
System.out.println("Y:::"+y);
System.out.println("I:::"+i);
System.out.println("J:::"+j);
}
void method2()
{
System.out.println("I:::"+i);
System.out.println("J:::"+j);
}
}
class AbstractExample1
{
public static void main(String args[])
{
//A obj=new A();
A obj1=null;
System.out.println(obj1);
B obj2=new B();
System.out.println(obj2); //obj.Ref code.
obj1=obj2;
System.out.println(obj1); //obj.Ref code.
obj1.method1();
System.out.println();
obj1.method2();
//System.out.println("Static Z vlue is::"+A.Z);
//A.method3();
}
}
Output
Abstract class and abstract method-javaform
                             To get the code file use me.
Abstract methods of base class should be redefined in a subclass,hence they can't be final or static or private.
Since abstract methods can't be instantiated,in such class,we can have static members and such members can be accessed directly with the class name.

Continue to the next topic Packages in Java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing 

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required