Interview Questions and Answers

Abstract class vs Interface

An interface is an empty shell, there are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.

// I say all motor vehicles should look like this:
interface MotorVehicle
{
void run();

int getFuel();
}

// my team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

int fuel;

void run()
{
print("Wrroooooooom");
}


int getFuel()
{
return this.fuel;
}
}

Abstract Class- Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class. Lots of people say that in abstract class at least your one method should be abstract.


// I say all motor vehicles should look like this :
abstract class MotorVehicle
{

int fuel;

// they ALL have fuel, so why not let others implement this?
// let's make it for everybody
int getFuel()
{
return this.fuel;
}

// that can be very different, force them to provide their
// implementation
abstract void run();


}

// my team mate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
void run()
{
print("hurrr");
}
}


The key technical differences between an abstract class and an interface are:

  • Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
  • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).
  • When inheriting an abstract class, a concrete child class must define the abstract methods, whereas an an abstract class can extend another abstract class and abstract methods from the parent class don't have to be defined.
  • Similarly, an interface extending another interface is not responsible for implementing methods from the parent interface. This is because interfaces cannot define any implementation.
  • A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.
  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

MySQL Cluster

MySQL Cluster is a technology providing shared-nothing clustering and auto-sharding for the MySQL database management system. It is designed to provide high availability and high throughput with low latency, while allowing for near linear scalability.[2]MySQL Cluster is implemented through the NDB or NDBCLUSTER storage engine for MySQL ("NDB" stands for Network Database).

MySQL Cluster is designed around a distributed, multi-master ACID compliant architecture with no single point of failure. MySQL Cluster uses automatic sharding (partitioning) to scale out read and write operations on commodity hardware and can be accessed via SQL and Non-SQL (NoSQL) APIs

Database Index

A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookups and efficient ordering of access to records.

Indexes are also type of tables, which keep primary key or index field and a pointer to each record into the actual table.

The users cannot see the indexes, they are just used to speed up queries and will be used by Database Search Engine to locate records very fast.

INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert or update, database need to insert or update index values as well.

Unique Key

A unique index means that two rows cannot have the same index value.
Syntax -

CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...);
 
ALTER TABLE tbl_name ADD UNIQUE index_name (column_list) 

Primary Key

A primary key uniquely identify a row in the table. A table can have only one Primary Key.
Syntax-
ALTER TABLE tbl_name ADD PRIMARY KEY (column_list)

Full Text Index

FULLTEXT indexes are only useful for full text searches done with the MATCH() / AGAINST() clause.
Synatx-
ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list)

Method Overriding vs Method Overloading



Post a Comment