Shared Flashcard Set

Details

Java Cert - Chapter - 5
Objects Orientation , Overloading, Overriding , Constructors, and Return Types
39
Computer Science
Undergraduate 4
02/19/2006

Additional Computer Science Flashcards

 


 

Cards

Term
What is encapsulation?
Definition
Hiding implementation details from users of software to maintain flexibility of possible change in future implementations of software.
Term
How do you implement encapsulation?
Definition
Keep instance variables private, make public accessor & mutator methods,
Term
What is a IS-A relationship?
Definition
it is based on inheritance. eg - horse is type of animal.
Term
What is Has-A relationship?
Definition
A HAS-A B if class A has a reference to an instance fo class B.
Term
How is a HAS-A shown in UML ?
Definition
As a dotted line from the method that calls the method of another class of the same signature. It feels to outsides like the class which has-a has the functionality eventhough it doesnt.
Term
Can constructors be overridden?
Definition
No constructors can only be over loaded
Term
When can you not override a method?
Definition
if it is constructor or a final method?
Term
what must a subclass of abstract class do in order to be concrete?
Definition
the subclass must override all abstract methods.
Term
what happens if a subclass is stored in reference of super class and an overridden method is called on the reference?
Definition
the subclass method gets invoked.
Term
why cant overriding method have more restrictive modifier than a superclass modifier?
Definition
If the subclass has more restrictive modifier then it wont be callable from the super class reference which is not allowed.
Term
what will happen if an overriden method is given a more restrictive modifier than superclass method?
Definition
Compile Error!!
Term
what are the rules for overriding a method?
Definition
access modifier : cant be more restrictive.
return type: must be same
argument list: must be exactly the same
Term
What are the rules for throwing exceptions?
Definition
Overriding method cant throw broader exception though it may throw a narrower exception.
Term
Can you override a private method?
Definition
No if a method is not visible it cant be overriden
Term
How do you invoke a super class version of a method?
Definition
super.method();
Term
what are the requirements of an overloaded method?
Definition
Only that it change its return type.
Term
How does a compiler determine what type of methods can be called on an object.
Definition
by looking at the reference type. Only when methods are overridden that compiler will call the method of sub type
Term
can access type of overloaded method change?
Definition
yes it can . be careful if method's acess type is changed it becomes overloaded but not overridden.
Term
When is a class given a default constuctor?
Definition
when there is no constructor written by the programmer.
Term
will a class have a no arg constructor if there is already a constructor which takes arguments?
Definition
no no no . You take on constructore responsibility by defining a constructor.
Term
can constructor be private?
Definition
yes it can. it only means it cant be instantiated from the outside.
Term
are constructor names case sensitive?
Definition
yes constructor name must match exactly the class name
Term
can constructors have return types?
Definition
NO NO NO. this is a good thing.
Term
can you have a method with the same name as constructor
Definition
legal but stupid. If you see a return type then it is a method not a constructor. it will compile.
Term
is there any restriction on this() or super calls in a constructor?
Definition
YES they bust be the first line of code.
Term
what does the constructor insert as the first line of the constructor?
Definition
if there is no this() it inserts a call to super(). so Object.super() will always be invoked.
Term
can yo u call an instance method in a constructor?
Definition
yes but not before super() has run.
Term
can static variables be acessed before a super() call?
Definition
yes. static can but the other one cant
Term
can an abstract class have constructors?
Definition
Yes an abstract class must have constructors and they must get called before the concrete class.
Term
can an interface have a constructor?
Definition
NO - they are not part of an objects inheritance tree.
Term
can a constructor be invoked from within a instance method.
Definition
no it cant be invoked on this.
Term
what is the access modifier of the default constructor?
Definition
it is same at the access modifier of the class.
Term
what happens if the super class doesnt have a no arg constructor?
Definition
in that case you must put in a call to super(args).
Term
what does a call to this() mean?
Definition
it means we are calling a constructor of this class.
Term
if you have a call to this() then does the super() method get called.
Definition
you are only delaying the inevitable sooner or later the super() will get called
Term
can the return type of the overridden method be generalised?
Definition
no it must match the return type of super class method exactly.
Term
can you return null for a method that returns an object?
Definition
Yes you can no matter what type of object you can return null
Term
are there any implicit conversion rules for return types?
Definition
anythign that can be implicitly be converted can be returned. eg. char instread of int
Term
can you return an array where an object is expected.
Definition
yes an array is also an object so new int[] counts.
Supporting users have an ad free experience!