OOP Flashcards
What is OOP
It is a system where programs are considered as a collection of objects
What is a class?
A class in Java is a blueprint which includes all your data. A class contains fields (variables) and methods to describe the behavior of an object.
What is an object ?
It is an instance of a class. The object has state (represented by attributes of an object ) and behavior(represents by methods of an object ).The object of a class can be created by using thenewkeyword.
What is Inheritance
Inheritance is a process where one class acquires the properties of another.+:reusability
What is encapsulation ?
It is a mechanism of wrapping up the data and code together as a single unit.It is data hiding so the data in a class is hidden from other classes. It can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
What is abstraction ?
Abstraction(Interface / abstract classes):Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
In Java abstraction is achieved byinterfaces andabstract classes.
What is polymorphism ?
Polymorphism (overload /override):Polymorphism is the ability of a variable, function or object to take multiple forms.
What is a constructor ?
It is used to initialize an object.
It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.
There are two types of constructors:
Default Constructor it initializes the instance variables and objects with the default values
Parameterized Constructor it initializes the instance variables with the provided values.
What is a destructor ?
Destructor is a method which is automatically called when the object ismade ofscope or destroyed. Destructor name is also same asclass name but with the tilde symbol before the name.
What is an abstract class ?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible It can be inherited. An abstract class may or may not conatin abstract methods
What is an interface
An interface is a collection of abstract method.Any class that implements an interface defines the methods of the interface.
Interface VS Abstract class ?
Type of methods:Interface can have only abstract methods. Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also. Final Variables:Variables declared in a Java interface are by default final. An abstract class may contain non-final variables. Type of variables:Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. Implementation:Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class. Inheritance vs Abstraction:A Java interface can be implemented using keyword “implements” and abstract class can be extended using keyword “extends”. Multiple implementation:An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces. Accessibility of Data Members:Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.
What is Exception Handling ?
Exception is an event that occurs during the execution of a program.
Exceptions can be of any type — Run time exception, Error exceptions. Those exceptions are handled properly through exception handling mechanism like try, catch and throw keywords.
What is method overriding ?
Method overriding is a feature that allows a subclass to provide the implementation of a method that overrides in the main class. It will override the implementation in the superclass by providing the same method name, same parameter, and same return type.
What is method overloading ?
Overloading is the same method with different arguments, and it may or may not return the equal value in the same class itself.