Copy of Inheritance_Polymorphism_review_NA

Copy of Inheritance_Polymorphism_review_NA - Joel Thomas...

Info icon This preview shows pages 1–3. Sign up to view the full content.

A B C D Joel Thomas Java Review - Inheritance and Polymorphism - Briefly A is a superclass of B and C a B is-a A, a C is-a A B is a superclass of D a D is-a B, a D is- a A B and C are subclasses of A D is a subclass of B Notes on Inheritance: 1. subclasses inherit all the methods and variables of the superclass 2. a subclass can have only one superclass 3. subclasses cannot directly access the private instance variables of its superclass - the superclass must have a public "getter" or "setter" method to access them indirectly. 4. subclasses can override a method in a superclass by writing a method in the subclass with the same signature (return type, name, and parameter types) as the method in the superclass and then a. write code for the method in the subclass b. you can access the code in the superclass by using super.method1(args); and then add additional code for the subclass. Subclasses never inherit the constructors from the superclass 5. if the subclass has no constructors, the no-argument constructor from the superclass is used - if the superclass does not have a no-argument constructor, a compile error will occur 6. if the superclass has a no constructors, the no-argument constructor is used and the inherited data members will be initialized per the no-argument constructor and the instance variables of the subclass will be initialized to zero for primitives and null for reference types. 7. a constructor in a subclass can call a constructor from its superclass by: super.constructor(args); which must be the first line of code in the constructor and followed by any other initializations needed 8. subclasses cannot override static methods of a superclass Notes on Polymorphism: 1. A method that has been overridden in at least one subclass is said to be polymorphic. 2. instanceof is used to determine what type of object an object really is: if(x instance of A) ... 3. correct casting: int x = ((B)b). method1(); . has higher precedence than a cast so cast the object first. Notes on Abstract Classes: 1. A superclass with at least one abstract method (no code for the method) is an abstract class 2. Code for the abstract method must be provided in any subclass of the abstract class 3. You cannot create an instance of an abstract class. 4. An abstract class can have private instance variables, constructors, and concrete methods as well as abstract methods. 5. Use an abstract class with an abstract method when each subclass would have different code for the method. This forces each subclass to use the same signature so the code is less confusing. Notes on Interfaces: 1. An interface is a group of method headers with no implementation code a. contains no implementation code b. contains no constructors c. contains no instance variables d. can contain constant declarations e. all methods in an interface are public 2. public interface X {list of methods } 3. public class A implements X { } Java Review - Inheritance and Polymorphism - Page
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Joel Thomas 4. class A must implement every method in interface X.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern