OO Design Flashcards
What is constructor? What is a default constructor?
A constructor is a method used to initialize the state of an object. It is invoked at the time of object creation.
A constructor with no parameters is known as a default constructor. The purpose of the default constructor is to provide default values (null, 0, etc…) depending on the type of the object.
What is the difference between a virtual method and a pure virtual or abstract method?
A virtual method is a method whose behavior can be overridden within an inheriting class by a method with the same signature. In java all methods are virtual by default.
An abstract method is a method who’s implementation is required by a derived class that is not abstract. In java pure virtual or abstract methods are declared with the keyword abstract. However if the method is abstract, the class must be abstract.
What is instantiation?
Instantiation is the creation of an instance of a class. The result is an object.
In java, you instantiate a class to create a specific class file that is also an executable file you can run in a computer.
What the difference between a constructor and a destructor?
The constructor initializes the state of the object, which usually involves calling the constructors of its parent classes so that they can initialize their part of the object’s state.
Destructors are invoked automatically when an object goes out of scope or when the delete operation is used to destroy a dynamically created object. Java has a garbage collector responsible for finding and destroying unused objects.
What is a subclass or a derived class?
A sub-class is a class which inherits from another class called superclass or the parent class. Sub-class can access all public and protected methods and fields of its super class.
What is encapsulation? Give an example.
Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object. Encapsulation deals largely with information hiding.
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. Private fields cannot be accessed by anyone outside the class.
What is delegation in java?
Is the act of forwarding the method call to another instance. Class A delegates functionality to Class B. In this case, class A contains reference (object) of class B and calls methods of class B (delegation). E.g. Manager delegating some work to Officer.
What is the difference between association, generalization aggregation, and composition?
Association is a relationship between two objects. Association defines the multiplicity between objects. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.
Generalization uses a “is-a” relationship from a specialization to the generalization class. At a broader level you can understand this as inheritance.
Aggregation is a special case of association. When an object ‘has-a’ another object, then you have got an aggregation between them.
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object. The contained object cannot exist without the existence of container object.
What is a superclass or base class?
A base class is a class, in an object-oriented programming language, from which other classes are derived. It facilitates the creation of other classes that can reuse the code implicitly inherited from the base class (except constructors and destructors).
A base class may also be called parent class or superclass.
What is the difference between method overloading and method overriding?
When overloading, one must change either the type or the number of parameters for a method that belongs to the same class.
When overriding a method the method name, the number and types of parameters, and the return type will all remain the same. However the method definition is changed slightly to fit the needs of the child class.
Method overriding happens at run-time and is the driving force behind polymorphism. However, method overloading happens at compile-time.
What will be the initial value of an object reference which is defined as an instance variable?
The object references are all initialized to null in Java.
What is the difference between a method and a constructor in java?
Constructor
- used to initialize the state of an object
- must not have return type
- invoked implicitly
- java compiler provides a default constructor if one is not provided
- constructor name must be the same as class name
Method
- used to expose behavior of an object
- invoked explicitly
- must have a return type unless void
- not provided by the compiler
- name can be same as the class name
What are the principal concepts of OOP?
There are four principle concepts upon which object oriented design and programming rest. They are:
- Abstraction
- Polymorphism
- Inheritance
- Encapsulation
(i. e. easily remembered as A-PIE).
What is inheritance?
Inheritance is the process by which objects of one class acquire the properties of objects of another class. A class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Inheritance is done by using the keyword extends.
Example: public class Ellipse extends Shape {}
The two most common reasons to use inheritance are to promote code reuse and to use polymorphism.
What is abstraction? How would you make a class abstract?
Abstraction refers to the ability to make a class abstract. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
To make a class abstract use the abstract keyword.
Example: public abstract class Employee {}