7. What are access modifiers?
Access modifiers specify the scope of accessibility of a data member, method, constructor or class. Depending on the language there can be various modifiers, but there are three main and shared betweem all of the OOP languages, that are fundamental.
Access Modifier | Definition |
---|---|
public | No restricted access, from same assembly (or package) or a referencing one |
private | Access limited to the containing type (class or struct) |
protected | Access limited to the containing class or derived classes |
8. Explain the concept of inheritance
Inheritance is one of the main features of object-oriented programming which allows classes to inherit properties and methods from other classes. This helps improve code reuse.
For example, we can have a base class called animal
, that may define the common properties shared by all animals. Then, we can define child classes such as: mammals
, reptiles
, etc. that will inherit the common attributes of all animals but will be able to define their own specific attributes and methods.

However, there are some limitations of the process of inheritance, for example, it requires more processing time for the programs as it has to navigate various classes during execution. Also, due to inheritance, the parent and child class are tightly coupled. When any changes are needed in the logic, it may require changes in both parent and child classes.
If the inheritance is not correctly implemented, it can lead to undesired results.
9. What are the various types of inheritance?
The various types of inheritance include:
- Single inheritance - A class inherits the properties of a single parent class.
1class A {
2 //parent class code
3}
4
5class B extends A {
6 //child class code
7}
- Multiple inheritances - An object or class can inherit features from more than one parent object or parent class. Not supported in all programming languages.
- Multi-level inheritance - One class has more than one parent class but at different levels of inheritance
1class A {
2 // parent class code
3}
4class B extends A {
5 // your code
6}
7class C extends B {
8 // your code
9}
- Hierarchical inheritance - One parent can have one or more child/sub/derived classes
1class A{
2 //parent class code
3}
4class B extends A {
5 //child class code
6}
7class C extends A {
8 //child class code
9}
- Hybrid inheritance - Combination of more than one type of inheritance in a single program
1 A
2 /
3 B C
4 /
5 D E
10. What is polymorphism?
Polymorphism is a significant feature of object-oriented programming that represents the ability for existence in multiple forms. A single interface
can be implemented in multiple ways by providing various definitions and implementations.
In Object-Oriented Programming languages, there are two types of polymorphism: 1. Static Binding (or Compile time) Polymorphism, e.g., Method Overloading
1function add(a, b){
2 return a+b;
3}
- Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding
xxxxxxxxxx
class Phone{
message(){
console.log("Message");
}
}
//Extending the Phone class
class iPhone extends Phone{
//Overriding message() of Phone class
sms(){
console.log("iPhone Message");
}
}
let mess = new iPhone();
mess.sms();