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

8. What is the purpose of using an \"abstract\" class when using inheritance? Wh

ID: 3748987 • Letter: 8

Question

8. 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? 9 What is the dilfference 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 print(run):( eato: super act): System.out print sleep public void eat0 public void eat0 System.out printear): super.eato System.out print bark) Assume that the following declaration appears in a client program Dog fido - new UnderDogl: new UnderDogl: What is printed as a result of the ca fido.act)? Output:

Explanation / Answer

8.

Abstract meaning:

Existing in thought or as an idea but not having a physical or concrete existence.

In Java and other prog. languages there is a concept of abstract classes (which is little different from Interfaces).

For a class to be abstract it needs to be declared with abstract keyword.

eg.   

Abstract classes are mostly used in inheritence. They act as a template for the inheriting or extending classes.

for eg. In the above code block.

A person can extend class Extrovert.

or there can be an abstract class Employee, and different type of professionals can extend eg, IT, managment etc are type of employee.

An abstract class can include abstract methods or they may have no abstract method.

Main characteristics of Abstract classes is that they cannot be instantiated, but they can be extended.

Abstract methods can't have a body. The implementation is left to the inheriting class.

eg. i

Now if a non abstract class extends class Extrovert, it's compulsory for it to implement all abstract methods of Extrovert class. In our e.g. a person class can implement express() as they like, may be a person talks too much or sings or dances.

When a method is abstract, it shouldn't have any defination in the abstract class itself, but must be implemented when it's inherited by another non-abstract class.

Now, since an abstract class acts like a template for other classes and it also has non-implemented abstract methods, an abstract class can not be instantiated. i.e they are not concrete.

9.

Method overloading is a common term used in many prog. languages.

A function/method is differentiated from other methods by its name.

eg.

int foo();

is different from

int bar();

But languages which support overloading has another way to differentiate i.e by method parameters.

e.g.

are both different methods.

So, when we call using foo(1), and foo(1,2.1), compiler knows at compile-time which method to execute.

So, Overloading is to define different methods in the same class but with different type or number of parameters.

On the other hand, overriding comes into play during inheritence,

lets understand with an eg.

We can see that in overriding we have same method signature with same type/no. of arguments. But one methods overrides or hides the definition of the parent method. @override annotation is not necessary, but its good to tell compiler that we'r intentionallyoverriding this definition.

10.

output:

>runeatsleep

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