Documente Academic
Documente Profesional
Documente Cultură
Use:
Abstract Class: A class which contains the abstract keyword in its declaration is
known as abstract class.
Use: Let’s say we have a class Animal that has a method sound() and the
subclasses(see inheritance) of it like Dog, Lion, Horse, Cat etc. Since the animal
sound differs from one animal to another, there is no point to implement this
method in parent class. This is because every child class must override this
method to give its own implementation details, like Lion class will say “Roar” in
this method and Dog class will say “Woof”.
Use:
i. It is used to achieve total abstraction.
ii. Since java does not support multiple inheritance in case of class, but by using
interface it can achieve multiple inheritance .
iii. It is also used to achieve loose coupling.
iv. Interfaces are used to implement abstraction. So the question arises why use
interfaces when we have abstract classes?
The reason is, abstract classes may contain non-final variables, whereas
variables in interface are final, public and static.
Ex:
Encapsulation: The process of binding the group of data within a single unit of class
Use:
Tightly encapsulated class: the class conations private properties class said to be
tightly encapsulation.
Use:
Use:
Use:
Inheritance: The ability to create classes that shares the attributes and methods
of existing classes.
Use: Inheritance is a parent-child relationship of a class which is mainly used for code reusability
Types of Inheritance:
Use:
Constructors: A constructor is an instance method that usually has the same name
as the class.
2. Parameterized constructor
Use:
Array List: Java ArrayList class uses a dynamic array for storing the elements. It inherits
AbstractList class and implements List interface.
Use:
import java.util.*;
class TestCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Ravi");//Adding object in arraylist
list.add("Vijay");
list.add("Ravi");
list.add("Ajay");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
} } }
Arrays:
Fixed in size
Collections:
HashMap is a Map based collection class that is used for storing Key & value pairs, it is
denoted as HashMap<Key, Value> or HashMap<K, V>. ... It does not sort the stored
keys and Values. You must need to import java.util.HashMap or its super class in order
to use the HashMap class and methods.
Ex:
Note:
o It maintains no order.
Java Hashtable class implements a hashtable, which maps keys to values. It inherits
Dictionary class and implements the Map interface.
o It is synchronized.
Ex:
Difference between HashSet and HashMap
HashSet contains only values whereas HashMap contains entry(key and value).
List Vs Set:
List is an ordered collection it maintains the insertion order, which means upon
displaying the list content it will display the elements in the same order in which they
got inserted into the list.
Set is an unordered collection, it doesn’t maintain any order. There are few
implementations of Set which maintains the order such as LinkedHashSet (It maintains
the elements in insertion order.
If the requirement is to have only unique values then Set is your best bet as any
implementation of Set maintains unique values only.
If there is a need to maintain the insertion order irrespective of the duplicity then List is
a best option. Both the implementations of List interface – ArrayList and LinkedList
sorts the elements in their insertion order.
Use:
Consider a situation, Employee object contains many informations such as id, name, emailId
etc. It contains one more object named address, which contains its own informations such
as city, state, country, zipcode etc. as given below.
class Employee{
int id;
String name;
Address address;//Address is a class
...
}
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
Use:
Object: Objects have states and behaviors. Example: A dog has states - color, name,
breed as well as behaviors – wagging the tail, barking, eating. An object is an instance
of a class.
Use:
String: The String class is used to manipulate character strings that cannot be
changed. Simply stated, objects of type String are read only and immutable.
Use:
String Buffer: Java StringBuffer class is used to create mutable (modifiable) string. The
StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
Use:
2) String is slow and consumes more StringBuffer is fast and consumes less memory
memory when you concat too many when you cancat strings.
strings because every time it creates
new instance.
3) String class overrides the equals() StringBuffer class doesn't override the equals()
method of Object class. So you can method of Object class.
compare the contents of two strings
by equals() method.
Note: String: once create string object we can’t perform any change in the existing object
and if trying to perform any changes with those changes a new object will be created.
Note: String Buffer: once we create a string buffer object we can perform any type of
changes in the existing object this changeable behavior mutability.
Use:
Throws: When we are throwing any exception in a method and not handling it, then
we need to use throws keyword in method signature to let caller program know the
exceptions that might be thrown by the method.
Use:
Use:
Static: Static keyword in java is mainly used to save memory as with the help of static
we can declare data one time and access it in a whole program where we need the
same data more than one time in a program. After creating static keyword there is no
need to declare that data again and again. Static keyword can be used with:
Nested class
Method
Variable
Use:
Non-static: Memory for non-static variable is created at the time of create an object of
class. These variable should not be preceded by any static keyword Example: These
variables can access with object reference.
Use:
Void: main method won’t return anything to JVM that’s why void It will not return any
return type.
Use:
Main: Won’t checked by compiler at runtime but JVM Check runtime for method inside
the class or not.
Use:
Singleton Class: Singleton Pattern says that just"define a class that has only one instance and
provides a global point of access to it".
Use:
Use:
THIS keyword:
Use:
Super keyword: The super keyword in java is a reference variable which is used to refer
immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
Use:
Packages: A Java package is a mechanism for organizing Java classes. Package are used in Java, in-
order to avoid name conflicts and to control access of class, interface and enumeration
Use:
Garbage collection: The garbage collector is a program which runs on the Java Virtual
Machine which gets rid of objects which are not being used by a Java application anymore.
It is a form of automatic memory management
Use:
Try-catch: The try block contains set of statements where an exception can occur. A try
block is always followed by a catch block, which handles the exception that occurs in
associated try block. A try block must be followed by catch blocks or finally block or both.
A catch block is where you handle the exceptions, this block must follow the try block
Use:
Checked exceptions: Checked Exceptions are checked at compile time only, these are
must and should handle by the programmer.
Use:
Use:
What is Assertion:
Asserts helps us to verify the conditions of the test and decide whether test has
failed or passed.
Ex:
driver.navigate().to("http://google.com");
Assert.assertEquals("Google", driver.getTitle()); }
Note:
There are other assert methods that could be used to verify the text in place of
assertEquals()
assertTrue(strng.contains(“Search”));
assertTrue(strng.startsWith(“Google”));
assertTrue(strng.endsWith(“Search”));
Array vs ArrayList:
Array is used represent a group of elements in a single element and this elements must be homogenous
and must fixed number.
package Sample;
for(String ss:s) {//for each loop to get all values from array
System.out.println(ss);
}
System.out.println(a[i]);
}
//3rd approach to print the data from array
for(int aa:a) { //a is reference variable * foreach loop
System.out.println(aa);
}
}
int eID;
String ename;
Emp(int eID, String ename){
this.eID = eID;
this.ename = ename;
}
}
**********************************************************
Emp [] e = new Emp[5];
e[0] = e1;
e[1] = e2;
e[4] = e3;
for(Object o : e) {
if(o instanceof Emp) {
Emp E = (Emp)o;
-- ArrayList:
Dynamic sized arrays in Java that implement List interface.
ArrayList<Type> arrL = new ArrayList<Type>();
class Test
arr[0] = 1;
arr[1] = 2;
System.out.println(arr[0]);
/*............ArrayList..............*/
L.add(1);
L.add(2);
System.out.println(arrL.get(0));
}
Difference between arraylist and linkedlist ?