What is Abstract class in Java


 A class that is declared as abstract is known as abstract class.
Syntax:
abstract class <class-name>{}
An abstract class is something which is incomplete and you cannot create instance of abstract class.
If you want to use it you need to make it complete or concrete by extending it.
A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended.
What is Abstract method in Java?
A method that is declare as abstract and does not have implementation is known as abstract method.
If you define abstract method than class must be abstract.
Syntax:
abstract return_type method_name ();
An abstract method in Java doesn't have body, it’s just a declaration. In order to use abstract method you need to override that method in Subclass.

Example:
package com.opps;
abstract class AbstractClassExampleBank {
            abstract void  withDrawMoney();
            void displayData()
            {
                        System.out.println("Hell iam internal method of Abstarct class");
            }
}
package com.opps;
public class IciciBankAtm extends AbstractClassExampleBank{
            void withDrawMoney() {
            System.out.println("Money withdraw  from ICICI BANK ATM");
            }
}
package com.opps;
public class HdfcBankAtm extends AbstractClassExampleBank {
            void withDrawMoney() {
                        System.out.println("Money withdraw  from HDFC BANK ATM");
  }
}
package com.opps;
public class TestAbstractClass {
public static void main(String[] args)
{
            AbstractClassExampleBank hdfc=new HdfcBankAtm();
            hdfc.withDrawMoney();
            hdfc.displayData();
            AbstractClassExampleBank icici=new IciciBankAtm();
            icici.withDrawMoney();
            icici.displayData();
}
}


Summary:

-    abstract is a keyword in java.
-    Use abstraction if you know something needs to be in class but implementation of that varies.
-     In Java you cannot create instance of abstract class , its compiler error.
-    A class automatically becomes abstract class when any of its method declared as abstract.
-     abstract method doesn't have method body.
-    Variable cannot be made abstract, its only behavior or methods which would be abstract.
-    If a class extends an abstract class or interface it has to provide implementation to all its abstract method to be a concrete class. Alternatively this class can also be abstract.


Share:

No comments:

Post a Comment

Ordered List

Popular Posts

The Magazine

Facebook

Post Top Ad

LightBlog

Post Top Ad

Your Ad Spot

About Me

authorHello, my name is Jack Sparrow. I'm a 50 year old self-employed Pirate from the Caribbean.
Learn More →

Comment

Post Top Ad

Your Ad Spot

Hot

AD BANNER

Sponsor

AD BANNER

Recent Comments

Extra Ads

AD BANNER

Contact Form

Name

Email *

Message *

Translate

Sponsor

test

Home Top Ad

Responsive Ads Here

Flickr

Ad Banner
Responsive Ads Here

About Us

authorHello, my name is Jack Sparrow. I'm a 50 year old self-employed Pirate from the Caribbean.
Learn More →

Recent Posts

Unordered List

  • Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  • Aliquam tincidunt mauris eu risus.
  • Vestibulum auctor dapibus neque.

Sample Text

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

Pages

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.