Abstraction in Java

Abstraction

Abstraction: Hiding of implementation and using the functionality is called as abstraction.

Abstraction in java can be achieved by using a special classes called "Abstract classes".

Abstract Classes: These are same as classes but have two types of methods namely :
  1. Concrete Methods 
  2. Abstract Methods
1.Concrete Methods : The method which have declaration along with the body/logic of the  method such Methods are called as Concrete Methods.

Syntax:
class ClassName
{
     <datatype> methodName()
    {
        Statements;
     }
}

Example:
class ConctreteClassName
{
    void concrete()
   {
       System.out.println("concrete Method");
   }
2.Abstract Method: The method which have no body or logic Such methods are called as Abstract Methods.

Syntax:

abstract class ClassName
{
   abstract <datatype> methodName();

    <datatype> methodName()
     {
          Statements;
      }
}

Example:

abstract class AbstractClassName
{
      abstract abstractMethod();
     
       <datatype> methodName()
     {
          Statements;
      }
}

Note : We use a keyword called abstract for specifying either a class or a method . 
  1. If this keyword is used before class then that class is called as Abstract class
  2. If it is used before method then such method is called as Abstract Method.
  3. A class must declare as abstract if that class contains at-least one abstract method.
  4. Abstract classes must be over-ridden by sub classes and the sub class should not have any abstract methods . If present such classes are also declared as Abstract Classes.
  5. We Cant create an object for abstract class . we can do this only by overriding all the abstract  methods of abstract class by the derived class . Otherwise the derived class must also an abstract class . 
  6. Object is created to the last derived class which overrides all the abstract methods.
  7. All the methods can be called using the object of the derived class when that class overrides all the abstract methods present in the base classes.

Program using one abstract class 



abstract class AbstractClass1
{
void concreteMethod()
{
System.out.println("Iam in concrete Method defined in Base class");
}

abstract void abstractMethod();
}
class AbstractClass2 extends AbstractClass1
{
void abstractMethod()
{
System.out.println("Iam in abstract Method defined in Derived class");
}
}

class AbstractClassTest
{
public static void main(String[] args) 
{
AbstractClass2 ac2=new AbstractClass2();

ac2.concreteMethod();

ac2.abstractMethod();
}
}

Save As:

AbstractClassTest.java

Compile:

javac AbstractClassTest.java

Run:

java AbstractClassTest


Output:

Iam in concrete Method defined in Base class

Iam in abstract Method defined in Derived class.

Note:


In the above program we have created an object for the derived class where it is not an abstract and as it is inherited its base class we can call all the methods in the base class by using the object of derived class.

Program using two abstract class 

import java.util.*;

abstract class CarNameClass
{
String carName;

int carCost;

void carName(String carName)
{
this.carName=carName;
}

abstract void carCost(int carCost);

abstract void carDetails();
}

abstract class CarCostClass extends CarNameClass
{
void carCost(int carCost)
{
this.carCost=carCost;
}
}

class CarDetailsClass extends CarCostClass
{
void carDetails()
{
System.out.println("CarName fromCarNameClass :"+carName);

System.out.println("carCost from CarCostClass :"+carCost);
}
}

class CarTestClass
{
public static void main(String[] args) 
{
Scanner sc=new Scanner(System.in);

System.out.println("enter carName:");

String name=sc.next();

System.out.println("enter carCost: ");

int cost=sc.nextInt();

CarDetailsClass carDet=new CarDetailsClass();

carDet.carName(name);

carDet.carCost(cost);

carDet.carDetails();
}
}

Save As:

CarTestClass.java

Compile:

javac CarTestClass.java

Run:

java CarTestClass

Input:

enter carName:

BMW

enter carCost:

500000

Output:

CarName fromCarNameClass :BMW

carCost from CarCostClass :500000

Note:

In the above program we have two abstract classes namely CarNameClass and CarCostClass whose values are passed from the CarTestClass and are displayed using CarDetails class which is not abstract .


Disadvantage :

Using the abstract classes we can not provide 100% abstraction.
By Using abstract classes we cant implement multiple inheritance .

This all can be avoided by a concept of interface which is an another approach for abstraction. 

Interfaces:

These are same as classes which have variables and Methods. But the variables in this interfaces are always "private static final" where as methods are "public abstract". 
Interfaces have only abstract methods (i.e.. Methods with no body). 

It is not necessary to declare the variables as private static final but whatever the variables declared in the interfaces are treated as public static final by the JVM.

In the same way there is no necessity for the methods to declare as public abstract but whatever the methods declared in an interface will be considered as public abstract methods. 

Interfaces are defined using a keyword "interface" followed by the name .

Syntax:

interface InterfaceName
{
    <datatype> VariableName="value";
      
     <return type> Methodname();
}

Example:

interface InterfaceClassName
{
     int year=2016;
     
     void interfaceMethodName();
      
}


Program using an interface implemented by single class

