Java Final - Inheritance

Ace your homework & exams now with Quizwiz!

What is the keyword must be used to inherit a class?

"extends"

What is the keyword used to refer to member of base class from a sub class?

"super" (used to refer to its immediate superclass)

What is the relationship between objects and abstract classes?

-You cannot construct an object of an abstract class -BUT you can still have an object reference whose type is an abstract class (must be an inherited concrete subclass)

when would you consider using interfaces over abstract methods?

-You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. -You want to specify the behavior of a particular data type, but not concerned about who implements its behavior. -You want to take advantage of multiple inheritance of type.

when would you consider using abstract methods over interfaces?

-You want to share code among several closely related classes. -You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private). -You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

toString

-method that returns a string representation for each object, used for debugging example: Rectangle box = new Rectangle(5, 10, 20, 30); String s = box.toString(); // Sets s to "java.awt.Rectangle[x=5,y=10,width=20,height=30]"

Steps to developing an Inheritance Hierarchy:

1) List the classes that are part of the hierarchy 2) Organize classes into an inheritance hierarchy 3) Determine common responsibilities 4) Decide which methods are overridden in the subclass 5) Declare the public interface of each subclass 6) Identify instance variables 7) Implement constructors and methods 8) Construct objects of different subclasses and process them

The bad way and the better way to write toString methods:

BAD WAY: public class BankAccount { ... public String toString() { return "BankAccount[balance=" + balance + "]"; } } public String toString() } . { } return getClass().getName() + "[balance=" + balance + "]"; The

If your subclass tries to call an instance variable or method in the superclass that is private, what happens?

Compilation Error - cannot be inherited by the subclass and does not have access to it

In order to concatenate an object and a string, without the result being messed up, what do you have to do?

Override the method and supply your own version -yields a string that describes the object's state

hashCode

Uniquely identify each commit.

How do you access the initialized variables of the superclass in the subclass?

Using the superclass constructor, which can be called through the subclass's constructor AS LONG AS it's the FIRST STATEMENT in the subclass constructor

Example of Polymorphism

When a superclass reference is expected (the method is presentQuestion(Question q) and a subclass reference is passed in -- presentQuestion(ChoiceQuestion cq)

When do you use "super"

When you're writing a method in the subcass that needs to directly call the method inside the superclass (ex. if you're calling method display, inside display in the subclass, you need to reference the superclass so that it's not calling itself over and over." Super is NOT an object refernece

What are one of the limitations of using abstract methods?

You cannot construct objects of classes with abstract methods. For example, once the Account class has an abstract method, the compiler will flag an attempt to create a new Account() as an error.

BankAccount acct = new Checking(); How would you turn acct into a Checking object?

You would have to cast acct (which is a BankAccount reference) into an actual Checking object to use Checking specific methods Checking chAcct = (Checking) acct;

concrete class

a class for which you can create an object (basically anything that isn't abstract)

abstract class

a class for which you cannot create an object

abstract methods

a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

What is a drawback of protected variables?

accessible by subclasses AND by other classes in the same package (i.e. not that protected)

BankAccount acct = new Checking(); what is the object reference what is the object what methods can you call on these?

acct is a BankAccount reference to a (new) Checking object -can only call BankAccount methods on "acct"

instanceOf will return true if:

an object can be cast to the new Type

instanceOf is a what...

an operator (not a method) **DOES NOT OPERATE ON NUMBERS

When overriding a method, you are not allowed to

change the type of parameter variable

equals Method

checks whether two objects have the same CONTENTS differs from == operators, which only checks if the two references are referring to the asme object

if an object is null, instanceOf will return

false!

How to successfully cast obj to be of type Question, using instanceOf:

if (obj instanceof Question) { Question q = (Question) obj; }

polymorphism

is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

What happens if you try to extend more than one class to a subclass?

it won't work and you'll get a compile time error

polymorphism allows us to:

manipulate objects that share a set of tasks even though the tasks are executed in different ways

protected access

object that can be accessed by the methods of the object's class and all its subclasses Access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class

How do you cast an object?

public boolean equals(Object otherObject) { Stamp other = (Stamp) otherObject; return color.equals(other.color) && value == other.value; }

instanceOf

tests whether an object belongs to a particular type

What happens if a subclass extends an abstract superclass?

the subclass is not itself abstract and can implement the abstract method -overrides the abstract methods

What happens if you omit the superclass constructor call?

the superclass constructor with no arguments is called

What happens when you concatenate a string with an object?

the toString method is called. Example "box=" + box; becomes "box=java.awt.Rectangle[x=5,y=10,width=20,height=30]" **DOES NOT WORK W NUMBERS bc they're not objects

method call are always determined by ...

the type of the actual object

dynamic method lookup

the virtual machine calls an instance method, and it located the method of the implicit parameter's class

What are the general methods defined in the (automatically extended) class Object?

toString equals hashCode

Is it legal to store a subclass reference in a superclass variable? like such: Question q = cq;

yes

why would someone use a "final" method / class?

you can prevent other programmers from creating subclasses or from overriding certain methods -- no one can extend 'data type'. When you have a reference of type String, it must contain a string object and never an object of a subclass

What do you do when you need to convert a superclass reference to a subclass reference?

you cast it, but in order to protect against bad casts, you need to use "instanceOf"

How do you get around a method parameter (default: object) not knowing anything about the actual object it's being compared to? (for the equals method)

you need to cast it to make it read the right parameter variables

If you're trying to use the "equals" method, and comparing two objects, you will get an error if you enter one object type and another entry that is not the object type. How do you prevent this?

you test whether the two objects belong to the same class use if statement if (getClass() != otherObject.getClass()) { return false; }


Related study sets

Lippincott: Nursing Care During Pregnancy

View Set

Community Mental Health Nursing Questions

View Set

Assignment #1: Rhetorical Terms Study: AP English 11 Words

View Set

BIO 112 Module 4 Animal Evolution Ch. 27

View Set

Professional Roles and Leadership

View Set