Creado por Nik Nguyen
hace más de 8 años
|
||
Pregunta | Respuesta |
What is the purpose of inheritance? | The purpose of inheritance is to define a very general class and then later define more specialized classes that add some new details to the existing general class definition. This saves work, as more specialized class INHERITS all the properties of the general class and you, the programmer, need only program the new features. |
Define a class hierarchy. | The purpose of inheritance is to define a very general class and then later define more specialized classes that add some new details to the existing general class definition. This saves work, as more specialized class INHERITS all the properties of the general class and you, the programmer, need only program the new features. |
How do you use a derived class? | A derived class is a class defined by adding instance variables and methods to an existing class. The derived class EXTENDS THE EXISTING CLASS, so we say that the existing class that the derived class is built upon is called the BASE CLASS, OR SUPERCLASS. |
Define the rules of inheritance for subclasses. | * A subclass can add new private instance variables. * A subclass can add new public, private, or static methods. * A subclass can override inherited methods. * A subclass may not redefine a public method as private. * A subclass may not override a static methods of the superclass. * A subclass should define its own constructors. * A subclass cannot direct access the private members of its superclass. It must use accessor or mutator methods. |
Compare and contrast overriding and overloading. | Method overriding is where a new method definition given in the derived class has the same name, return type, and number of and types of parameters. But if the number of parameters are different, then the method is overloaded. Remember that overloading means to create an additional ‘ load’ on a method name by using it for another method, whereas overriding replaces a methods definition. |
How is super used in programming? | super is a keyword that is used to CALL THE CONSTRUCTORS OF A BASE CLASS. By using super(parameter), you are able to make a call to that specific constructor with that specific number of parameters. IT MUST AWAYS BE THE FIRST ACTION TAKEN IN A CONSTRUCTOR DEFINITION. If a specific call to the base-class constructor is not included, then the use of super() will call the constructor which does not have parameters (I.E. the default constructor). However, this is not usually a favorable choice. |
How do you call an overridden method? | Using super, a method that has been overridden can be invoking the method name in the base class as it is defined. This is written by using super.methodName(). This will call the overridden method of the base class, so if you are calling this in methodName(), then the methodName of the base class is called—not the current class. In this case, using super.writeOutput() will call writeOutput of the base class—not of the derived class. |
Discuss how the derived class of a derived class inherit specific characteristics. | Derived-derived classes continue to share the same public members of the ancestor class. However, it cannot modify and change the private instance variables of the base or the ancestor class. |
Why does type compatibility matter? | Various things to keep in mind for type compatibility: UnderGraduate is still a Student. If you were to call a class with parameters (Student s1, Student s2), with a Student studentobjectz and Undergraduate underGradOBject, both will still be considered students and treated the same way. And because it is a type person, anything that works under objects of type Person will work for underGraduate as well. Due ot this, you can assign an object of a class to be a variable of any ancestor type—BUT NOT THE OTHER WAY AROUND. The following is ok: Student s = new Student(); Undergraduate ug = new Undergraduate(); Person p1 = s; Person p2 = ug; Even this is ok: Person p1 = new Student(); Person p2 = new Undergraduate(); But this is illegal: Student s = new Person(); Undergraduate ug = new Person(); …etc. Students are persons, but are persons necessarily students? |
Define the purpose of polymorphism. | Polymorphism is meant to make changes in the method definition of the derived classes, and have those changes apply to methods written in the use class. This happen automatically in Java! |
Define dynamic binding. | Where a bunch of objects that share the same type, but not the same reference, automatically use the method that is within its reference and not the type that necessarily created it. The focus is more on the object referred to. |
¿Quieres crear tus propias Fichas gratiscon GoConqr? Más información.