Friday, 17 April 2015

Inheritence

1.If super class and sub class having same variables those are not overridden

Example:

class Mammal{
String name="furry";
String makeNoise()
{
return "generic noise";
}
}

class Zebra extends Mammal
{
String name="stripes";
String makseNoise()
{
retrun "bray";
}
}


public class ZooKeeper
{
public static void main(String arg[])
{
new ZooKeeper().go();
}
void go()
{
Mammal m=new Zebra();
System.out.println(m.name+m.makeNoise());
}
}

Output:
furry bray

 here stripes is not  present in the output because variables are not overrideen.only funcions are overridden because of that m.makeNoise() given the output as bray istead of generic noise.

2.Static methods are not overridden

Example:

class Singer
{
public static String sing()
{
return "la";
}
}

public class Tenor extends Singer{
public static String sing()
{
return "fa";
}
public static void main(String arg[])
{
Singer s=new Tenor();
System.out.println(s.sing);
}
}

Output:
la instead of fa because static methods are not ovverridden

Tuesday, 14 April 2015

Interfaces


  • Mutliple inheritence is not supported in java.Means one subclass must have only one super class.
  • Multiple inheritence is achieved with help of interfaces.one  class can implements multiple interfaces
  • If a class extends the class as well interfaces the naming convention is first need to extend the class afterwards implements the interface.
Ex: class Foo extends Foo1 implements Foo2,Foo3