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?
-> 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;
}
}
In Java describe the differences between an abstract class and an interface. Provide examples for both...
what is abstract class in java? for example, abstract class animal and subclass cat which extends abstract class, and the test class which will call cat class to show if it yawns, scratch and whatnot. I used to create class animal but it was not abstract, so I am not sure the purpose of it. Another question, I created interface, by right clicking the package, I think the teacher saying interface is acts independent? and not part of hierarchy like...
JAVA question Interface and Abstract Class (20 pts): There are three types of triangles in terms of how many sides are equal: • Equilateral Isosceles Scalene There are three types of triangles in terms of the degrees of interior angles: • Right · Acute . Obtuse Right Isosceles triangle is a right triangle, as well as an isosceles triangle. Triangle 590° Right triangle Isosceles triangle Right isosceles triangle Create a public interface Triangle, add the following method signatures: • double...
Exercise 1: Understanding Object Oriented Design Principles [Please Answer clearly using python, No java code only python] What are the differences between an abstract class and an interface class? Present 02 examples one of an abstract class and one of an interface class in python
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...
QUESTION 2: Elementary Java Programming (a) Explain what a Wrapper class is in Java and provide an example of one and what it [06] "wraps". (b) You have been given an array of elements to search through for a particular value. [02] Which loop would you choose between a for loop and a for-each loop and why? (c) In Java what is the difference between a StringBuilder and StringBuffer? What [02] are they used for? Total: 10
Abstract Classes and Interfaces Java This particular assignment is doing research and programming. I want you to do a research report of the following: Ways that abstract classes promotes software reusability, decreases time programming, and helps reduce the number of errors in software. Research the using abstract classes and interfaces. I would like for you to be creative and come up with your own example of abstract classes and interfaces in Java by writing your own abstract class and interface,...
Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...
describe the importance of cell-material interface for biomaterials . Provide examples 2
Is it possible to write a Java interface from which you could create an anonymous class, but for which you can't create a lambda expression? Why or why not? Prove or demonstrate as necessary.
Explain the following object-oriented(OO) concepts with the aid of code examples (either C++ or Java): Inheritance Over-riding . Over-loading Describe any differences between C++ and Java in how these OO concepts are implemented? 3(c) 17 Marks] Describe C++ namespaces using a code example. Describe Java packages, again using a code example. How do C++ namespaces compare to Java packages? 3(d) [5 Marks] What are inline methods in C++/Java? Explain the terms accessor and mutator. Explain the following object-oriented(OO) concepts with...