Abstraction in java

Previous
Next

Abstraction:

  • Abstraction is the concept of hiding un-necessary details of Object and shows only essential features in communication process.
  • Abstraction describes “What an object can do instead how it does it?”.
  • Abstraction is the “General View” of Object.

Abstract class:

  • It is the partial representation of Object.
  • If a class is not able to provide definition to all the methods become an abstract class.
  • ‘abstract’ modifier is used to define abstract classes.
  • Abstract class also saved with ‘.java’ extension only and class file will be generated when abstract class has compiled.

Note: Abstract class is allowed to define

  1. abstract methods(methods don’t have body)
  2. concrete methods(methods having body)
abstract class AbstractClass
{
	void concreteMethod( )
	{
		// having body….
	}
	abstract void abstractMethod( ); // no definition
}
  • Abstract class cannot be instantiated directly using ‘new’ keyword, because it was not fully defined.
  • We can instantiate classes because these are completed defined.
  • For example, we can release any Mobile into the market after 100% manufactured.
abstract class AbstractClass
{
	void concreteMethod() 
	{
		// empty
	}
	abstract void abstractMethod() ;
}
class Access
{
	public static void main(String args[ ])
	{
		new AbstractClass(); // Error :
	}
}

Can we define main() method inside abstract class ?

  • JVM invokes main() method automatically to start program execution.
  • We can define both static and non static methods inside abstract class.
  • Main() is static method and it will be called automatically using class name.
  • We can access other static methods of abstract class using class identity.
abstract class Test 
{
	public static void main(String[] args) 
	{
		System.out.println("Abstract class main...");
		Test.fun();
	}
	static void fun()
	{
		System.out.println("Static user method");
	}
}
  • We can define constructor in Abstract class.
  • Constructor is working like concrete method(having body).
abstract class Test 
{
	Test()
	{
		// logic...
	}
}
  • We can define constructor in abstract class but we cannot create object for abstract class directly.
  • Abstract class is not fully defined. With partial definition, we cannot release any object for communication into real world.
abstract class Test 
{
	Test()
	{
		// logic...
	}
	public static void main(String args[ ])
	{
		new Test();  // Error :
	}
}
Previous
Next

Add Comment

Courses Enquiry Form