Abstract methods and classes

Ace your homework & exams now with Quizwiz!

When can an class implement an interface, but not implement all interface methods?

When it is an abstract class

abstract class

declared with abstract keyword. May or may not includ abstract methods. Cannot be instantiated, but can be subclassed

What is implicit about interface methods that aren't declared as default or static?

they are implicitly abstract.

Differences between abstract classes and interfaces

1. abstract classes can have non-static and non-final fields 2. abstract classes can define public, protected, and private concrete methods 3. interface fields automatically public, static, and final. All defined declared are public.

When to use interfaces

1. you expect unrelated classes will implement your interface. e.g. Comparable and Cloneable are implemented by many unrelated classes 2. you want to specify behavior of a particular data type, but not concerned about who implements its behavior 3. You want to take advantage of multiple inheritance of type

When to use abstract classes

1. you want to share code among closely related classes 2. you expect that classes extending your abstract class have many common methods/fields, or require access modifiers other than public 3. you want to declare non-static or non-final fields. Enables you to write methods that can access/modify the state of the object to which they belong

What restrictions apply to a subclass of an abstract class?

Must provide implementations for all abstract methods in the parent class. If it does not, then the subclass must be declared abstract

abstract method

method declared without an implementation and without braces, followed by semicolon.

If a class includes abstract methods...

the class itself must be declared abstract.


Related study sets

Course 3 Week 2 Wireless protocols and WPA

View Set

Physics Test 1 Practice Problems

View Set

Infant and Child Development- Exam 2 Quiz Questions 5-9

View Set

Western Civilization II: Chapter 17

View Set

Chapter 7: Project Cost Management

View Set