Midterm Flashcards

1
Q

Base class, parent class and super class are terms that are _______

A

synonymous…they mean the same thing!

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

Inheritance

A
Inheritance in Java:  is really about what methods and attributes in a base (or super) class are accessible from inside a subclass that is derived (or extended) from the base class. (T!)
If a method or attribute of the base class is accessible from the subclass, then that method or attribute is said to have been inherited from the base (or super) class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Benefits of Inheritance

A

The properties of one class (attributes and methods) of can be inherited (be accessed by) by another class.
Q. What use is that?
A. This saves the coder from having to write the same code in several different classes. (Yay!)

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

how do you use inheritance?

A

you build a class hierarchy.
Hierarchy: an ranked order of things. (T!)
Example: a family tree is an example of a hierarchy.
Superclass or parent class or base class- this is the class that donates or allows access to its data and methods.
sub-class or child class or derived class- the class that inherits or is granted access to the data and methods of the parent.

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

Can you have more than one super, base or parent class?

A

Single inheritance- any class can only have one super class.

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

what is inherited by the super class from the sub-class?

A

any methods and attributes of the super class that are NOT DECLARED AS PRIVATE

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

Can a super class inherit from a subclass?

A

nope it only goes one way

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

What is the mother of all classes in Java?

A

The object class

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

Defining features of an abstract method: (T!)

A

1) the Java keyword abstract appears in the method declaration header, and2) the method body is absent. There is no body code in an abstract method.
The body code is written in concrete versions of the method that we will code in the sub-classes. The abstract method is over-ridden in the sub-classes. We’ll see an example of this a little later.

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

The new class or derived class is referred to as a direct sub-class of the class from which it is derived. (T!)

A

The original class is called the base class or super class. (T!)

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

When you derive a new class from a base class, the process is additive. Your new class will contain:

A

all of the public or protected methods and attributes of both the base class and the subclass.

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

IMPORTANT NOTE: attributes and methods marked private are not inherited by sub-classes. (T!)

A

Setting attributes and methods to private makes them inaccessible outside of the class in which they are defined. (T!)

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

Benefit of inheritance: if you have to change a method or attribute in the super class…

A

you just change it in just one class. (T!)

inheritance will make that change accessible to all of the subclasses below it.

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

class:
extend :
super class:
sub-class:

A
  • a data type
  • to make a new class that inherits the contents of an existing class
  • a parent or base class
  • a child class that inherits or extends a super class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Access Modifiers

A

public: accessible from any class, anywhere. (T!)
private: no access from outside the class at all. (T!)
protected: accessible from any class in the same package, OR from any class that is a sub class of the class. This sub class could be located in another package and still have access rights. (T!)
NOTE: If you don’t specify any access modifier on an attribute or a method, Java will use the DEFAULT level of visibility, which means only classes in the same package will have access to the data members and methods. Any classes in other packages WILL NOT have access, even if they are sub-classes derived from the class.

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

When writing a constructor method for a derived class (or subclass), you must include a call to the constructor of the superclass by using the “___” keyword.

A
super
This must be the first line in your derived class constructor method. (T!)

If you forget to put it in, Java will try to actually put it in there for you when it compiles your file, but it will try to call the default constructor. Do you see a potential problem here?Hint: remember what happens to the default constructor if you code a constructor method?

17
Q

Method over-riding is different from method overloading. (T!)

A
Method overloading involves writing methods with the same name but different method signatures in the same class. (T!) 
Method over-riding involves writing methods with the exact same name and signature but placing them in different classes in a class hierarchy. (T!)
18
Q

what is an interface?

A

An interface is a special type of class that it provides a way of describing a set of abstract public methods and constant values.

19
Q

what does an interface hold?

A

An interface can hold data values, but by default the values will be constants. Java automatically makes the values public, static and final. (T!)

20
Q

what happens if you have abstract methods in an interface that is being implemented by a class

A

The abstract methods defined in the interface will be implemented (get their body code) in whatever class that implements the interface.

As of JDK 1.8, interfaces can now also hold methods with a default implementation, which means the method can have some body code.
As of JDK 1.8, These default methods can also be declared static.

21
Q

Interfaces have two main uses in Java (T!)

A

1) if you want a large number of classes to implement some common function through a method, you can write the abstract method declaration in an interface and then have each of the classes implement the interface.
2) Second main use for interfaces in Java is to simulate multiple inheritance. Often used when a class has to exhibit an “offbeat” behaviour not found in its super class. (T!)

22
Q

When you implement an interface you must:

A

Any coder implementing the interface must fulfill the contract and OVER-RIDE any abstract method in their code. At a minimum, they have to add a set of curly braces to the method declaration. Usually, you add body code that will be executed when the method is called.

23
Q

A static default method in an interface has body code and is owned by the interface, not by whatever class implements the interface.

A

NOTE: A static method cannot be over-ridden in the implementing class. You can call it using InterfaceName.methodName(). (T!)

24
Q

A lot of the interfaces in JDK1.8 now have default methods added to them
Now, if a coder implements one of these updated interfaces they have these options:

A

1) ignore the default method, which means it just gets inherited.
2) over-ride the default method with your own body code as needed.
3) just re-declare the default method in your class, which makes it abstract.

25
Q

what kind of inheritance do we use?

A
The type of inheritance we use in Java is called single inheritance. Java only supports single inheritance. (T!)
So, all Java class hierarchies are single parent families (T!)
26
Q

Differences Between Classes and Interfaces

A
As a coder, you cannot instantiate an object from an interface. The JRE can do this, but you the coder cannot. (T!)
However, you can declare reference variables of an interface type. Then any class that implements the interface can be assigned to the reference.
So… Iterable list = new ArrayList() ; // legal 
Remember that in Java a class can only have one superclass.
EX. public class Circle extends Ashape, Animal //illegal 
However, a class can implement one or more interfaces simply by separating the interfaces with commas. 
Suppose we had several interfaces that contained conversion factors for various types of conversions. 
EX. public class Converter implements SpeedConversionFactors, WeightConversionFactors, VolumeConversionFactors // legal
27
Q

The most important difference between a class and an interface is

A

is that the interface describes only abstract method headers without defining how these are implemented,(T!) (except for any default method implementations that might appear in the interface).

The abstract methods have no implementation and will be over-ridden in the implementing class.
The default methods will have a default implementation that can be over-ridden, unless the method is also declared static, in which case it cannot be over-ridden in the sub class.

28
Q
public interface ConversionFactors
{
	double INCH_TO_MM = 25.4;
	double OUNCE_TO_GRAM = 28.349523;
    double POUND_TO_GRAM = 453.5924;
	double HP_TO_WATT = 745.7;
	double WATT_TO_HP = 1.0/HP_TO_WATT;
} //end interface
A

NOTE: We do not have to declare these as public, static, and final. They are all public, static, and final by default in an interface.

29
Q

As of JDK 1.8 any interface that has just one abstract method is now also known as a

A

FUNCTIONAL INTERFACE.

30
Q

A class can implement one or more interfaces by separating the interface names with commas.

A

public interface MyInterface extends HisInterface, HerInterface, TheirInterface