//We can create the constructor of abstract
class, but we cannot create the constructor of interface why?
Constructor are mainly used to initialized the instance
variable, inside abstract class instance variable are final or non-final (so we can create constructor of abstract
class) and inside interface all instance variable are by default final(so we
cannot create the constructor of interface because final variable are initialized at the time of
declaration)
abstract public class PrintName {
String fname;
String mname;
public PrintName()
{
super();
}
public PrintName(String fname, String mname) {
super();
this.fname = fname;
this.mname = mname;
}
abstract public void printingFullName();
}
public class
PrintNameBase extends PrintName {
String lname;
public
PrintNameBase(String lname) {
super("govind","ballabh");
this.lname = lname;
}
@Override
public void
printingFullName() {
System.out.println("FullName="+fname+"
"+mname+" "+lname);
}
public static void
main(String[] args) {
PrintNameBase pnb=new
PrintNameBase("khan");
pnb.printingFullName();
}
}
o/p:-------------
FullName=govind
ballabh khan
public interface
PrintNameInterface {
String fname="govind";
String mname="ballabh";
String lname="khan";
public void
printName();
}
public class
Printbase implements PrintNameInterface {
@Override
public void
printName() {
System.out.println("Full
Name="+fname+" "+mname+"
"+lname);
}
public static void
main(String[] args) {
Printbase pb=new
Printbase();
pb.printName();
}
}
FullName=govind
ballabh khan
No comments:
Post a Comment