What is Abstraction in Java

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.
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.



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
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();
}

}

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.