JAVA / OOP Flashcards
What are the 4 pillars of Object Oriented Programming?
- Abstraction
- Polymorphism
- Inheritance
- Encapsulation
What is a class?
A class acts as a blueprint in which objects can be created. A simple example is a pattern to create a dress. The pattern is the class and the dress that is made from it is the object. Classes contain the relevant data and behavior(s) an object has.
What is an object?
An instance of a class - contains the basic properties / states and behavior. Objects are instantiated using the ‘new’ keyword
Discuss some of Java’s naming conventions:
- Class names are always the same as the file name, with each class being its own unique & separate file.
- Class names are capitalized.
- Variables should be logical & concise.
- CamelCasing
Java Naming Conventions
- Class names are always the same as the file name, with each class being its own unique & separate file.
- Class names are capitalized and should be a noun
- Methods should be a verb and start with a lowercase letter
- Variables start with a lowercase letter, should be logical & concise, avoiding single character names.
- Packages should be all lowercase. Separate multiple words with dots:
java.lang
- Constants should be all uppercase and multiple words separated by underscores:
MAX_VALUE
- CamelCasing
Difference between a Class & an Object?
Class: A class is the building block that leads to Object-Oriented Programming. It is a user-defined data type, that holds its own data members and member functions, which can be accessed and used by creating an instance of that class. It is the blueprint of any object.
Object: An object is an instance of a class. All data members and member functions of the class can be accessed with the help of objects. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created), memory is allocated. For example, considering the objects for the Account class is Saving account, Current account, etc.
Method Overloading
Method Overloading is a Compile time polymorphism. It occurs between methods in the same class. In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading, the return type can or can not be the same, but we have to change the parameter because, in java, we can not achieve the method overloading by changing only the return type of the method.
Method Overriding
Overriding occurs between superclass and subclass. Method Overriding is a Run time polymorphism. In method overriding, the derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, the return type must be the same or co-variant (return type may vary in the same direction as the derived class).
What is a constructor?
A constructor is a special method that is used to create an object out of a class definition. In Java they have the same name as the class Constructors are called first when an object of a class is instantiated. It is essentially responsible for setting up that object including assigning specific data members with values passed to it. If a class lacks a constructor, Java assigns one at run time. This empty constructor which maintains no parameters is known as the default constructor. Constructors cannot be private, they are always public. You can have more than 1 constructor which accepts different parameters (overloading).
Where are objects / variables stored?
- Normal variables are stored in the stack
- Objects are stored in the heap. The “variables” that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.
- The JVM allocates the heap, inside which resides the string pool
What are some of the benefits / features of Java?
- Object-oriented - every is an object / easily extended.
- Platform independent - Compiled and run on the JVM (“Write once, run anywhere” / Bytecode)
- Simple - relatively simple to learn if understand OOP
- Secure - enables virus / tamper-free systems.
- Architecture-Neutral - file format which compiled code can run on many processors.
- Portable - due to its arch. neutrality / no implementation independent aspects.
- Supports Multi-threading
- Robust - emphasizes compile time & runtime error checks
- Interpreted - byte code translated on the fly to native machine languages.
- Memory Management (JVM handles garbage collection)
- Backward Compatible (Older versions supported despite new changes)
- Maturity & Support (Libraries & extensive documentation)
Discuss Exception Handling (Errors vs. Exceptions)
- Errors refers to an illegal operation performed by the user which results in the abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing. It is of three types: Compile-time, Run-time, Logical.
- Exceptions refer to an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions. They indicates conditions that a reasonable application might want to catch. Exceptions are the conditions that occur at runtime and may cause the termination of the program. But they are recoverable using try, catch and throw keywords. (Checked & Unchecked Exceptions)
Discuss Abstract Classes:
Abstract classes aim to implement the same or different behavior among multiple related objects. They provide just a method signature which allows you to implement a framework / template method design pattern. They can contain both abstract and concrete methods but the abstract methods must be implemented elsewhere. Concrete methods are distinguished by having a full name & method body. They cannot be instantiated. Can have final methods which will force the subclass to not change the method body.
Discuss Interfaces:
Interfaces are totally incomplete. They do not have any properties. They must show that the inheriting children are capable of doing something. You cannot instantiate abstract classes or interfaces. By default, all methods are abstract. Methods themselves are the only things composing an interface, it merely acts as a contract with the compiler which states that these specific methods must be implemented by whatever class implements the interface. They cannot be static or involve any implementation.
Can you achieve multiple inheritance in Java?
No, BUT - you can achieve multiple inheritance through the use of interfaces. (I.e. extending an abstract class, and implementing multiple interfaces).