The Gateway to Computer Science Excellence
+2 votes

Which of the following is/are correct with reference to Abstract class and interface?

  1. A class can inherit only one Abstract class but may inherit several interfaces
  2. An abstract class can provide complete and default code but an interface has no code
    1. a is true
    2. b is true
    3. Both a and b are true
    4. Neitehr a nor b are true
in Object Oriented Programming by Veteran (105k points)
recategorized | 629 views

1 Answer

+2 votes

ans is C 

An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern so b is true

he key technical differences between an abstract class and an interface are:

  • Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.

  • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).

  • When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.

  • Similarly, an interface extending another interface is not responsible for implementing methodsfrom the parent interface. This is because interfaces cannot define any implementation.

  • A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces so a is true

  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

by Boss (49.3k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,280 answers
104,840 users