Polymorphism and Inheritance

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What exactly is inheritance?

Inheritance is essentially a relationship between objects which share common characteristics. EX: An animal, wolf and zebra object would share common characteristics like the ability to taste or make a noise.

Can a class extend several classes at once?

NO! You cannot extend to multiple classes. Using interfaces is a possible solution as one can use multiple interfaces.

What is overriding?

Overriding is whenever a subclass edits or alters methods from a superclass. The subclass will define the method from the superclass with the same return type, name and parameters of the method.

What is partial overriding?

Partial overriding is whenever a subclass overrides the superclass method by using a call to the superclass method or using "super();" A subclass will do this when it wants to do what the superclass does and extra tasks.

How are subclass objects legally declared?

Suppose a UnderGrad, Employee, Student and GradStudent class exist and Student is the superclass with the others as subclasses. Ex: Student perry = new Student(); Student adam = new GradStudent(); Student elizabeth = new UnderGrad(); These are all legal because a GradStudent "is-a" student, a student "is-a" student, and a UnderGrad "is-a" student. Use the "is-a" method going from right to left (<--------) of the statement.

How does a subclass inherit constructors from a superclass?

The statement "super();" will allow a subclass to inherit a constructor from a superclass. "Super();" must always be used in the first line of the sub constructor body. EX: public SubClass(int x) { super(); this.count = x; }

What is polymorphism and why is it so important?

UnderGrad, Employee, Student and GradStudent class exist and Student is the superclass with the others as subclasses. Ex: Student perry = new Student(); Student adam = new GradStudent(); Student elizabeth = new UnderGrad(); Let's suppose computeGrade is a method which is both redefined in UnderGrad and GradStudent from Student. When the program runs "elizabeth.computeGrade" , which appropriate method will it run? Hence, Polymorphism is given birth. The correct method is chosen by the type of the actual object, in this case computeGrade will run from the UnderGrad class. (The selection of the correct method occurs during the run of the program)

What is a subclass?

A subclass is basically the class which "inherits" or shares the code from the superclass. (Subclass is also called a derived class)

What limits are placed with overriding methods?

A subclass may not override static methods of the superclass and it cannot redefine a public method of the superclass as private.

What code does a subclass inherit or extend from a superclass?

A subclass will always inherit methods and instance variables. However, if the superclass contains private instance variables, the coder will have to use accessors (get methods) or mutators (set methods). IMPORTANT- the subclass will never inherit the constructors unless specified (using the "super" term).

What is a superclass?

A superclass is the class which "extends" or shares its characteristics (code) to subclasses. (Superclass is also called a base class)

How does one distinguish between a superclass or a subclass?

By using the "is-a" relationship, a coder can understand which classes are subclasses or superclasses. EX: A Person, Employee, Student and GradStudent class exist. By using the "is-a" method, we will know that a Employee "is-a" Person, a Student "is-a" Person and a GradStudent "is-a" a Student (Multi-level Inheritance as GradStudent is also a Person). BUT, a Person is not a Employee and neither is a Person a Student. As a result, Person would be the superclass and the Employee, GradStudent and Student classes are subclasses

What is downcasting?

EX: Fish s = new Carp(); Carp g = new Carp(); int x = s.getMass(); //compile-time error int y = g.getMass(); // correct Although s and g are Carp objects, s.getMass will not work because s is type Fish and the Fish class does not have the getMass method. To solve this, cast s: int x = ((Carp) s).getMass(); The outer parentheses are IMPORTANT!

What is the purpose of inheritance?

Inheritance allows an efficient way for the programmer to reuse code. Since, a subclass shares features of a superclass, new code is only needed in the subclass.

What the heck is the "instanceof" keyword?

The code "instanceof" will determine if a certain subclass is actually type of the superclass. EX: public class Animal{} public class Fish extends Animal{} public class Tuna extends Fish { public static void main(String args[]){ Fish m = new Fish (); Tuna d = new Tuna (); System.out.println(m instanceof Animal); //true System.out.println(d instanceof Fish); //true System.out.println(d instanceof Animal); //true } }


Kaugnay na mga set ng pag-aaral

Final Exam Review for Chapters 1-5

View Set

NUR 410 PEDS PREPU, interv week 6 peds, PN108 PrepU Chapter 39, Ricci Chapter 43: Nursing Care of the Child With an Alteration in Urinary Elimination/Genitourinary Disorder

View Set

Nursing 112 (Pharmacology) test 1

View Set

Edmentum Mastery Test: Cross Sections of Three-Dimensional Objects (100%)

View Set

Anatomy and Physiology Module 7 - Respiratory System

View Set

Principles of Risk Management and Insurance Chapter 23, RMI exam 3

View Set