Question

In Java describe the differences between an abstract class and an interface. Provide examples for both...

In Java describe the differences between an abstract class and an interface. Provide examples for both citing the differences. Which would you choose to use and why?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

-> Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.

-> abstract keyword is used to create an abstract class and it can be used with methods also whereas interface keyword is used to create interface and it can’t be used with methods.

-> Abstract classes can have constructors but interfaces can’t have constructors.

-> Abstract classes methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.

-> We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.

-> Subclasses use extends keyword to extend an abstract class and they need to provide implementation of all the declared methods in the abstract class unless the subclass is also an abstract class whereas subclasses use implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface.

Example of abstract class:

public class Salary extends Employee {

private double salary;  

public Salary(String name, String address, int number, double salary) {

super(name, address, number);

setSalary(salary);

}

public void mailCheck() {

System.out.println("Within mailCheck of Salary class ");

System.out.println("Mailing check to " + getName() + " with salary " + salary);

}

public double getSalary() {

return salary;

}

public void setSalary(double newSalary) {

if(newSalary >= 0.0) {

salary = newSalary;

}

}

public double computePay() {

System.out.println("Computing salary pay for " + getName());

return salary/52;

}

}

public class AbstractDemo {

public static void main(String [] args) {

Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);

Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);

System.out.println("Call mailCheck using Salary reference --");

s.mailCheck();

System.out.println("\n Call mailCheck using Employee reference--");

e.mailCheck();

}

}

Examples for interface class:

public interface Shape {

public String LABLE="Shape";

void draw();

double getArea();

}

public abstract class ShapeAbs implements Shape {

public double getArea() {

return 0;

}

}

Add a comment
Know the answer?
Add Answer to:
In Java describe the differences between an abstract class and an interface. Provide examples for both...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT