• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/29

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

29 Cards in this Set

  • Front
  • Back

What is inheritance

Operates on the premise that many objects have similarities to a larger more general object. Inheritance allows a programmer to create many subclasses based off of once larger SuperClass

How do you create a SuperClass and a SubClass?

SuperClass is a regular old class with fields and methods than can be used in similar, more specific classes




SubClass is also a regular old class, separate from the SuperClass but in the same package except, the class declaration has this syntax




Public Class SubClassName extends


SuperClassName{

What is the relationship between a superclass and subclass as far a calling methods go

when a subclass extends a superclass, it has all the same members (hold private members) as the superclass because they both get created in memory when you declare a subclass in the main method




The superclasses constructor will always be executed just before the subclass constructor




the reverse is never true, you cannot use a superclass to call its subclasses members

How to use a SuperClass's constructor

in the subclass constructor, use the keyword super(argument passed here); this must the 1st line of code in the subclass's constructor because the superclass constructor must execute 1st




if you dont use the keyword super, the default or if it has it, the no-arg constructor will be used. This equivalent to just typing




super();




in the 1st line of of the subclass constructor

More on the superclass Constructor

if a superclass does have one or more constructors then you MUST use the super() keyword to define the parameters for one of the constructors

how to override superclass methods in a subclass

- in order to actually override a superclass's methods in a subclass, the subclass methods needs to have the same name and accept the same variable type or else it will be a different method or an overloaded method




**if using a superclass object, it will not be overridden by a subclass. only way is to use a subclass with an overridden method




How to use a superclass method inside of an overridden subclass method

- if you want to call the superclass's method inside of the subclass method that overrides it, just use the keyword "super":




super.methodName();

How to be sure a superclass method does not get overridden?

use the keyword "final" in the superclass method declaration

the "protected" access specifier

used when you want methods from the same class, a subclass or classes from the same package to access the fields of your class.




Its advised not to use this when possible because it can cause data leaks. Final is better

the "protected" access specifier and inheritance

public access grants access to the member from any class in the same package, protected does the same.




The difference is, protected grants access to subclasses that are in a different packages as well. This is not true for the public access specifier

inheritance of many classes

you can inherit as many classes as you want in a chain. This class A is the giant class, class B inherits class A's members and class C is a small class. Class C inherits both class A and class B's members

The Object class

Every class in Java extends the object class. In other words every class in java is a subclass of the Object class which has some methods




- toString is one which returns the object name followed by @ and then its hexidecimal hashcode




- equals is another method that will check to see if two objects are the same by comparing their toString methods. It will return true if they are the same and false if not

polymorphisim: consider Class_A the super class and Class_B the subclass

an example of ploymorphism:




Class_A turd = new Class_B();




since class B is a Class A type thru inheritance, you can definea class A variable using a class B object. the reverse is not true

polymorphisim and using overriden methods in superclass variable type

When performing a polymorphic declaration, keep in mind that any overridden methods in the subclass will be used instead of the Superclass because it is a subclass object stored as a superclass variable.




- with that said all other methods that are not overridden in the subclass cannot be accessed by a polymorphic variable

What methods does the object turd have available to it?




Class_A turd = new Class_B();




what is this know as?

- all the methods in class_A (except overridden methods)




- none of the methods in class_B (except over ridden methods)




- dynamic binding. refer to page 646-7

When performing a polymorphic declaration how do you satisfy the constructor of the of the subclass object?

- just as you would if it were a subclass type, nothing changes




- the only difference is in the methods that can be accessed




- refer to page 649

How to use the instanceOf operator




Consider Class_A is a class and turd is reference to that class




Consider Class_B is a different class and fart is a reference to that class

(turd instanceOf Class_A) will return true




(fart instanceOf Class_A) will return false




- simply a boolean operator to determine what reference variables belong to which reference types




(referenceVariable instanceOf ReferenceType/ClassName)

is the instanceOf operator conscious of inheritance/polymorphism?




Consider the same variable above but Class_B is a subclass of Class_A

(turd instanceOf Class_A) will return true




(fart instanceOf Class_A) will return as well




this is because Class_B is still a reference to class_A thru inheritance. The reverse is not true




(turd instanceOf Class_B) will return false

Abstract methods

a method in a superclass that has to be overriden in a subclass




public abstract variableType methodName(parameters);




- ends with a semicolon, used the keyword abstract




- must be overriden in a subclass or it will throw an error

rules of using abstract methods

- when using abstract methods, you cannot call an instance of the class, you must declare a subclass in the main methods.




- often used with abstract classes




- polymorphic definitions are the only way to use abstract methods

Abstract Classes

These classes are often used with abstract methods. They serve only as an inheritance classes for lower level operational subclasses.




How to declare




in the class header


public abstract class ClassName{




}

what is an interface

an interface is a way of creating a common vein of methods in a superclass and down thru its subclasses




- an interface is basically an abstract superclass above all others that only has abstract methods




- often used so that two objects of the same type can be compared

How is an interface defined?

Same way as a superclass except you use the keyword interface instead of class in the class declaration




public interface InterfaceName{

How is an interface implemented in a class?

you add the keyword "implement" and the InterfaceName to the class header. Same place you add extends for to extend a superclass into a subclass




SuperClass


public class ClassName implements InterfaceName{




SubClass


public class ClassName extends SuperClassName impliments InterfaceName{



how to use set up methods in an interface

They all end with semicolons because they are abstract methods



returnType methodName(variableType name);



How to use an interface in a class

- the return type, method name and variable type must all be exactly the same as the interface methods




public returnType methodName(variableType name){


code goes here


}

Do you have to use an interface in a subclass if a superclass uses one?

No. You only have to use the interface if you implement it in the class header

Can you declare a variable of an interface type that references an object of that interface?




in other words is this ok? provided that ObjectName impliments InterfaceName




InterfaceName vari = new ObjectName():

Yes but vari will only have access to the overwriten methods in the interface, not the object's methods

can you reference a SuperClass or Interface in the main method after a subclass that extends or impliments either?




Inter is an interface


Class_A is SuperClass




Class_B is a subclass with the method header




public class extends Class_A impliments Inter{

after a Class_B object is declared, you can reference the interface and the Superclass anywhere downstream because Class_B inherits both of them, therefore those objects exist in memeory and can be called