Abstraction
Abstraction
Abstraction
Abstraction
• Showing only essential features by hiding background
detail is called Abstraction.
• For example:
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.
Syntax:
abstract return_type name();
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.
}
Abstract class
{
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.
•
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...
• 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
{
//…………..
}