INTRODUCTION II Flashcards
Define programming
Programming is the process of creating a set of instructions that tell a computer how to perform a task
What are the advantages of inheritance
Promotes reusability
Code is reliable since it has already been tested and debugged
Effort and time is saved
What are the advantages of encapsulation
- Encapsulation protects an object from unwanted access by clients.
- Encapsulation allows access to a level without revealing the complex details below that level.
- It reduces human errors.
- Simplifies the maintenance of the application
- Makes the application easier to understand.
What are the advantages of polymorphism
It helps the programmer to reuse the codes, i.e., classes once written, tested and implemented can be reused as required. Saves a lot of time.
Single variable can be used to store multiple data types.
Easy to debug the codes.
What are the advantages of abstraction
Encapsulation protects an object from unwanted access by clients.
Encapsulation allows access to a level without revealing the complex details below that level.
It reduces human errors.
Simplifies the maintenance of the application
Makes the application easier to understand.
Explain the difference between encapsulation and data abstraction as used in object oriented programming
Abstraction- Problems are solved at the design level
Encapsulation- Problems are solved at the implementation level
In abstraction, implementation complexities are hidden using abstract classes and interfaces. While in encapsulation, the data is hidden using methods of getters and setters.
Explain the difference between polymorphism and inheritance as used in object-oriented programming
Inheritance is basically applied to classes. Whereas polymorphism is basically applied to functions or methods.
Briefly explain any two different types of inheritance supported in OOP
- Single-When a class extends another one class only then we call it a single inheritance.
- Multiple Inheritance- one class extending (Or inherits) more than one base class.
- Multilevel inheritance -one can inherit from a derived class, thereby making this derived class the base class for the new class.
- Heirachical -one class is inherited by many sub classes
- Hybrid inheritance is a combination of Single and Multiple inheritance.
Briefly explain the different programming paradigms
Imperative: Programming with an explicit sequence of commands that update state.
Declarative: Programming by specifying the result you want, not how to get it.
Structured: Programming with clean, goto-free, nested control structures.
Procedural: Imperative programming with procedure calls.
what are constructors in java?
A Java constructor is special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initializes the newly created object before it is used
Code excerpt of polymorphism
public class Animal{ ... public void sound(){ System.out.println("Animal is making a sound"); } }
public class Horse extends Animal{ ... @Override public void sound(){ System.out.println("Neigh"); } }
Code excerpt of abstraction
abstract class Shape{ // code }
public class Sports implements Shape
[
]
Code excerpt of encapsulation
class Account { private int account_number; private int account_balance;
public void show Data() { //code to show data }
public void deposit(int a) { if (a < 0) { //show error } else account_balance = account_balance + a; } }
Using a code segment (s), explain the usage of keyword super in Java programming.
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.
class Vehicle { int maxSpeed = 120; }
/* sub class Car extending vehicle */ class Car extends Vehicle { int maxSpeed = 180;
void display() { /* print maxSpeed of base class (vehicle) */ System.out.println("Maximum Speed: " + super.maxSpeed); } }
/* Driver program to test */ class Test { public static void main(String[] args) { Car small = new Car(); small.display(); } }
Explain the difference between throw and throws as used in OOP
- Point of Usage
The throw keyword is used inside a function. It is used when it is required to throw an Exception logically. The throws keyword is used in the function signature. It is used when the function has some statements that can lead to exceptions. - Exceptions Thrown
The throw keyword is used to throw an exception explicitly. It can throw only one exception at a time. The throws keyword can be used to declare multiple exceptions, separated by a comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then. - Syntax
Syntax of throw keyword includes the instance of the Exception to be thrown. Syntax wise throw keyword is followed by the instance variable. throw new ArithmeticException(“/ by zero”); Syntax of throws keyword includes the class names of the Exceptions to be thrown. Syntax wise throws keyword is followed by exception class names. public static void main(String[] args)throws InterruptedException