Abstraction and Interface Concept Flashcards

(27 cards)

1
Q

What a class can’t do between normal class when it was declared as abstract?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a normal class also call? (Class can be instantiated)

A

concrete class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Create an abstract class Ball with hit methods

A
public abstract class Ball{
     public abstract int hit(int batSpeed);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to extend from Ball class?

public abstract class Ball{
     public abstract int hit(int batSpeed);
}
A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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;
    }
}
A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the different between abstract class and interface class?

A

A class can only inherit one abstract class / concrete class (extend) but interface class can inherit more than one (implements)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is this an error?

public abstract class D {
     private abstract void jump();
}
A

Yes, abstract methods cannot be private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can abstract class have constructor

A

Yes, it was used to initialize the abstract methods

But it can’t instantiated directly using the new keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you create an abstract field in abstact class?

public abstract class Ball{
     public abstract int oldScore;
     public abstract int hit(int speed);
}
A

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);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an abstract method?

A

A method declared with the abstract keyword that has no implementation (no body). It ends with a semicolon instead of curly braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can an abstract class exist without any abstract methods?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What must be done if a subclass does not implement all inherited abstract methods?

A

The subclass itself must also be declared as abstract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of a constructor in an abstract class?

A

To initialize the fields of the abstract class. It is called from the subclass’s constructor using the super() keyword, either explicitly or implicitly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can an abstract method be declared as final?

A

No. abstract methods are designed to be overridden, whereas final methods cannot be. The two keywords are mutually exclusive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can an abstract method be declared as static?

A

No. static methods belong to the class and cannot be overridden, which contradicts the purpose of an abstract method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an Interface in Java?

A

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.

17
Q

Key difference between an abstract class and an interface regarding constructors?

A

An abstract class can have constructors. An interface cannot.

18
Q

When should you use an abstract class over an interface?

A

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).

19
Q

When should you use an interface over an abstract class?

A

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.

20
Q

What is the rule for access modifiers when implementing an abstract method?

A

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.

21
Q

Can an abstract class implement an interface?

A

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.

22
Q

Can you declare a variable using an abstract class as its type?

A

Yes. This is a core principle of polymorphism. For example: Ball b = new BaseBall();, where Ball is abstract and BaseBall is a concrete subclass.

23
Q

Can fields (instance variables) be declared abstract?

A

No. The abstract keyword can only be applied to classes and methods.

24
Q

Can a concrete method in an abstract class call one of its own abstract methods?

A

Yes. This is the ‘Template Method’ design pattern. At runtime, polymorphism ensures the subclass’s specific implementation of the abstract method is executed.

25
Can an abstract class extend a concrete class?
Yes. An abstract class is free to inherit from a concrete class. It can add new abstract methods that its own concrete subclasses must then implement.
26
``` public abstract class Account { private int balance; public Account(int initialBalance) { this.balance = initialBalance; } public abstract void deposit(int amount); public int getBalance() { return balance; } } public class SavingsAccount extends Account { // Line Z: Constructor implementation public SavingsAccount(int initial) { // Assume super() call is here } @Override public void deposit(int amount) { // Implementation here } public void showBalance() { System.out.println("Current: " + balance); // Line W } } ``` What must be in Line Z?
``` super(initial); ```
27
``` public abstract class Processor { public abstract void process() throws IOException; } public class FileProcessor extends Processor { @Override public void process() throws Exception { // Line S // file processing logic } } ``` Will this execute?
No, the subclass method is allowed to throw fewer or same exception as the superclass method but cannot throw broader checked exceptions