Jul 16, 2019

What is Java Abstraction? | How to Implement Abstraction In Java?

We don’t apply abstraction on data we only hiding data and abstraction perform on functionality.

Show functionality hide complexity

Or show function hide implementation

Abstraction is another way to achieve run time polymorphism

Abstract is a keyword as well as modifier and it use before class and member function but you can’t use before Datamember and constructor.

Abstract method

Abstract class A{

Abstract void show();

}

Abstract method should be inside abstract class and abstract method is only declaration

and its complementation in another class.

NOTe: abstract method can’t be static,final ,private not allowed

What is abstract class ?

normal class

Class A{

--normal class

}

Abstract class

abstract class A{

--abstract class

}

Abstract class may and may not have abstract method.abstract class cant be instanciated .or we can we can create an object o f abstract class.

Class A extends B{

--normal class {

}

-----------------

abstract class B{

abstract void hello(){}

abstract void hi(){}

}

When class A extends class B then class must have define all the method inside B.

Class A{

Void hi() { }

Void hello() { }

}

Or make this class also abstract then no need to define the method in class A. like this

Abstract class A extends B{

}

Can we create references of Abstract class?

Ans- yes

A a=new B();

Here object type is B and reference variable type is A and its an alos example of upcasting.

We can hold the child class object into parent class Reference variable.

This is very important use.

Abstract class Base{

Abstract int x=10; // error we can’t use abstract before data member

Int x=10;

Void show(){

Syso(“base”);

}

Abstract void display() ;

Abstract Base(){ //we cant put abstract before constructor only

Make it Base() { }

}

}

NOTE: can we have a constructor inside abstract class? Yes we have but you think that we can’t create an object of abstract class and you then there is no constructor inside abstract class but constructor is there whether we define or not compiler automatically provide constructor in our class. Because when Base class extends by some other class and this class is not have constructor then rule will be break because when child class extending parent class then 1 parent class constructor run after that child class that’s why there must be constructor in abstract class. Why we can’t have static abstract method together because static method is associate with class static binding and abstract associated with object and its dynamic binding.

Why abstract private not together ?

Because we can take it out the private method out of the class and we have to override abstract method outside the class so that's why.

Why abstract final not together ?

Because final method can’t be override.

Why we can’t create an object of abstract class?

Because what we will we do with that object because inside abstract class only method declaration is there so even though we cant use that method thats why .

What is the difference between Abstract class and interface?

And interface represent only behavior of object.and we cant create of object of an interface.inside interface no init block no static block no constructor to use interface we write implements and interface name which class going to Use this interface and to use abstract class we use extends.

Through interface we achieve multiple inheritance but not with abstract class

When we use interface/abstract class?

Ans; when we have requirement specification we should go with interface and when we know requirement and as well as some implementation but not 100% then we should go for abstract

Examle 1 Abstraction

Birds.java program

Now I’m going to illustrate an example of dynamic loading here because in my example 1 now next when an abstract class is implemented by many classes so we have to create object of every class so we don’t want to create an object of each class which implementing an abstract class .

Dynamic object Creation using Class.forName

Example 2 Abstraction

OperateAC.java program

Interface is another way to achieving the abstraction.Interface is a blueprint of class.why blueprint?

Ans. As class is a blueprint for an object class is a collection of data member and function and we save it Test.java compile and run and bytecode of the class is also generated as well as interface is save as Test.java run and compile same and byte code is also generated

Interface

Interface is a contract between 2 partie programmer and programming language. In java every work where java wants you to define method and java calls method and declare method and method will be executes yours for this java creates interface.via interface we can achieve dynamic bindIng, Achieve run time polymorphism,multiple inheritance.when in project level we have only requirement then create an interface and keep it.fro java 8 in interface static method may be there because static method is statically bind and interface dynamically bind and we can have main method also.

How a class uses interface ?

Interface I

{

Void show() ;

}

-------------------------------

class A implements I

{ public void show() { } }

If class A is implements interface then it has to implements all the method present in interface with public access privilege if class don’t Want to implement methods then make it abstract .we can’t create an object of an interface.

Can a class implements more than 1 interface?

Yes by using comma , but this is not called multiple inheritance its called multiple implements keyword

Can interface extends another interface?

Yes by using extends keyword its an example of inheritance in interface

We can not declare variable inside interface we must have to initialize it

Interface I

{

//Int x; we can’t declare

Int x=10;

Void show();

Static void disp() { }

}

---------------------------------

class A implements I

{ public void show() { } }

Example 3

shows all about interface.

Note : We can’t invoke the child personal method with parent reference. Any interface can’t extends any class but extends any interface. Object class is the parent class of all the classed but not for interf Interface can’t implements any class not extends any class.and Any class can’t extends interface implements interface. And a interface extends more than 1 interface simultaneously separated by comma this is the multiple inheritance in interface in java Example of a class implements 2 interface its called Multiple implementation of a Class but not multiple inheritance using 2 Interface in java

Example 4.

When an interface extends more than 1 interface Is called multiple inheritance though interface in java illustrate example in Example 5.

Note: class A extends B implements I1,I2,

Class A implements I1,I2 extends B its not allowed

Tricky Question if we have an interface:

we wanna use this interface in another but we don’t want to override both method I wanna override only1 and don’t to make my class generally with trick with adapter class.

Abstract so its not possible

Interface

{

Void show();

Void hi(); }

Class A implements //then what can we do we will see in Example 6

{

Public void show(){

}

}

Can we create an object of an Interface?

Ans: Generally people say no ya offcoarse no but lets see something

Interface

{

Void show();

}

Class Test{

Public static void main(String args[])

{

new I(){ //So this is basically anonymous nested class for this fo

public void show() { }

}; anonymous it must have any parent either make any abstr

} -act class or Interface make it Parent and why new Is wri

} there because its rule where we declare it at that we create a

Object of that anonymous class now here anonymous class Implementing I interface.

Now Question we don’t want to implements an interface but we want To override
 
method forcefully so for this also we use anonymous class to override method
 
compulsory in some real life if we wish to pay from

Phone pe then we necessary have phonepe in our mobile to transfer money.its a fix
 
condition we want from everybody. Example 7 illustrate

Interface

{

Void show();

Public String toString();

}

Class Test implements I

{

Public void show(){

System.out.println(“ Show“)

}

Public static void main(String args[])

{

I i =new Test();

i.show(); // it will give error because we can’t call child method with parent reference but
 
when we write i.toString it will compile how this line when this method id not
 
declare in interface then you think that interface extends Object Class but
 
interface can’t extends class so how its working. Its working when we compile
 
the program compiler will declare the methods Of object class not extends
 
Object class that’s why and every class extending Object class internally so
 
method is internally will be present in Class Test and Definition is in Interface
 
so this working like this. Not a magic. We are only override only 1 method.

}

}

#AbstractClassInJava #JavaAssignmentHelp #JavaProjectHelp #JavaProgrammingHelp #Java #AbstractClass