Download interface - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
2

An interface is a reference type in Java, it is similar to class, it is a
collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface.

Along with abstract methods an interface may also contain constants,
default methods, static methods, and nested types. Method bodies exist
only for default methods and static methods.

Writing an interface is similar to writing a class. But a class describes the
attributes and behaviours of an object. And an interface contains
behaviours that a class implements.
3
An interface is different from a class in several
ways:
 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can
appear in an interface must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces
4
The interface keyword is used to declare an interface. Here is a
simple example to declare an interface:
//General Syntax
interface interfaceName
{
//abstract methods
//final variable
}
interface myInterface
{
void method1();
void method2();
int size=11; //final and static
}
class myClass implements myInterface
{
//define method1 and method2
}
5
As shown in the figure given below, a class extends another class,
an interface extends another interface but a class implements an
interface.
6
interface printable
{
void print();
}
class A implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A obj = new A();
obj.print();
}
}
7
If a class implements multiple interfaces, or an interface
extends multiple interfaces i.e. known as multiple
inheritance.
8
interface Printable
{
void print();
}
interface Showable
{
void show();
}
class A implements Printable,Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[])
{
A obj = new A();
obj.print();
obj.show();
}
}
9
A class implements interface but one interface extends
another interface .
interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
class Testinterface2 implements Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[])
{
Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();
}
}
10
 The object cloning is a way to create exact copy of an
object. For this purpose, clone() method of Object class is
used to clone an object.
 The java.lang.Cloneable interface must be implemented
by the class whose object clone we want to create. If we
don't implement Cloneable interface, clone() method
generates CloneNotSupportedException.
 The clone() method is defined in the Object class. Syntax of
the clone() method is as follows:
 protected Object clone() throws CloneNotSupportedExcept
ion
11
 To clone objects of a class, over-ride the clone() method of
the Object class.
class MyClass implements Cloneable
{
public object clone()
{
MyClass cloned=super.clone();
//clone members if required
return cloned;
}
}
12
 The clone() method saves the extra processing task
for creating the exact copy of an object. If we
perform it by using the new keyword, it will take a lot
of processing to be performed that is why we use
object cloning.
Advantage of Object cloning
 Less processing task
13
class Student implements Cloneable
{
int rollno;
String name;
Student(int rollno,String name)
{
this.rollno=rollno;
this.name=name;
}
As you can see in this example, both
reference variables have the same value.
Thus, the clone() copies the values of an
object to another. So we don't need to
write explicit code to copy the value of
an object to another.
public Object clone()throws CloneNotSupportedException
{
return super.clone();
}
public static void main(String args[])
{
try
{
Student s1=new Student(101,"Ahmad");
If we create another object by new
keyword and assign the values of
another object to this one, it will require
a lot of processing on this object. So to
save the extra processing task we use
clone() method.
Student s2=(Student)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}
catch(CloneNotSupportedException c){}
}
}
14




Java inner class or nested class is a class i.e. declared inside the
class or interface.
We use inner classes to logically group classes and interfaces in one
place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including
private data members and methods.
Syntax of Inner class
class Java_Outer_class
{
//code
class Java_Inner_class
{
//code
}
}
15
class OutterClass
{
private int x=300;
class InnerClass
{
private int ans;
public void getValue()
{
display();
System.out.println("Outer class value :"+x);
}
}
public void display()
{
System.out.println("Outer class display");
}
public void accessInner()
{
InnerClass obj=new InnerClass();
obj.ans=300;
System.out.println("Inner class value :"+obj.ans);
}
}
16
public class myMain
{
public static void main(String args[])
{
OutterClass outObj=new OutterClass();
OutterClass.InnerClass inobj=outObj.new InnerClass();
//OR
/*
OutterClass.InnerClass inobj=new OutterClass().new InnerClass();
*/
}
}
17
There are basically three advantages of inner classes in java. They are as
follows:
1) Nested classes represent a special type of relationship that is it can
access all the members (data members and methods) of outer
class including private.
2) Nested classes are used to develop more readable and maintainable
code because it logically group classes and interfaces in one place
only.
3) Code Optimization: It requires less code to write.
18