What is Abstraction?
Abstraction is process of hiding the implementation details and just showing only the functionality. to the user is nothing but Abstraction.
Abstraction can be achieved by using interface and abstract class.
Interface give 100% abstraction and abstract class give 0-100% abstraction.
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.
Abstraction is process of hiding the implementation details and just showing only the functionality. to the user is nothing but Abstraction.
Abstraction can be achieved by using interface and abstract class.
Interface give 100% abstraction and abstract class give 0-100% abstraction.
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:
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.
What is an interface?
Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default.
What is the use of interfaces?
As mentioned above they are used for abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance, using interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes.
Syntax
interface MyInterface
{
public void method1();
public void method2();
}
Note:
All the methods are public abstract by default
These methods are not having body
Example
Example
package com.opps;
public interface Customer {
public void saveCustomer();
public void displayCustomer();
public void editCustomer();
public void deleteCustomer();
}
package com.opps;
public class CustomerImpl implements Customer{
public void saveCustomer() {
System.out.println("Save the Customer Details");
}
public void displayCustomer() {
System.out.println("Display the Customer Details");
}
public void editCustomer() {
System.out.println("Edit the Customer Details");
}
public void deleteCustomer() {
System.out.println("Delete the Customer Details");
}
}
package com.opps;
public class CustomerTest {
public static void main(String[] args)
{
Customer customer=new CustomerImpl();
customer.saveCustomer();
customer.displayCustomer();
customer.editCustomer();
customer.deleteCustomer();
}
}
No comments:
Post a Comment