Abstract Classes, Interfaces, Static Variables

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What are two important facts about abstract classes?

1. Cannot instantiate an object of an abstract class (the use of the new expression is illegal) 2. Subclass must override all abstract methods

What happens if you don't override abstract methods in every subclass?

A compile-time error.

What is a static field?

A field that doesn't belong in any one object

What is an interface like?

A fully abstract class, but with more restrictions; a formal promise about what the class will provide/contract enforced by compiler

What can't you have in non-abstract classes?

Abstract methods.

What must a class that implements an interface do?

It must implement all methods in implemented interfaces.

Example of static method

Java's predefined class Math contains static max and sqrt int maximum = Math.max(2,3)

Is a static field automatically constant?

No, you must declare it final to have it be constant

What can you have in an abstract class?

Non-abstract methods.

What can a static method NOT reference?

Non-static things: Can't reference data field in class that's not static Can't invoke non-static method of class unless it creates local object of class and uses it to invoke nonstatic method It CAN reference static fields and methods. STATIC STICKS WITH STATIC.

What problem does an interface solve?

Problem of multiple inheritance

What is a static method?

Still a member of class, use class name instead of object name to invoke the method

Abstract classes are better than interfaces when:

They have data fields in common.

What are the restrictions on methods in interfaces?

They must be public

What are the restrictions of fields of interfaces?

They must be public, static, and final + contain initializer == essentially constants

Why is it preferred if a static field is private?

You want to ensure access occurs only through appropriate methods.

Example of abstract class:

public abstract class Shape { }

Example of abstract method:

public abstract double area(); Does not have a body - just a method header and a semicolon

Example of an interface:

public interface Whistler { void whistle(); int MEANING_OF_LIFE = 42; class Human extends Mammal implements Whistler { }


संबंधित स्टडी सेट्स

Structure and Function of Cartilaginous Joints

View Set

Second Quarter Exam Review (Survey of the Bible)

View Set

Chapter 35: Assessment of Immune Function

View Set

Chapter 7: Economic Factors, Business Information, Strategies, and Risks

View Set

chapter 21 and 22 test (alcohol and illegal drugs)

View Set