Documente Academic
Documente Profesional
Documente Cultură
if we declare any concrete method in an interface compile time error will come.
By default members will be treated as public static final variables so it expects some
value to be initialized.
The variables inside interfaces are static final variables means constants and we
cannot create object from interface so there is no need of constructor in interface that
is the reason interface doesn’t allow us to create constructor.
class Test
{
public static void main(String[] args)
{
Parent p =new Parent();
Child1 c1 = new Child1();
Child2 c2 = new Child2();
p = c2;
System.out.println(p instanceof Child1); //false
System.out.println(p instanceof Child2); //true
18. Interface
Interface is a pure abstract class. They is syntactically similar to classes, but
you cannot create instance of an Interface and their methods are declared
When you create an interface it defines what a class can do without saying
Abstract class is a class which contain one Interface is a Java Object containing method
or more abstract methods, which has to be declaration but no implementation. The classes
implemented by its sub classes. which implement the Interfaces must provide the
method definition for all the methods.
Abstract class is a Class prefix with an Interface is a pure abstract class which starts with
abstract keyword followed by Class interface keyword.
definition.
Abstract class can also contain concrete Whereas, Interface contains all abstract methods
methods. and final variable declarations.
Abstract classes are useful in a situation Interfaces are useful in a situation that all
that Some general methods should be properties should be implemented.
implemented and specialization behavior
should be implemented by child classes.
20. String Comparison
String comparison can be done in 3 ways.
Using == operator
== operator compares two object references to check whether they refer to same instance.
This also, will return true on successful match.
String s1 = "Java";
String s2 = "Java";
String s3 = new string ("Java");
test(s1 == s2) //true
test(s1 == s3) //false
By compareTo() method
compareTo() method compares values and returns an int which tells if the string compared
is less than, equal to or greater than the other string. It compares the String based on
natural ordering i.e alphabetically. Its general syntax is,
int compareTo(String str)
String s1 = "Abhi";
String s2 = "Viraaj";
String s3 = "Abhi";
s1.compareTo(S2); //return -1 because s1 < s2
s1.compareTo(S3); //return 0 because s1 == s3
s2.compareTo(s1); //return 1 because s2 > s1