What a class can’t do between normal class when it was declared as abstract?
The abstract class cannot be instantiated
Ball b = new Ball(); // Error
new keyword is creating a Ball object, but since Ball is abstract class, we can’t create object instance
What does a normal class also call? (Class can be instantiated)
concrete class
Create an abstract class Ball with hit methods
public abstract class Ball{
public abstract int hit(int batSpeed);
}How to extend from Ball class?
public abstract class Ball{
public abstract int hit(int batSpeed);
}public class BaseBall extends Ball{
public int hit(int batSpeed){
// code here
}
}Subclass must provide an implementation for each abstract method in the abstract class
Create BaseBall subclass with this abstract class and use all methods
public abstract class Ball {
public abstract int hit(int batSpeed);
public abstract int score(int newScore);
public String message(String message) {
return "Ball says: " + message;
}
}public class BaseBall extends Ball{
public int hit(int batSpeed){
return batSpeed;
}
public int score(int newScore){
return newScore;
}
}
BaseBall b = new BaseBall();
int speed = b.hit(10);
int newScore = b.score(20);
String message = "Speed : " + speed + ", Score : " + newScore ;
String returnMessage=b.message(message);
System.out.println(returnMessage);
public String message(String message) {
return "Ball says: " + message;
}This concre method in abstract class is optinal, we can override it or just use it
What is the different between abstract class and interface class?
A class can only inherit one abstract class / concrete class (extend) but interface class can inherit more than one (implements)
Is this an error?
public abstract class D {
private abstract void jump();
}Yes, abstract methods cannot be private
Can abstract class have constructor
Yes, it was used to initialize the abstract methods
But it can’t instantiated directly using the new keyword
Can you create an abstract field in abstact class?
public abstract class Ball{
public abstract int oldScore;
public abstract int hit(int speed);
}No, but we can create a normal fields (instance variable in an abstract class)
public abstract class Ball {
public int oldScore;
public abstract int hit(int speed);
}What is an abstract method?
A method declared with the abstract keyword that has no implementation (no body). It ends with a semicolon instead of curly braces.
Can an abstract class exist without any abstract methods?
Yes. Declaring a class abstract without abstract methods is legal. The primary purpose is simply to prevent anyone from creating an instance of that class.
What must be done if a subclass does not implement all inherited abstract methods?
The subclass itself must also be declared as abstract.
What is the purpose of a constructor in an abstract class?
To initialize the fields of the abstract class. It is called from the subclass’s constructor using the super() keyword, either explicitly or implicitly.
Can an abstract method be declared as final?
No. abstract methods are designed to be overridden, whereas final methods cannot be. The two keywords are mutually exclusive.
Can an abstract method be declared as static?
No. static methods belong to the class and cannot be overridden, which contradicts the purpose of an abstract method.
What is an Interface in Java?
A reference type that is a collection of abstract methods. A class implements an interface, thereby inheriting its abstract methods. It is another way to achieve 100% abstraction.
Key difference between an abstract class and an interface regarding constructors?
An abstract class can have constructors. An interface cannot.
When should you use an abstract class over an interface?
When you want to share code among several closely related classes or provide a common base class with some default implementation (concrete methods) and state (fields).
When should you use an interface over an abstract class?
When you expect that unrelated classes will implement it (e.g., Comparable, Serializable) or when you need to take advantage of multiple inheritance of type.
What is the rule for access modifiers when implementing an abstract method?
The access level cannot be more restrictive than the abstract method’s. For example, a protected abstract method can be implemented as protected or public, but notprivate.
Can an abstract class implement an interface?
Yes. An abstract class can implement an interface but does not need to provide implementations for the interface’s methods. That responsibility is passed to the first concrete subclass.
Can you declare a variable using an abstract class as its type?
Yes. This is a core principle of polymorphism. For example: Ball b = new BaseBall();, where Ball is abstract and BaseBall is a concrete subclass.
Can fields (instance variables) be declared abstract?
No. The abstract keyword can only be applied to classes and methods.
Can a concrete method in an abstract class call one of its own abstract methods?
Yes. This is the ‘Template Method’ design pattern. At runtime, polymorphism ensures the subclass’s specific implementation of the abstract method is executed.