Midterm Flashcards
Base class, parent class and super class are terms that are _______
synonymous…they mean the same thing!
Inheritance
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.
Benefits of Inheritance
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 do you use inheritance?
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.
Can you have more than one super, base or parent class?
Single inheritance- any class can only have one super class.
what is inherited by the super class from the sub-class?
any methods and attributes of the super class that are NOT DECLARED AS PRIVATE
Can a super class inherit from a subclass?
nope it only goes one way
What is the mother of all classes in Java?
The object class
Defining features of an abstract method: (T!)
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.
The new class or derived class is referred to as a direct sub-class of the class from which it is derived. (T!)
The original class is called the base class or super class. (T!)
When you derive a new class from a base class, the process is additive. Your new class will contain:
all of the public or protected methods and attributes of both the base class and the subclass.
IMPORTANT NOTE: attributes and methods marked private are not inherited by sub-classes. (T!)
Setting attributes and methods to private makes them inaccessible outside of the class in which they are defined. (T!)
Benefit of inheritance: if you have to change a method or attribute in the super class…
you just change it in just one class. (T!)
inheritance will make that change accessible to all of the subclasses below it.
class:
extend :
super class:
sub-class:
- 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
Access Modifiers
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.