Вы находитесь на странице: 1из 35

Java Abstraction

Abstraction
• Showing only essential features by hiding background
detail is called Abstraction.

• For example:

1.phone call, we don't know the internal processing.

2. Laptop, we don’t know how RAM, Hard Dis ,Mother oard


etc worked .
Abstraction

Ways to achieve Abstraction


There are two ways to achieve abstraction in java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract class
• A class that is declared with abstract keyword, is known as
abstract class in java.

abstract class T
{
…….
}
• To declare a class abstract, you simply use the abstract
keyword before the class keyword at the beginning of the
class declaration.

• It can contain abstract method and non-abstract


(instance method) methods.
What is Abstract method
• A method that is declared as abstract and does not have
body(Definition) is known as abstract method.

Syntax:
abstract return_type name();

• example : abstract void add();


Instance method vs abstract
method
Instance method Abstract method

void display() abstract void display();


{
//logic
}

int result(int x,int y) abstract int result(int x,int y) ;


{
//logic
}
Abstract class
Point to Remember

1. Any class that contains one or more abstract methods must


also be declared abstract.
abstract class T
{
abstract void display();
abstract void result();
void add()
{
………..
}
}
Abstract class
2. We can not create object of abstract class. That is, an
abstract class cannot be directly instantiated with the new
operator.

abstract class T
{
abstract void display();
}
Public static void main(String args[])
{ invalid

T t1=new T();
}}
What is Abstract class
• Such objects would be useless, because an abstract class is
not fully defined.

• Also, you cannot declare abstract constructors, or abstract


static methods.
What is Abstract class
abstract class Abs
{ invalid
abstract Abs()
{
//logic
}
invalid
abstract static void display()
{
//logic
}

}
Abstract class

• The abstract method is defined in sub class.

• Any subclass of an abstract class must either


implement(define) all of the abstract methods in the
superclass, or be itself declared abstract.
Ex.
abstract class A
{
What is
abstract void callme();
Abstract class
// abstract method
void callmetoo() //Abstract class
{
System.out.println("This is a concrete method.");
}
}
class B extends A
{
void callme()
{ // Sub class
System.out.println("B's implementation of callme."); }
}
class AbstractDemo
{
public static void main(String args[]) {
B b = new B(); //main class
b.callme();
b.callmetoo(); } }
abstract class Shape
{
What
abstract void draw();
is Abstract class
}
class Rectangle extends Shape
{
void draw()
{ System.out.println("drawing rectangle"); }
}
class Circle1 extends Shape
{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1
{
public static void main(String args[]){
Rectangle r=new Rectangle();
s.draw();
} }
abstract class Bike{

{
Bike()
What is Abstract class
System.out.println("bike is created");
}
abstract void run();
void changeGear()
{
System.out.println("gear changed");}
}
class Honda extends Bike
{
void run(){System.out.println("running safely..");} }
class TestAbstraction2{
public static void main(String args[]){
Honda obj = new Honda();
obj.run();
obj.changeGear(); }}
Core Java Programming
Java Interfaces
Interfaces...
• Interface in java is similar to class that have
variables and methods but there is measure
difference between class and interface , interface
have only abstract method and constant variables.

• To declare a interfaces the interface keyword is


used

• The interface in java is a mechanism to achieve


fully abstraction.
Interfaces...
Syntax:
interface interface_name.
{
constant variables ;
abstract method;
}
Ex:
interface Helloin
{
public static final int x=5;
public abstract void add();
Interfaces...

• java interfaces and methods inside interfaces are


implicitly(automatically) public and abstract either
you put these keywords or not.

• The variables of interfaces are implicitly public


static and final either you put these keywords or
not.

• The java compiler adds public and abstract


keywords before the interface method and public,
Interfaces...


Interfaces... Valid or not valid
interface I1 interface Test
{ {
int x=5; int x=67;
void display(); void display()
} {
//….
}
interface I1 }
{
final int x=5;
abstract void display();
}
Interfaces...
interface I1 interface Test
{ {
int x; int k=67;
void demo(); void hello();
public d1(); void result()
} {
//….
}
}
Interfaces...

• Java interfaces are implemented using a class that


implements interface using implements keyword.

• Means abstract method of interface are defined in


class that implements interface.
Interfaces...

//Syntax of using interfaces


interface J1
{
//Methods and variables of interface;
}
class T1 implements J1
{
//Here all methods of interface are define
}
//Simple example that demonstrate the concept of interface
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A6 obj = new A6();
obj.print();
}
}
Implementing Interfaces...

• Property 1 :Once an interface has been defined,


one or more classes can implement that interface.
interface Area
{
final static float pi=3.14F;
float compute (float x, float y);}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return x*y; } }
class Circle implements Area
{
public float compute(float x, float y) { return (pi*x*x); }
}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle r = new Rectangle();
Circle c = new Circle();
System.out.println(“Area of rectangle :” +a.compute(10.0f,20.of));
System.out.println(“Area of rectangle :” +a.compute(10.0f,0.0f));
} }
Implementing Interfaces...

• Property 2 :
• a class can implements more than one interface at
a time, the interfaces are separated with a comma.
class A1 implements I1,I2,I3
{
//definition of I1,I2 and I3 interfaces
}
Interfaces...
interface I1
{ void demo1(); }
interface I2
{ void demo2(); }
class Test implements I1,I2
{
public void demo1()
{
System.out.println("this is method of I1 interface");
}
public void demo2()
{
System.out.println("this is method of I2 interface");
}
public static void main(String args[])
{
Test t1=new Test();
t1.demo1();
t1.demo2();
}}
Implementing Interfaces...

• Property 3 :
• An interface can extends other interface .
interface I1
{
void demo1();
}
interface I2 extends I1
{
public void demo2();
Interface can be Extended...
interface A
{ void meth1();
void meth2();}
interface B extends A
{
void meth3();
}
class MyClass implements B
{
public void meth1() { System.out.println("Implement meth1()."); }
public void meth2() { System.out.println("Implement meth2()."); }
public void meth3() { System.out.println("Implement meth3()."); }
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1(); ob.meth2(); ob.meth3(); } }
Interfaces...
Property 4:
an interface can extends multiple interface simultanesly.

interface B
{
//…
}
interface C
{
//…
}
interface A extends B , C
{
//……………
}
Interface can Extended...
interface I1
{
void demo1();
}
interface I2
{
void demo2();
}
interface I3 extends I1,I2
{
void demo3();
}
Interface can be Extended...
class Test1 implements I1,I2,I3
{
public void demo1()
{
System.out.println("this is method of I1 interface");
}
public void demo2()
{
System.out.println("this is method of I2 interface");
}
public void demo3()
{
System.out.println("this is method of I3 interface");
}
public static void main(String args[])
{
Test t1=new Test();
t1.demo1();t1.demo2();t1.demo3();
}
}
Interfaces...
• Java does not support multiple inhertance but it give implementation of multiple
inheritance using interfaces.
interface C
{
//…..
}
class B
{
//…
}
class A extends B implements C
{
//…………..
}

Above statement is a valid statement.


Core Java

Вам также может понравиться