JAVA Unit 2
JAVA Unit 2
}
}
5. Write a few points about ‘default constructor’.
A constructor is called "Default Constructor" when it doesn't have any
parameter.
Ex:
class A
{
A()
{
}
}
6. What does ‘static’ keyword do in a class?
A class can be made static only if it is a nested class.
Nested static class doesn’t need a reference of Outer class. In this case, a static
class cannot access non-static members of the Outer class.
}
}
Parameterized constructor: Constructor with arguments is called as parameterized
constructor.
Ex:
class A
{
A(int a)
{
}
}
8. Difference b/w ‘string’ class and ‘string buffer’ class.
String class String Buffer
String class is slower while performing StringBuffer class is faster while performing
concatenation operation. concatenation operation.
String class uses String constant pool. StringBuffer uses Heap memory
Return type A constructor cannot have any return A method can have a
type. return type.
Abstract class can have both an abstract A class can only have
as well as concrete methods. concrete methods.
When more than one method having same When the method name and parameters
name with different parameters within the are same in the super class and the child
same class class is called overriding
It occurs between the methods in the It occurs between superclass and sub class
same class
You can use any access modifier The access modifier of super class method
and subclass method must be same.
}
}
//Creating a child class
class Bike2 extends Vehicle
{
//defining the same method as in the parent class
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[])
{
Bike2 obj = new Bike2(); //creating object
obj.run(); //calling method
}
}
21. What is string and Explain string methods with example.
A string is an object that represents a sequence of characters or char values.
The java.lang.String class is used to create a Java string object.
There are two ways to create a String object:
string literal : Java String literal is created by using double quotes.
Example: String s=“Welcome”;
new keyword : Java String is created by using a keyword “new”.
Example: String s=new String(“Welcome”);
Java String Methods
Java String length(): The Java String length() method tells the length of
the string. It returns count of total number of characters present in the
String.
Example:
public class Example{
public static void main(String args[]
{
String s1="hello";
String s2="whatsup";
System.out.println("string length is: "+s1.length());
System.out.println("string length is: "+s2.length());
}
}
Java String compareTo(): The Java String compareTo() method compares
the given string with current string. It is a method of ‘Comparable’
interface which is implemented by String class.
Example:
public class CompareToExample
{
public static void main(String args[])
{
String s1="hello";
String s2="hello";
String s3="hemlo";
String s4="flag";
System.out.println(s1.compareTo(s2)); // 0
System.out.println(s1.compareTo(s3)); //-1
System.out.println(s1.compareTo(s4)); // 2
}
}
Java String concat() : The Java String concat() method combines a specific
string at the end of another string and ultimately returns a combined
string.
Example:
public class ConcatExample
{
public static void main(String args[])
{
String s1="hello";
s1=s1.concat("how are you");
System.out.println(s1);
}}
Java String IsEmpty() : This method checks whether the String contains
anything or not.
Example:
public class IsEmptyExample
{
public static void main(String args[])
{
String s1="";
String s2="hello";
System.out.println(s1.isEmpty()); // true
System.out.println(s2.isEmpty()); // false
}}
Java String Trim() : The java string trim() method removes the leading
and trailing spaces.
Example:
public class StringTrimExample
{
public static void main(String args[])
{
String s1=" hello ";
System.out.println(s1+"how are you"); // without
trim()
System.out.println(s1.trim()+"how are you"); // with trim()
}}
Java String toLowerCase() : The java string toLowerCase() method
converts all the characters of the String to lower case.
Example:
public class StringLowerExample
{
public static void main(String args[])
{
String s1="HELLO HOW Are You?”;
String s1lower=s1.toLowerCase();
System.out.println(s1lower);}
}
}
Java String toUpper() : The Java String toUpperCase() method converts
all the characters of the String to upper case.
Example:
public class StringUpperExample
{
public static void main(String args[])
{
String s1="hello how are you";
String s1upper=s1.toUpperCase();
System.out.println(s1upper);
}
}
22. Write a note on inheritance.
It is a mechanism in which one object acquires all the properties and behaviors of a
parent object.
When you inherit from an existing class, you can reuse methods and fields of the
parent class.
We can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
Advantages:
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates a new class that derives from an existing class.
The meaning of "extends" is to increase the functionality.
23. How to create object? What happens when you create objects?
The new keyword is used to create an object or instance of the class.
new keyword allocates memory (heap) for the newly created object and also
returns the reference of that object to that memory.
Syntax:
ClassName object = new ClassName();
The class container is the super class for the Component represents an object with
container of AWT graphical representation
26. Illustrate array declaration and accessing data elements using an example.
public class ArrayDemo
{
public static void main(String[] args)
{
int[] anArray; // declare an array of integers
Abstract methods don’t have body, they just have method signature.
If a class has an abstract method it should be declared abstract, the vice versa is not
true, which means an abstract class doesn’t need to have an abstract method
compulsory.
If a regular class extends an abstract class, then the class must have to implement all the
abstract methods of abstract parent class
Example:
3. Abstract class.
A class which is declared with the abstract keyword is known as an abstract
class.
It can have abstract and non-abstract methods (method with the body).
It needs to be extended and its method implemented.
It cannot be instantiated(objects cannot be created)
Syntax:
abstract class A
{
Abstract void display()
{
}
}
II. Default: The access level of a default modifier is only within the package.
It cannot be accessed from outside the package. If you do not specify any
access level, it will be the default.
Example:
package pack;
class A
{
void msg()
{
System.out.println(“Hello”);
}
}
import pack.*;
class B
{
public static void main(String args[])
{
A obj = new A();//Compile Time Error
Obj.msg();//Compile Time Error
}
}
III. Protected: The access level of a protected modifier is within the package
and outside the package through child class. If you do not make the child
class, it cannot be accessed from outside the package.
package pack;
public class A
{
protected void msg()
{
System.out.println(“Hello”);
}
}
package mypack;
import pack.*;
class B extends A
{
public static void main(String args[])
{
B obj = new B();
Obj.msg();
}
}
IV. Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package and
outside the package.
Understanding Java Access Modifiers
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
31. Explain array of object references.
as name says it stores an array of objects.
An array that conations class type elements are known as an array of objects. It stores
the reference variable of the object.
Unlike a traditional array that store values like string, integer, Boolean, etc
an array is a collection of the same data type that dynamically creates objects and can
have elements of primitive types. Java allows us to store objects in an array.
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double