 interface Interface1
{

String NAME="void Main Technologies";

void printName();}

class InterfaceClass implements interface1
{
String address="DILSUKNAGAR";

public void printName() 
{ 
System.out.println("Name of Institute:"+NAME); 
}

public void printAddress()
{  
System.out.println("Address of Institute:"+address); 
}
}
class InterfaceClassTest
{
public static void main(String[] args)
  { 
InterfaceClass ic=new InterfaceClass();

ic.printName();

ic.printAddress() ;
}
}

Save As:

InterfaceClassTest.java

Compile:

javac InterfaceClassTest.java

Run:

java InterfaceClassTest

Output:

Name of Institute:void Main Technologies

Address of Institute: DILSUKNAGAR

Note:

From the above program we have implemented only one interface and overridden the method in the interface.

Interface and Abstract class:

when one class implements an interface it must override all the methods in that particular interface otherwise the class which implements these interface must declared as abstract class otherwise compilation error .

Syntax:

interface <interface name>
{
     method declaration();


abstract class  <class name > implements <interface name >
{
    not defined all the methods in the interface ;
}

class <class name 1> extends <class Name>
{
     completely defined all the methods ;
}

Program using Interface and Abstract Class


interface i1
{
public void name();
}

abstract class Abstract1 implements i1
{
public void location()
{
System.out.println("Address of Institute: Dilsuknagar");
}
}

class NoAbstract extends Abstract1
{
public void name()
{
System.out.println("Name : voidMainTechnologies");
}
}

class InterfaceClassTest2 
{
public static void main(String[] args) 
{
NoAbstract na=new NoAbstract();

na.name();

na.location();
}
}

Save As:

InterfaceClassTest2.java

Compile:

javac InterfaceClassTest2.java

Run:

java InterfaceClassTest2

Output:

Name of Institute:void Main Technologies

Address of Institute: DILSUKNAGAR

Note:

From the above program we have implemented only one interface and the abstract method has not been overridden in the subclass . Hence, the subclass must be abstract class otherwise raises a compilation error. So , this abstract class has been extended to other class in which we have overridden the method present in the interface.

Now , in the above we have created an object for last sub class which is not abstract and accessed all the remaining methods in the main class .

Using more than one  interface:


A class can implement more than one interfaces separated by comma . we must override all the methods in the interface's we have implemented , otherwise the class which implemented these interface's must be abstract class otherwise compilation error.

Syntax :

class <class name> implements <interface 1> ,<interface 2>
{
    over ride methods in both interfaces ;
}

Program using two interfaces implemented by single class


interface SuperInterface

{

public void name();

}



interface SubInterface 

{
public void location();
}

class SuperSubInterface implements SubInterface,SuperInterface
{
public void name()
{
System.out.println("Name: void main technologies");
}
public void location()
{
System.out.println("Address of Institute: Dilsuknagar");
}
}
class InterfaceClassTest3 
{
public static void main(String[] args) 
{
SuperSubInterface ssi=new SuperSubInterface();

ssi.name();

ssi.location();
}
}

Save As:

InterfaceClassTest3.java

Compile:

javac InterfaceClassTest3.java

Run:

java InterfaceClassTest3

Output:

Name of Institute:void Main Technologies

Address of Institute: DILSUKNAGAR

Note :

In the above program we have implemented two different interfaces and overridden the methods in both interfaces's . 

Interface's with inheritance:

An interface can also extend another interface same as class . The methods and variables present in the super interface will also be inherited in the  sub interface . Here to perform inheritance between two interface's we use extends keyword.

Syntax:

interface <interface name 1>
{
     method declaration();
}

interface <interface name 2> extends <interface name 1>
{
     method declarations();
}

class <class name> implements <interface name 2>
{
     override methods of both interfaces ;
}

Program using inherited interfaces


interface InheritedInterface1

{

public void name();

}


interface InheritedInterface2 extends InheritedInterface1
{
public void location();
}
class InheritedInterface implements InheritedInterface2
{
public void name()
{
System.out.println("Name : void Main Technologies");
}

public void location()
{
System.out.println("Address of Institute
 : Dilsuknagar");
}
}
class InheritedInterfaceTest 
{
public static void main(String[] args) 
{
InheritedInterface ii=new InheritedInterface();

ii.name();

ii.location();
}
}

Save As:

InheritedInterfaceTest.java

Compile:

javac InheritedInterfaceTest.java

Run:

java InheritedInterfaceTest

Output:

Name of Institute:void Main Technologies

Address of Institute: DILSUKNAGAR

Note :

From the above we have extended two interface's together and the sub interface has been implemented by another class . By this we can say that even interface's also follows Inheritance . 
By these concept instead of implementing two interfaces we can implement the sub interface and can acquire all the functionalities in both interfaces.

Inner Interface's:

An interface in another interface is called as inner interface . We can access the outer interface data into inner interface but we can not access the data of inner interface in outer interface.


Syntax:

interface <outer interface name>
{
    method declaration();

        interface <inner interface name>
         {
               method declarations();
         }
}

class <class name> implements <outer interface name>.<inner interface name >
{
     override methods of both inner and outer interface ();
}

Program using inner Interface's


interface InheritedInterface1
{
public void name();

interface InnnerInterface2
{
public void location();
}
}

class InnerInterface implements InheritedInterface1.InnerInterface2
{
public void name()
{
System.out.println("Name : void Main Technologies");
}

public void location()
{
System.out.println("Location : Dilsuknagar");
}
}
class InheritedInterfaceTest 
{
public static void main(String[] args) 
{
InnerInterface ii=new InnerInterface();

ii.name();

ii.location();
}
}

Save As:

InnerInterfaceTest.java

Compile:

javac InnerInterfaceTest.java

Run:

java InnerInterfaceTest

Output:

Name of Institute:void Main Technologies

Address of Institute: DILSUKNAGAR

Note :

Above program demonstrates how to use the inner interface's  and how can be implemented in the implemented class. These type of interface's can also be called as nested interface's .



Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java

Java Inner Classes