Java Flashcards
What is OOP?
Object-Oriented Programming. a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object, Classes, Inheritance, Polymorphism, Abstraction, Encapsulation.
What is an object?
Any entity that has a state and behavior. An instance of a class. It contains an address and takes up some space in memory. Can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
what is a class?
In the context of Java, classes are templates that are used to create objects, and to define object data types and methods. Blueprint from which you can create an individual object. Does not consume any space.
Advantages of OOP over Prodcedure Oriented Programming.
- Makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
- Provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
What is Functional Programming?
using functions to program.
What is a constructor?
a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object. A special type of method which is used to initialize the object.
When is a constructor called?
When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. Without parameters, these constructors are called automatically, otherwise, parameterized constructors can be called using initializer list.
Rules to remember while creating a constructor.
- Constructor name must be the same as its class name
- A Constructor must have no explicit return type
- A Java constructor cannot be abstract, static, final and synchronized
What is a Default Constructor?
a no-args constructor that the Java compiler inserts on your behalf; it contains a default call to super(); which is the default behavior. If you implement any constructor then you no longer receive a default constructor.
What is a Parameterized Constructor?
A Constructor which has a specific number of parameters. Used to provide different values to the distinct objects. Can have any number of parameters in the constructor.
What is Constructor Overloading?
a technique of having more than one constructor with different parameter lists.
Constructor:
- A Constructor is used to initialize the state of an object.
- A Constructor must not have a return type.
- The Constructor is invoked implicitly.
- The Java compiler provides a default constructor if you don’t have any constructor in a class.
- The Constructor name must be same as the class name.
Method:
- A method is used to expose the behavior of an object.
- A method must have a return type.
- A method is invoked explicitly.
- The method is not provided by the complier in any case.
- The method name may or may not be same as class name.
What is Inheritance?
A mechanism in which one object acquires all the properties and behaviours of a parent object. New classes can be built on top of existing classes. Can inherit existing class and reuse methods and fields of the parent class. Parent-child relationship.
Advantages of Inheritance
- For Method Overriding (so runtime polymorphism can be achieved).
- For code Resuability.
Terms used in Inheritance?
- Class: A class is a group of objects which has common properties. It is a template or blueprint from which objects are created.
- Subclass/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. Its also called a base class or a parent class.
- Reusability: reuse fields and methods of the existing class when you create a new class.
Three types of Inheritance in Java:
Single, multilevel, and hierarchical.
Why is multiple inheritance not supported in Java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in Java. To reduce ambiguity when inheriting.
What is Polymorphism?
the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.s. Any Java object that can pass more than one IS-A test is considered to be polymorphic. all Java Objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
What is Method Overloading?
If a class has multiple methods having same name but different in parameters.
Different ways to overload a method?
- By changing number of arguments.
- By changing the data type.
Usage and rules of Method Overriding:
Usage;
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism.
Rules:
- The method must have the same name as in the parent class.
- The method must have the same parameter as in the parent class.
- There must be an IS-A relationship (inheritance).