Final Exam Flashcards
INHERITANCE
REDUCES PROGRAM DEVELOPMENT TIME
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses
class MountainBike extends Bicycle {
// new fields and methods defining // a mountain bike would go here
}
The direct superclass of a subclass (specified by the keyword extends in the first line of a class declaration) is the superclass from which the subclass inherits. An indirect superclass of a subclass is two or more levels up the class hierarchy from the subclass.
Superclass Subclass
Shape Circle, Triangle, Cube
Student UndergradStuedent
Employee Faculty, Staff
Single Inheritance
A class is derived from one direct superclass. In multiple inheritances a class is derived from more than one direct superclass.
Java does not support multiple inheritance
Subclass
is more specific than its superclass and represents a smaller group of objects
Every object of a subclass is also an object of that class’s superclass.
However a superclass object is NOT an object of its class’s subclass
is-a relationship
Represents inheritance
A object of a subclass also can be treated as an object of its superclass
Ex: Vehicle, represents all vehicles, cars, trucks, boats, bicycles etc…..
**public class Animal**{ }
public class Mammal extends **Animal{** }
public class Reptile extends **Animal**{ }
public class Dog extends **lMamma**{ }
A Has-a relationship
Represents composition,
A class object contains refrences to objects of other classes
Ex: A car has a, steering wheel
Superclasses and Subclasses
Single inheritance relationships form treelike hierarchial structures,
a superclass exists in a hierarchical relationship with its subclass
Protected Members
A superclass’s public memebers are accessible whereever the program has a reference to an object of that superclass or one of its subclass
A superclass’s private members can be accessed directly only within the superclass’s declaration
A superclass’s protected members have an intermediate level of protection btw public and private access. They can be accessed by memebers of its subclass, and by members of other classes in the SAME package
Superclass’s private members are hidden in its subclasses and can be accessed only through the public or protected methods inherited from the superclass\
Overridden canbe accessed by a super and a dot(.) seperator.
Relationship btw Superclass and Subclasses
Subclass CANNOT access the private members of its superclass, but CAN access the non-private members
Subclass, can invoke a constructor of its superclass by using the keyword “super” followed by parathensis with superclass arguments. Must appear in the first statement of the subclass constructor body
_______ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities
Inheritance
A superclass’s _____ members can be accessed in the superclass declaration and in subclass declarations.
Public and Protected
In a ______ relationship, an object of a subclass can also be treated as an object of its superclass.
is-a or inheritance
In a _____ relationship, a class object has references to objects of other classes as members
has-a or composition
In a single inheritance, a class exists in a ____ relationship with its subclasses
hierarchical
A superclasses _____ members are accessible anywhere that the program has a reference to an object of that superclass or to an object of one of its subclasses
Public
When a object of a subclass is instantiated a superclass ______ is called implicityly or explicityly.
constructor
Subclass constructors can call superclass constructors via the ______ keyword
super
Polymorphism
Enables us to write programs that process objects that share the same superclass as if they’re all objects of the superclass
Can simplify programming
We can design and implement systems that are easily extensible. The only parts of a program that must be altered to accomadate new classes are those that require direct knowlege of the new classes that you add to the hierarchy.
If a class contains at least one abstract method, its an_____ object
Abstract
may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
public abstract class GraphicObject { // declare fields // declare non-abstract methods abstract void draw(); }
Classes from which objects can be instantiated are called _____ classes
Concrete
_____ involves using a superclass variable to invoke methods on superclass and subclass objects, enabling you to “program in the general”
Polymorphism
Methods that are not interface methods and that do not provide implementations must be declared using the keyword _____
abstract
Casting a reference stored in a superclass variable to a subclass type is called _______
downcasting