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:
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.
- 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.
No comments:
Post a Comment