Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked list each node in the list stores the contents of the node and a pointer or reference to the next node in the list. It does not store any pointer
What is the difference between an interface and abstract class?
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
Why are interface variables static and final by default?
Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned
Diamond Problem ?
Due to diamond problem java doesn’t support multiple inheritance.
1 2 3 4 5 6 7 |
GrandParent / \ / \ Parent1 Parent2 \ / \ / Test |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// A Grand parent class in diamond class GrandParent { void fun() { System.out.println("Grandparent"); } } // First Parent class class Parent1 extends GrandParent { void fun() { System.out.println("Parent1"); } } // Second Parent Class class Parent2 extends GrandParent { void fun() { System.out.println("Parent2"); } } // Error : Test is inheriting from multiple // classes class Test extends Parent1, Parent2 { public static void main(String args[]) { Test t = new Test(); t.fun(); } } |
Multiple inheritance is not supported by Java using classes , handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there
Association, Composition and Aggregation in Java
Association
Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
// Java program to illustrate the // concept of Association import java.io.*; // class bank class Bank { private String name; // bank name Bank(String name) { this.name = name; } public String getBankName() { return this.name; } } // employee class class Employee { private String name; // employee name Employee(String name) { this.name = name; } public String getEmployeeName() { return this.name; } } // Association between both the // classes in main method class Association { public static void main (String[] args) { Bank bank = new Bank("Axis"); Employee emp = new Employee("Neha"); System.out.println(emp.getEmployeeName() + " is employee of " + bank.getBankName()); } } |
Output:
1 |
Neha is employee of Axis |
In above example
Doubly linked list implementation
A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes previous and
What is difference between concrete classes and abstract classes?
Abstract classes usually have partial or no implementation. On the other hand, concrete classes always have full implementation of its behavior.Unlike concrete classes, abstract classes cannot be instantiated. Therefore abstract classes have to be extended in order to make them useful. Abstract classes may contain abstract methods, but concrete classes
Define public, static, final ?
public: for the accessibility across all the classes, just like the methods present in the interface
static: as interface cannot have an object, the interfaceName.variableName can be used to reference it or directly the variableName in the class implementing it.
final: to make them constants. If 2 classes implement the same interface
Difference between association and dependency?
Association –> A has-a C object (as a member variable)
Dependency –> A references B (as a method parameter or return type)
Dependency – A change in a class affects the change in it’s dependent class. Example- Circle is dependent on Shape (an interface). If you change Shape , it affects Circle too. So, Circle has a dependency on Shape.
Association– means
What are Object Oriented Programming (OOPs) Concept ?
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Association
- Aggregation
- Composition
Abstraction
Abstraction is the concept of hiding the internal details and describing things in simple terms. For example, a method that adds two integers. The internal processing of the method is hidden from the outer world. There are many ways to achieve abstraction in object-oriented programmings, such as encapsulation