Abstract Class

Abstract class:
  • Abstraction is a process of hiding the class implementation from its
    utilization. The abstraction specified that do not show how object
    functionality is implemented but provide an interface to use the
    functionality.

In abstract class you can declare

  • Abstract method
  • Concrete method
    — ( Defining method with method declaration and method body is known as concrete method )
  • Static and non static member
  • You can also declare constructor
  •  It is not mandatory that abstract class must have abstract method.
  • You can not create an instance of abstract class.
  • Abstract keyword can not compile with following keyword.

— Static
— Final
— Private

Abstract class
  • If you define class as abstract class then class must have at least one subclass simply abstract class have no use.
  • you must provide implementation of abstract method in sub class  if you don’t want provide implantation then you must declare subclass as abstract
Abstract class Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
abstract class Demo2
{
 
	abstract void test(int arg);
 
	abstract void disp();
 
 
}
 
//you must provide implementation of abstract method in sub class 
/*
or if you don't want provide implantation then you must declare subclass 
as abstract
*/
 
 
class Sample2 extends Demo2
{
 
	void test(int arg)
	{
 
		System.out.println("running int arg test method");
	}
 
	void disp()
	{
 
		System.out.println("running disp method");
 
	}
 
}
 
public class Abstract2
{
 
	public static void main(String[] args) 
	{
 
		System.out.println("*******************************");
 
 
                Sample2 obj1=new Sample2();
 
 
		   obj1.test(12);
		   obj1.disp();
 
 
		System.out.println("*******************************");
	}
}
 
Output:
 
running int arg test method
running disp method
*******************************
Defining subclass as Abstract
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package abstraction;
 
abstract class Demo3
{
 
	abstract void test(int arg);
 
	abstract void disp();
 
 
}
 
//you must provide implementation of abstract method in sub class 
/*
or if you don't want provide implentation then you must declare subclass as 
abstarct
*/
 
 
abstract class Sample3 extends Demo3
{
	void test(int arg)
	{	
		System.out.println("running int arg test method");
	}
 
	//disp abstract......
 
}
 
class Sample4 extends Sample3
{
 
	void disp()
	{
 
		System.out.println("running disp method");
 
	}
 
 
}
 
public class Abstract3
{
 
	public static void main(String[] args) 
	{
 
		System.out.println("*******************************");
 
 
         Demo3 obj1=new Sample4();
 
 
		   obj1.test(12);
		   obj1.disp();
 
 
		System.out.println("*******************************");
	}
}
interview Question
1. Can you declare abstract class as final class ?
ans: No

2. How to Access access member of abstract class
ans: define That member as static…..!!
because we can not create instance of abstract class
so if you define member as static then it can be refer through class.

How to Access member of abstract class ?
Ans: By declaring member of Member of as a static
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
 abstract class Demo1
 {
   static int k=12; //static member
 
   double d=12.4;//non static member
 
    Demo1()//constructor
    {
 
	System.out.println("running Demo1 constructor");
 
    }
 
    static void test() //static method
    {
    	System.out.println("running test method");
    }
 
 
	abstract void disp();//abstract method
 
 
}
 
 
public class Abstract1
{
 
	public static void main(String[] args) 
	{
 
		System.out.println("***************************");
 
 
		Demo1 d1;//creating  reference variable of Demo1
 
 
       /*
       you can not create instance of Demo1
       so we can access only static property because static member associated 
       with class not instance
       */
 
		System.out.println("k value: "+Demo1.k);
		Demo1.test();
 
		System.out.println("***************************");
 
 
	}
 
}
 
 Output:
 
 k value: 12
running test method
***************************

Leave a Reply

Your email address will not be published. Required fields are marked *