Prev Next

Java / Abstract Class

Could not find what you were looking for? send us the question and we would be happy to answer your question.

Concrete class.

Refers to the class with complete implementation i.e. no abstract methods and only concrete methods.

Can I create an abstract variable?

No, abstract keyword is applicable only for classes and methods.

What is an abstract class?

A class that has only partial implementation and has abstract methods.

Can an abstract class be instantiated?

No. Abstract classes cannot be instantiated since it is abstract and not concrete.

Can a abstract class be defined without any abstract methods?

Yes. It cannot be instantiated since declaring a class as abstract restrict it to be not instantiated on its own.

What is the difference between an Interface and an Abstract class?
  • All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods.
  • A class may implement a number of Interfaces, but can extend only one abstract class.
  • In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
  • Abstract classes can implement interfaces without even providing the implementation of interface methods.
  • Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
  • Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
  • An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
Can an abstract method be declared static in Java?

No. An abstract method cannot be static. You cannot override a static method, so making it abstract would not make any sense.

Java 8, difference between abstract class and Interface.

Both abstract class and interface are used for abstraction by hiding the internal implementation of the feature and only showing the functionality to the users.

method types: Interface can have only abstract methods. From Java 8, interface can have default and static methods also. Abstract class can have abstract and non-abstract methods.

Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.

Type of variables: Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.

Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

Implementation: Abstract class can provide the implementation of interface while Interface cannot provide the implementation of abstract class.

Inheritance vs Abstraction: A Java interface can be implemented using keyword "implements" and abstract class can be extended using keyword "extends".

Multiple implementation: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

Can abstract class have Constructor?

Yes, but its object cannot be created by calling the constructor instead it is invoked during constructor chaining.

Can abstract class implement an interface in Java?

Yes, an abstract class can implement the interface by using implements keyword. Since they are abstract, they don’t need to implement all methods.

Can an abstract class be final in Java?

No.

Can abstract class have a static method in Java?

Yes, the abstract class can declare and define static methods. It is not recommended though as per design principles.

What is an abstract method in Java?

An abstract method is a method without a body. You just declare method, without defining it and use abstract keyword in method declaration. All method declared inside Java Interface are by default abstract. Here is an example of an abstract method in Java.

public void abstract anExampleAbstractMethod();
Can an abstract class have the main method?

Yes, it is also a static method and it can execute the abstract class.

public abstract class AbstractClassMain {
	
	public static void main(String[] args) {
		System.out.println("Hello from abstract class");
	}

}

Can we call an abstract method from a non-abstract method?

Yes.

Can we mark a local inner class as abstract?

Yes.

public class OuterClass {
    public void instanceMethod(){
        abstract class InnerClass{
        }
    }
}

Below is an example that illustrates the usage of local inner abstract class.

public class SendNotification {

	public enum NotifyBy {
		MESSAGE, EMAIL, PHONECALL
	}

	public Runnable createNotification(NotifyBy type) {
		abstract class ProtoType implements Runnable {
			public void run() {
				prepareContent();
				sendNotification();
			}

			abstract void sendNotification();

			private void prepareContent() {
				// notification message content
			}
		}

		switch (type) {
		case EMAIL: {
			return new ProtoType() {
				void sendNotification() {
					System.out.println("Sent using email.");
				}
			};
		}
		case MESSAGE: {
			return new ProtoType() {
				void sendNotification() {
					System.out.println("communicated by text message.");
				}
			};
		}

		}

		return new ProtoType() {
			void sendNotification() {
				System.out.println("communicated by phone call.");
			}
		};
	}

	public static void main(String[] args) {
		new Thread(new SendNotification().createNotification(NotifyBy.MESSAGE)).start();
		;
	}

}
Can abstract method include throws clause?

Yes. Abstract methods can throw exceptions.

Can an abstract class have a final concrete method?

Yes. An abstract class can have the final concrete method. See the below example.

public class AbstractClassFinalConcreteMethod {

	public static void main(String[] args) {
		Parent p = new Child();
		p.parentMethod();
	}
}

abstract class Parent {
	
	final void parentMethod() {
		System.out.println("Final Method in a abstract class");
	}
}

class Child extends Parent {
	
}
Output:
Final Method in a abstract class

In the above example, the parent abstract class has a final concrete method 'parentMethod' which is allowed and also called directly from the parent reference.

What is Double Brace initialization in Java?

Double brace initialization creates an anonymous class derived from the specified class and provides an initializer block.

new ArrayList<String>() {{
   add("ONE");
   add("TWO");
}};

The outer brace creates the anonymous inner class while the inner represents the initializer block.

«
»
Enum

Comments & Discussions