0% found this document useful (0 votes)
59 views22 pages

WINSEM2020-21 STS3205 SS VL2020210500206 Reference Material I 18-Feb-2021 ABSTRACT CLASS IN JAVA

The document discusses abstract classes in Java. It defines abstract classes as classes that can contain abstract methods that do not have an implementation. Abstract classes cannot be instantiated, and subclasses must implement any abstract methods. The document provides examples of abstract classes and methods, and compares abstract classes to interfaces. It notes that abstract classes can provide default implementations, while interfaces only declare methods.

Uploaded by

RAJ MAHANTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views22 pages

WINSEM2020-21 STS3205 SS VL2020210500206 Reference Material I 18-Feb-2021 ABSTRACT CLASS IN JAVA

The document discusses abstract classes in Java. It defines abstract classes as classes that can contain abstract methods that do not have an implementation. Abstract classes cannot be instantiated, and subclasses must implement any abstract methods. The document provides examples of abstract classes and methods, and compares abstract classes to interfaces. It notes that abstract classes can provide default implementations, while interfaces only declare methods.

Uploaded by

RAJ MAHANTA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ABSTRACT CLASS

IN
JAVA
ABSTRACT CLASS

● A class which is declared with and abstract keyword is called as an abstract


classes

● An abstract class is a class that is marked with the abstract keyword and cannot be
instantiated

● If a class has at least one abstract method, then the class must be declared as
abstract
NEED OF ABSTRACT CLASS

● Abstract classes define an abstraction and leave concrete implementation details


to subclasses

● If an abstract type and its subclasses (concrete implementations) are well


defined, then the users of such classes can employ runtime polymorphism
NEED OF ABSTRACT CLASS

● Another reason to favor abstract classes is their ability to offer default


implementation

● Abstract classes are the perfect placeholder for common functionality required in
almost all of the subclasses of the abstract class
DEFINING ABSTRACT CLASS

● Any class that contains one or more abstract methods must also be declared
abstract
● an abstract class cannot be directly instantiated with the new operator
To declare an abstract method, use this general form

abstract type name(parameter-list);


EXAMPLE

A simple example of a class with an abstract method, followed by a class


which implements that method
class B extends A {
abstract class A { void callme() {
abstract void callme(); System.out.println("B's callme");
void callmetoo() { }
System.out.println("Concrete method "); }
} class Main {
} public static void main(String args[]) {
B b=new B();
b.callme();
b.callmetoo();
}
}
CONCRETE CLASS

● A concrete class is the first non abstract subclass that extends an

● Abstract class and is required to implement all inherited abstract methods

● An abstract class becomes useful when it is extended by a concrete


subclass
EXAMPLE

• First, note that Animal is marked as abstract and Walrus is not. In this
example, Walrus is considered the first concrete subclass of Animal

• Second, since Walrus is the first concrete subclass

• it must implement all inherited abstract methods

public abstract class Animal {


public abstract String getName();
}
public class walrus extends Animal {
//does not compile
}
ABSTRACT METHODS

• Abstract keyword is used to declare method called as abstract


• An abstract method contains a method signature, but no method body
• Instead of curly braces , an abstract methods will have a semicolon(;) at the end
EXAMPLE

public abstract class Emp {


private String name;
private String address;
public abstract double computepay();
//Remainder of class definition
}
EXAMPLE

Declaring a method has two consequences

The class containing it must be declared abstract

Any class inheriting the current class, must either override the abstract method

or declare itself abstract


RULES FOR ABSTRACT METHODS

● Abstract methods may only be defined in abstract classes

● Abstract methods may not be declared private or final

● Abstract methods must not provide a method

● body/implementation in the abstract class for which is it declared

RULES FOR ABSTRACT METHODS


DIFFERENCE

ABSTRACT INTERFACE

An abstract class can provide a default An interface only declare a method


implementation of a method
All classes implementing the interface must define
Derived classes can just use that definition that method
and need not define that method
If there are already many classes implementing an
It is possible to make changes to the interface, you cannot easily change that interface
implementation of an abstracts class
QUESTION:01

Which is true? (Choose all that apply.)

A. "X extends Y" is correct if and only if X is a class and Y is an interface.

B. "X extends Y" is correct if and only if X is an interface and Y is a class.

C. "X extends Y" is correct if X and Y are either both classes or both
interfaces.

D. "X extends Y" is correct for all combinations of X and Y being classes
and/or interfaces

Answer: C
QUESTION:02

What modifiers are implicitly applied to all interface methods?

A. Protected

B. Public

C. Private

D. default

Answer: B
QUESTION:03

Which statements are true for both abstract classes and interfaces?
(Choose all that apply)?

A. All methods within them are assumed to be abstract.

B. Both can contain public static final variables.

C. Both can be extended using the extend keyword

D. Both B and C

Answer: D
QUESTION:04

Which statements are true about the Concrete Class? (Choose all that
apply?

A. A concrete subclass can be declared as abstract.

B. A concrete subclass must implement all inherited abstract methods.

C. A concrete subclass must implement all methods defined in an


inherited interface.

D. A concrete subclass cannot be marked as final

Answer: A
QUESTION:05

Which statements are true about the following code? (Choose all that apply)

A. The CanBark interface doesn’t compile


interface HascalCords {
public abstract void makesound();
} B. A class that implements HasVocalCords
interface CanBark extends HascalCords { must override the makeSound() method.
public void bark();
} C. A class that implements CanBark
inherits both the makeSound() and
bark() methods.

D. A class that implements CanBark only


inherits the bark() method
Answer: C
QUESTION:05
QUESTION:06

abstract class Reptile {


public final void layEggs() {
System.out.println("Reptile laying eggs"); A. Reptile laying eggs
}
}
class Main { B. Lizard laying eggs
public static void main (String[] args) {
Reptile reptile =new Lizard(); C. Run time error
reptile.layEggs();
}
} D. The code wont compile
class Lizard extends Reptile {
public void layEggs() {
System.out.println("Lizard laying eggs");
}
}

Answer: D
THANK YOU

You might also like