OO Design Flashcards

0
Q

What is constructor? What is a default constructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is the difference between a virtual method and a pure virtual or abstract method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is instantiation?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What the difference between a constructor and a destructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a subclass or a derived class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is encapsulation? Give an example.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is delegation in java?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between association, generalization aggregation, and composition?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a superclass or base class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between method overloading and method overriding?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will be the initial value of an object reference which is defined as an instance variable?

A

The object references are all initialized to null in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between a method and a constructor in java?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the principal concepts of OOP?

A

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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is inheritance?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is abstraction? How would you make a class abstract?

A

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 {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can java methods be abstract?

A

Yes. An abstract method consists of a method signature, but no method body.

Example:
public abstract double computePay();

Declaring a method as abstract has two results:

  • The class must also be declared abstract. If a class contains an abstract method, the class must be abstract as well.
  • Any child class must either override the abstract method or declare itself abstract.
16
Q

What is the difference between an interface and an abstract class?

A

Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods.

An interface is a collection of method declarations with no data and no bodies. That is, the methods of an interface are always empty; they are simply method signatures. Interfaces do not have constructors and they cannot be directly instantiated. When a class implements an interface it must provide the method definition for all the methods declared in the interface. The interface body can contain abstract methods, default methods, and static methods.

Java supports multiple inheritance of interfaces but not classes.

17
Q

What does the static keyword mean in java?

A

The static keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves. Static is used to store global information about a class.

18
Q

What is method overriding? Give an example.

A

Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.

19
Q

What is method overloading? Give an example.

A

Method Overloading means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.

20
Q

What is association?

A

Association is a relationship between two objects. In other words, 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.

21
Q

What is generalization?

A

Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.

22
Q

What is aggregation?

A

Aggregation is a special case of association. A directional association between objects. When an object ‘has-a’ another object, then you have got an aggregation between them. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.

23
Q

What is composition?

A

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, if the contained object cannot exist without the existence of container object, then it is called composition.