1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

Java Tutorials

When we go for Abstract and Interface in Java?

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used.
In an abstract class the partially common behaviour is given a concrete implementation.
Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.
For Example - Abstract Class
abstract public class Animal {
public eatChicken(){
System.out.println("Eat Chicken");
}

}

public Lion extends Animal{
}
public Tiger extends Animal{
}

Both Lion and Tiger class has common behaviour eatChicken() and all the implementaion put into eatChicken() method of super calss.
so we go for abstract class.

Example- Interface

Interface Animal{
public eat();
}
public Lion implements Animal{
public eat(){
System.out.println("Lion eat Non Veg");
}
}
public Cow implements Animal{
public eat(){
System.out.println("Cow eat Veg");
}
}
Both Lion and Cow class has different behaviour for eat() and they have different implementations. so we go for Interface.

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy