OOP

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is a copy constructor?

A copy constructor creates objects by copying variables from another object of the same class. The main aim of a copy constructor is to create a new object from an existing one.

What are 'access specifiers'?

Access specifiers or access modifiers are keywords that determine the accessibility of methods, classes, etc in OOPs. These access specifiers allow the implementation of encapsulation. The most common access specifiers are public, private and protected. However, there are a few more which are specific to the programming languages.

What is an abstract class?

An abstract class is a class that consists of abstract methods. These methods are basically declared but not defined. If these methods are to be used in some subclass, they need to be exclusively defined in the subclass.

What is an exception?

An exception is a kind of notification that interrupts the normal execution of a program. Exceptions provide a pattern to the error and transfer the error to the exception handler to resolve it. The state of the program is saved as soon as an exception is raised.

Operator Overloading

An operator or method overloading refers to a polymorphic characteristic of same symbol or operator having different meanings (forms) depending on the context. For example, the plus symbol (+) is used for mathematical addition as well as String concatenation. In either case, only context (i.e. argument types) determines the interpretation of the symbol.

Differentiate between a class and a method.

Class: A class is basically a template that binds the code and data together into a single unit. Classes consist of methods, variables, etc Method: Callable set of instructions also called a procedure or function that are to be performed on the given data

What is the difference between a class and a structure?

Class: User-defined blueprint from which objects are created. Structure: A structure is basically a user-defined collection of variables which are of different data types.

Java characteristics that exhibit polymorphism

Coercion Operator Overloading Polymorphic Parameters

What is data abstraction?

Data abstraction is a very important feature of OOPs that allows displaying only the important information and hiding the implementation details. For example, while riding a bike, you know that if you raise the accelerator, the speed will increase, but you don't know how it actually happens. This is data abstraction as the implementation details are hidden from the rider.

Java - Dynamic Polymorphism

Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime, that's why it is called runtime polymorphism. Method Overriding is one of the ways to achieve Dynamic Polymorphism. In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

What is encapsulation?

Encapsulation refers to binding the data and the code that works on that together in a single unit. For example, a class. Encapsulation also allows data-hiding as the data specified in one class is hidden from other classes.

What is the difference between an error and an exception?

Error Errors are problems that should not be encountered by applications Exception Conditions that an application might try to catch

What is Garbage Collection (GC)?

GC is an implementation of automatic memory management. The Garbage collector frees up space occupied by objects that are no longer in existence.

Java - create objects using deserializaiton

In Java Serialization is used to convert the current state of an object into a byte stream. deserialization is the exact opposite as we recreate the object using the byte stream. For the process of serialization, we need to implement Serializable interface. Exception Handling is to be done to create objects using this method.

Compile time polymorphism

In Java, compile time polymorphism refers to a process in which a call to an overloaded method is resolved at compile time rather than at run time. Method overloading is an example of compile time polymorphism. Method Overloading is a feature that allows a class to have two or more methods having the same name but the arguments passed to the methods are different. Unlike method overriding, arguments can differ in: Number of parameters passed to a method Datatype of parameters Sequence of datatypes when passed to a method.

Java - Interface

Interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Along with abstraction, interface also helps to achieve multiple inheritance in Java.Note: You can achieve 100% abstraction using interfaces.

Can you create an instance of an abstract class?

No. Instances of an abstract class cannot be created because it does not have a complete implementation. However, instances of subclass inheriting the abstract class can be created.

difference between OOP and SOP

OOP - objects rather than just functions and procedures OOP - bottom-up approach OOP - Provides data hiding OOP - Can solve problems of any complexity OOP - can be reused by reducing redundancy

What is dynamic polymorphism?

Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism which is resolved during runtime. An example of runtime polymorphism is method overriding.

What are the different types of inheritance?

Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid inheritance

Types of constructors

Types of constructors differ from language to language. However, all the possible constructors are: Default constructor Parameterized constructor Copy constructor Static constructor Private constructor

What are the limitations of OOPs?

Usually not suitable for small problems Requires intensive testing Takes more time to solve the problem Requires proper planning The programmer should think of solving a problem in terms of objects

What are virtual functions?

Virtual functions are functions that are present in the parent class and are overridden by the subclass. These functions are used to achieve runtime polymorphism.

Java - Create Objects using newInstance( ) method of Constructor class

We saw the newInstance method of class Class which we used to create an object. Similarly, the class constructor also consists of a newInstance( ) method which can be used to create objects. Other can default constructors with the help of this method we can also call parameterized constructors.

Java - Hierarchical Inheritance

When a class has more than one child classes (sub classes) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.

Java - Multilevel Inheritance

When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.

What is a class?

a prototype that consists of objects in different states and with different behaviors; has number of methods that are common the objects present within that class

Java - Can achieve abstraction in two ways

abstract class interface

What is the difference between public, private and protected access modifiers?

accessibility from own class: public, private, protected Accessibility from derived class: public, protected Accessibility form world: public

Differentiate between data abstraction and encapsulation.

data abstraction: Solves the problem at the design level Allows showing important aspects while hiding implementation details encapsulation: Solves the problem at the implementation level Binds code and data together into a single unit and hides it from the world

What is an object?

real-world entity which is the basic unit of OOPs; different objects have different states or attributes, and behaviors

java - blocks

static block: A static block in Java is the block which is executed only once at the time of class loading. A static block is also known as a static initialization block. A class can have more than one static blocks. instance block: An instance block in Java is the block which is executed whenever an object is created. A static block is also known as instance initialization block. An instance block is executed in the order they are written after the constructor makes the call to super.

Java - two classes categories

Built-in Classes User-Defined/ Custom Classes

What is a subclass?

A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a derived of Vehicle class.

What is a constructor?

A constructor is a special type of method that has the same name as the class and is used to initialize objects of that class.

Java - Static Polymorphism

A polymorphism that is resolved during compile time is known as static polymorphism. Method overloading is an example of compile time polymorphism.

How to achieve data abstraction?

Data abstraction can be achieved through: Abstract class Abstract method

Java - can achieve encapsulation by

Declaring the variables of a class as private. Providing public setter and getter methods to modify and view the variables values.

What are the main features of OOPs?

Inheritance, Encapsulation, Polymorphism, Data Abstraction

Differentiate between overloading and overriding.

Overloading: Two or more methods having the same name but different parameters or signature. Resolved during compile-time Overriding: Child class redefining methods present in the base class with the same parameters/signature Resolved during runtime

Can you call the base class method without creating an instance?

Yes, you can call the base class without instantiating it if: * It is a static method * The base class is inherited by some other subclass

Why use OOPs?

* allows clarity in programming allowing simplicity in solving complex problems. * code can be reused through inheritance thereby reducing redundancy * data and code bound together by encapsulation * allows data hiding, therefore, private data is kept confidential * problems can be divided into different parts making it simple to solve * polymorphism gives flexibility to the program by allowing the entities to have multiple forms

Java - rules to create a class

A Java class must have the class keyword followed by the class name, and class must be followed by a legal identifier. The class name must start with a capital letter and if you are using more than one word to define a class name, every first letter of the latter words should be made capital. There should not be any spaces or special characters used in a class name except the dollar symbol($) and underscore(_). A Java class can only have public or default access specifier. It must have the class keyword, and class must be followed by a legal identifier. It can extend only one parent class. By default, all the classes extend java.lang.Object directly or indirectly. A class may optionally implement any number of interfaces separated by commas. The class's members must be always declared within a set of curly braces {}. Each .java source file can contain any number of default classes but can only have one public class. Class containing the main() method is known as the Main class as it will act as the entry point to your program.

What is a destructor?

A destructor is a method that is automatically invoked when an object is destroyed. The destructor also recovers the heap space that was allocated to the destroyed object, closes the files and database connections of the object, etc.

What is a finally block?

A finally block consists of code that is used to execute important code such as closing a connection, etc. This block executes when the try block exits. It also makes sure that finally block executes even in case some unexpected exception is encountered.

What is a superclass?

A superclass or base class is a class that acts as a parent to some other class or classes. For example, the Vehicle class is a superclass of class Car.

What is a try/catch block?

A try/ catch block is used to handle exceptions. The try block defines a set of statements that may lead to an error. The catch block basically catches the exception.

What is a final variable?

A variable whose value does not change. It always refers to the same object by the property of non-transversity.

Java - Abstract class

Abstract class in Java contains the 'abstract' keyword. Now what does the abstract keyword mean? If a class is declared abstract, it cannot be instantiated, which means you cannot create an object of an abstract class. Also, an abstract class can contain abstract as well as concrete methods.Note: You can achieve 0-100% abstraction using abstract class.

Java - Advantages of Dynamic Polymorphism

Dynamic Polymorphism allows Java to support overriding of methods which is central for run-time polymorphism. It allows a class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods. It also allows subclasses to add its specific methods subclasses to define the specific implementation of same.

What is exception handling?

Exception handling in Object-Oriented Programming is a very important concept that is used to manage errors. An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve them.

What is the use of 'finalize'?

Finalize as an object method used to free up unmanaged resources and cleanup before Garbage Collection(GC). It performs memory management tasks.

What is hierarchical inheritance?

Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example, the vehicle class can have 'car', 'bike', etc as its subclasses.

What is hybrid inheritance?

Hybrid inheritance is a combination of multiple and multi-level inheritance.

Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. Since multiple inheritance is not supported in Java as it leads to ambiguity, so this type of inheritance can only be achieved through the use of the interfaces.

Java - Run time polymorphism

In Java, runtime polymorphism refers to a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this, a reference variable is used to call an overridden method of a superclass at run time. Method overriding is an example of run time polymorphism.

Java - Single Inheritance

In single inheritance, one class inherits the properties of another. It enables a derived class to inherit the properties and behavior from a single parent class. This will in turn enable code reusability as well as add new features to the existing code.

