Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the purpose of using an \"abstract\" class when using inheritance? When

ID: 3748749 • Letter: W

Question

What is the purpose of using an "abstract" class when using inheritance? When a method is defined as "abstract", what must the programmer make sure to do? What is the one thing you cannot do with abstract classes? 8. 9. What is the difference between overriding and overloading? Provide examples to explain the difference 10. Consider the following two classes. public class Dog public class UnderDog extends Dog public void acto public void acto System.out printf un eato: super acto: System.out print( sleep) public void eat public void eato System.out.print eat; super.eat0: System.out print bark; Assume that the following declaration appears in a client program. Dog fido new UnderDogll: What is printed as a result of the ca fido.act0? Output:

Explanation / Answer

8.
Q: Purpose of abstract class
An "Abstract" class is declared with abstract keyword. This class may include abstract method.

The purpose of abstract class is to define the default behavior for the inherited objects.

Q: If method is defined as "abstract", what should the programmer do?
If a method is declared as "abstract", in the deriving classes, the developer can either override the method by providing an implementation or the developer can continue to define the method as abstract (then the subclass too becomes abstract)

Q: Onething you cannot do with abstract classes?
With an Abstract class, we cannot create an instance of the class


9.
Overloading - In a class, if two or more methods have the same name but with different parameters.

Overriding - In a hierarchy of classes, if two methods have same name and parameters. One of the methods is defined in parent class and the other method is declared in the sub class.
This allows the derived class to define a different implementation of the method.

Example:

Overloading example

public class Calculator {

public int add(int a, int b) {
return a + b;
}
  
public float add(float a, float b) {
return a + b;
}
  
}

Overriding example:

public class Shape {

public void draw() {
  
System.out.println("I am a shape");
  
}

}

public class Circle extends Shape {

public void draw() {
  
System.out.println("I am a circle");
  
}

}


10.
Output is

run
eat
sleep

Explanation: As the object of UnderDog class is created, it invokes the "act" method in the UnderDog class. This method internally invokes the super classes' act method - This will print "run" and then "eat". Finally, "sleep" gets printed.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote