Interface
Interface
Interface
*/ interface Animal
interface
{ Sample
} void display();
} System.out.println(name);
}
Output:
Welcome
Shree
Example2
interface Drawable
{
void draw();
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println(“Drawing rectangle”);
}
}
Contd…
class Circle implements Drawable
{
public void draw()
{
System.out.println(“Drawing circle”);
}
}
public class TestInterface
{
public static void main(String args[])
{
interface MyInterfaceA
{
void display();
interface MyInterfaceB
{
void myMethod();
}
}
Contd..
{
public void myMethod()
{
System.out.println(“Nested interface method”);
}
public static void main(String args[])
{
A class is instantiated to create An interface can never be instanti- ated as the methods are
unable to perform any action on invoking.
Basic objects.
A class can implement any num- ber of interfaces and can An interface can extend multiple interfaces but cannot
extend only one class. implement any interface.
inheritance
Inheritance keyword extends implements
A class can have constructors to An interface can never have a constructor as there is
hardly any variable to initialize.
Constructor initialize the variables.
class sample
{
int a; float b;
sample()
{
a = 10;
b = 20;
}
}
class Mainclass
{
public static void main(String[] args)
{
ob2.a = 100;
System.out.println(ob1.a+” “+ob1.b);
System.out.println(ob2.a+” “+ob2.b);
}
}
Output:
10 20.0
100 20.0
100 20.0
Creating a copy using clone() method
• The object cloning is a way to create exact copy of an
object.
• The 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.
Syntax
protected Object clone() throws CloneNotSupportedExc
eption
Advantage of Object cloning
Student s2=(Student18)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}catch(CloneNotSupportedException c){}
}
}
Output:
101 amit
Types of Object cloning
1.The outer class can take over as many number of inner class objects as it needs.
2.If the outer class and the equivalent inner class both are public then any new class
can create an instance of this inner class.
3.The inner class objects do not get instantiated with outer class object
4.outer class can be able to call the private functions of inner class.
5.Inner class code has open way to all members of the outer class object that
contains it.
6.If the inner class has a variable with same name then the variable of outer class’s
can be accessed in given syntax
Outerclassname.this.variable_name
Types of Nested classes
Member Inner Class A class created within class and outside method.
Java ArrayList class uses a dynamic array for storing the elements.
Java ArrayList allows random access since array works at the index
basis.
In Java ArrayList class, operation is slow because a lot of shifting
wants to be occurred if any element is deleted from the array list.
ArrayList class hierarchy
ArrayList class declaration
• public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable,
Serializable
Constructors of Java ArrayList
Constructor Description
a1.addAll(a2);
Iterator itr=a1.iterator(); while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
STRINGS
chararray[0]=’J’;
chararray[1]=’a’;
chararray[2]=’v’;
chararray[3]=’a’;
Methods
compare()
concat()
equals()
split()
length()
replace()
compareTo()
intern()
• substring() etc.
The java.lang.String class implements Serializable,
Comparable and CharSequence interfaces.
CharSequence interface
used to represent sequence of characters.
implemented by String, StringBuffer and StringBuilder classes.
it can create string in java by using these 3 classes.
• The string objects can be created using two ways.
1. By String literal
2. By new Keyword
1.String Literal
• Java String literal is created by using double quotes.
• Example:
• String s=”welcome”;
• If the string already exists in the pool, a reference to the pooled instance is
returned
Contd..
2.By new keyword
String s1=”java”;
char c[]={‘s’,’t’,’r’,’i’,’n’,’g’};
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
Java String class methods
Method Description
char charAt(int index) returns char value for the particular
index
int length() returns string length
static String format(String
format, Object... args) returns formatted string
static String format(Locale l,
String format, Object... args) returns formatted string with given
locale
String substring(int beginIndex) returns substring for given begin index