What are the limitations of inheritance?

Increases the time and effort required to execute a program as it requires jumping back and forth between different classes The parent class and the child class get tightly coupled Any modifications to the program would require changes both in the parent as well as the child class Needs careful implementation else would lead to incorrect results

What is inheritance

Inheritance is a feature of OOPs which allows classes inherit common properties from other classes. For example, if there is a class such as 'vehicle', other classes like 'car', 'bike', etc can inherit common properties from the vehicle class. This property helps you get rid of redundant code thereby reducing the overall size of the code.

What is an interface?

It is a concept of OOPs that allows you to declare methods without defining them. Interfaces, unlike classes, are not blueprints because they do not contain detailed instructions or actions to be performed. Any class that implements an interface defines the methods of the interface.

What is method overloading?

Method overloading is a feature of OOPs which makes it possible to give the same name to more than one methods within a class if the arguments passed differ.

What is method overriding?

Method overriding is a feature of OOPs by which the child class or the subclass can redefine methods present in the base class or parent class. Here, the method that is overridden has the same name as well as the signature meaning the arguments passed and the return type.

What is the difference between multiple and multilevel inerhitance?

Multiple Inheritance: Multiple inheritance comes into picture when a class inherits more than one base class Example: A class defining a child inherits from two base classes Mother and Father Multilevel inheritance: Multilevel inheritance means a class inherits from another class which itself is a subclass of some other base class Example: A class describing a sports car will inherit from a base class Car which inturn inherits another class Vehicle

What is the difference between a class and an object?

Objects: A real-world entity which is an instance of a class An object acs like a variable of the class An object is a physical entity Objects take memory space when they are created Objects can be declared as and when required Class: A class is basically a template or a blueprint within which objects can be created Binds methods and data together into a single unit A class is a logical entity A class does not take memory space when created Classes are declared just once

What is operator overloading?

Operator overloading refers to implementing operators using user-defined types based on the arguments passed along with it.

Polymorphic Parameters

Parametric polymorphism allows a name of a parameter or method in a class to be associated with different types. In the below example I have defined content as a String and later as an Integer

Java - Coercion

Polymorphic coercion deals with implicit type conversion done by the compiler to prevent type errors. A typical example is seen in an integer and string concatenation.

What is polymorphism?

Polymorphism refers to the ability to exist in multiple forms. Multiple definitions can be given to a single interface. For example, if you have a class named Vehicle, it can have a method named speed but you cannot define it because different vehicles have different speed. This method will be defined in the subclasses with different definitions for different vehicles.

What are pure virtual functions?

Pure virtual functions or abstract functions are functions that are only declared in the base class. This means that they do not contain any definition in the base class and need to be redefined in the subclass.

Rules of Inheritance in Java

RULE 1: Multiple Inheritance is NOT permitted in Java. RULE 2: Cyclic Inheritance is NOT permitted in Java. RULE 3: Private members do NOT get inherited. RULE 4: Constructors cannot be Inherited in Java. RULE 5: In Java, we assign parent reference to child objects. RULE 6: Constructors get executed because of super() present in the constructor.

Can achieve polymorphism in Java by

Run time polymorphism Compile time polymorphism

Types of polymorphism in Java

Static polymorphism Dynamic Polymorphism

What is static polymorphism?

Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of compile-time polymorphism is method overloading.

java - Create objects using the newInstance( ) method of class Class

This method is not used often for creating objects. This method of creating an object is used if we know the class name and the default constructor is public in nature. To use this method for creating objects we need to handle 3 exceptions ClassNotFoundException- This exception occurs if the JVM is unable to find the class which is passed as an argument. InstantiationException- This exception occurs if the given class does not contain a default constructor. IllegalAccessException- This exception occurs if we don't have access to the specified class.

java - Create Objects using clone( ) method

What if the object that we want to create should be a copy of an already existing object? In that case, we can use the clone( ) method. clone( ) is a part of the Object class but it cannot be used directly as it is a protected method.

java - Create objects using 'new' keyword

While programming in Java you might've definitely come across the 'new' keyword. It is a keyword used to create an object which is dynamically allocated memory i.e memory to these objects is assigned at runtime. And this dynamic allocation is required most of the time while creating objects. Hence this method is used more often than others.

Java - classes consist of following

fields methods constructors blocks nested classes

What is OOPs?

type of programming based on objects. Individual objects are grouped into classes. OOPs implements inheritance, polymorphism, and hiding. Allows binding data and code together.


Conjuntos de estudio relacionados

Noise Exposure & Hearing Conservation Exam 1

View Set

يتر ميلاد : فط للقراءة

View Set

Chief Complaints & Vital Signs (Test Review)

View Set

QUIZ 3 Sampling and Hypothesis Testing

View Set

Systems Analysis & Design: CH 10 MC Questions

View Set

Chapter 32: Assessment of Hematologic Function

View Set

What is the capital of this state?

View Set

Saunders Psychiatric Medications

View Set