You can use the ____ modifier with methods when you do not want the method to be overridden.

  • In Interface, a class can implement multiple interfaces, whereas the class can inherit only one Abstract Class.
  • In Interface does not have access modifiers. Everything defined inside the Interface is assumed to have a public modifier, whereas Abstract Class can have an access modifier.
  • The Interface cannot contain data fields, whereas the abstract class can have data fields.
  • Interfaces help define a class’s peripheral abilities, whereas an abstract class defines the identity of a class.

You can use the ____ modifier with methods when you do not want the method to be overridden.

A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have zero or more abstract methods. i.e., methods without a body. It can have multiple concrete methods.

Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.

Abstract classes cannot be instantiated.

Important Reasons For Using Interfaces

  • Interfaces are used to achieve abstraction.
  • Designed to support dynamic method resolution at run time
  • It helps you to achieve loose coupling.
  • Allows you to separate the definition of a method from the inheritance hierarchy

Important Reasons For Using Abstract Class

  • Abstract classes offer default functionality for the subclasses.
  • Provides a template for future specific classes
  • Helps you to define a common interface for its subclasses
  • Abstract class allows code reusability.

The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.

An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)

Difference between Interface and Abstract Class in Java

An abstract class permits you to make functionality that subclasses can implement or override whereas an interface only permits you to state functionality but not to implement it. A class can extend only one abstract class while a class can implement multiple interfaces.

Parameters Interface Abstract class
Speed Slow Fast
Multiple Inheritances Implement several Interfaces Only one abstract class
Structure Abstract methods Abstract & concrete methods
When to use Future enhancement To avoid independence
Inheritance/ Implementation A Class can implement multiple interfaces The class can inherit only one Abstract Class
Default Implementation While adding new stuff to the interface, it is a nightmare to find all the implementors and implement newly defined stuff. In case of Abstract Class, you can take advantage of the default implementation.
Access Modifiers The interface does not have access modifiers. Everything defined inside the interface is assumed public modifier. Abstract Class can have an access modifier.
When to use It is better to use interface when various implementations share only method signature. Polymorphic hierarchy of value types. It should be used when various implementations of the same kind share a common behavior.
Data fields the interface cannot contain data fields. the class can have data fields.
Multiple Inheritance Default A class may implement numerous interfaces. A class inherits only one abstract class.
Implementation An interface is abstract so that it can’t provide any code. An abstract class can give complete, default code which should be overridden.
Use of Access modifiers You cannot use access modifiers for the method, properties, etc. You can use an abstract class which contains access modifiers.
Usage Interfaces help to define the peripheral abilities of a class. An abstract class defines the identity of a class.
Defined fields No fields can be defined An abstract class allows you to define both fields and constants
Inheritance An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces.
Constructor or destructors An interface cannot declare constructors or destructors. An abstract class can declare constructors and destructors.
Limit of Extensions It can extend any number of interfaces. It can extend only one class or one abstract class at a time.
Abstract keyword In an abstract interface keyword, is optional for declaring a method as an abstract. In an abstract class, the abstract keyword is compulsory for declaring a method as an abstract.
Class type An interface can have only public abstract methods. An abstract class has protected and public abstract methods.

Sample code for Interface and Abstract Class in Java

Following is sample code to create an interface and abstract class in Java

Interface Syntax

interface name{ //methods }

Java Interface Example:

interface Pet { public void test(); } class Dog implements Pet { public void test() { System.out.println("Interface Method Implemented"); } public static void main(String args[]) { Pet p = new Dog(); p.test(); } }

Abstract Class Syntax

abstract class name{ // code }

Abstract class example:

abstract class Shape { int b = 20; abstract public void calculateArea(); } public class Rectangle extends Shape { public static void main(String args[]) { Rectangle obj = new Rectangle(); obj.b = 200; obj.calculateArea(); } public void calculateArea() { System.out.println("Area is " + (b * b)); } }

When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version. This assumes that the base class version of the member is visible, as it would already be hidden if it were marked as private or, in some cases, internal. Although you can hide public or protected members without using the new modifier, you get a compiler warning. If you use new to explicitly hide a member, it suppresses this warning.

You can also use the new keyword to create an instance of a type or as a generic type constraint.

To hide an inherited member, declare it in the derived class by using the same member name, and modify it with the new keyword. For example:

public class BaseC { public int x; public void Invoke() { } } public class DerivedC : BaseC { new public void Invoke() { } }

In this example, BaseC.Invoke is hidden by DerivedC.Invoke. The field x is not affected because it is not hidden by a similar name.

Name hiding through inheritance takes one of the following forms:

  • Generally, a constant, field, property, or type that is introduced in a class or struct hides all base class members that share its name. There are special cases. For example, if you declare a new field with name N to have a type that is not invocable, and a base type declares N to be a method, the new field does not hide the base declaration in invocation syntax. For more information, see the Member lookup section of the C# language specification.

  • A method introduced in a class or struct hides properties, fields, and types that share that name in the base class. It also hides all base class methods that have the same signature.

  • An indexer introduced in a class or struct hides all base class indexers that have the same signature.

It is an error to use both new and override on the same member, because the two modifiers have mutually exclusive meanings. The new modifier creates a new member with the same name and causes the original member to become hidden. The override modifier extends the implementation for an inherited member.

Using the new modifier in a declaration that does not hide an inherited member generates a warning.

Examples

In this example, a base class, BaseC, and a derived class, DerivedC, use the same field name x, which hides the value of the inherited field. The example demonstrates the use of the new modifier. It also demonstrates how to access the hidden members of the base class by using their fully qualified names.

public class BaseC { public static int x = 55; public static int y = 22; } public class DerivedC : BaseC { // Hide field 'x'. new public static int x = 100; static void Main() { // Display the new value of x: Console.WriteLine(x); // Display the hidden value of x: Console.WriteLine(BaseC.x); // Display the unhidden member y: Console.WriteLine(y); } } /* Output: 100 55 22 */

In this example, a nested class hides a class that has the same name in the base class. The example demonstrates how to use the new modifier to eliminate the warning message and how to access the hidden class members by using their fully qualified names.

public class BaseC { public class NestedC { public int x = 200; public int y; } } public class DerivedC : BaseC { // Nested type hiding the base type members. new public class NestedC { public int x = 100; public int y; public int z; } static void Main() { // Creating an object from the overlapping class: NestedC c1 = new NestedC(); // Creating an object from the hidden class: BaseC.NestedC c2 = new BaseC.NestedC(); Console.WriteLine(c1.x); Console.WriteLine(c2.x); } } /* Output: 100 200 */

If you remove the new modifier, the program will still compile and run, but you will get the following warning:

The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.

C# language specification

For more information, see The new modifier section of the C# language specification.

See also