Java Unit 2
Java Unit 2
Java Unit 2
2marks
1.what is a class?(apr-13,apr-12,apr-14)
A class is a template for manufacturing objects. You declare a class by specifying
theclass keyword followed by a non-reserved identifier that names it. A pair of matching open
and close brace characters ({ and }) follow and delimit the class's body.syntax:
class identifier
{
// class body
}
4.what is an array?(apr-12,apr-14,apr-15,nov-15)
Java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type. syntax:dataType
arrayRefVar[]; example: double myList[];
5.what is a vector?(apr-12,nov-14,apr-16)
A vector is a dynamic array of object reference it can hold any classes or objects or primitive
data. It is not necessary that the object should belong to a particular object.It is very useful to
store different objects irrespective of their size.Array’s can also be easily implemented as
vectors. Syntax :Vector name = new vector ( ) ;
Example :Vector int list = new vector (4) ;
6.what is an objects?(nov-14,apr-16,nov-15)
An object is an instance of a class. every object must belong to a class. objects are created
and eventually destroyed – so they only live in the program for a limited time.
7.what is inheritance?(nov-14,nov-13)
Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. It is used to manage the hierarchical order.The class which
inherits the properties of other is known as subclass (derived class, child class) and the class
whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
Extends is the keyword used to inherit the properties of a class. syntax of extends
keyword.
class Super
{
statements;
1.Single Inheritance:
Single Inheritance is the simple inheritance of all, When a class extends another
class(Only one class) then we call it as Single inheritance. The below diagram represents the
single inheritance in java where Class B extends only one class Class A. Here Class B will be
the Sub class and Class A will be one and only Superclass.
2.Multiple Inheritance:
Multiple Inheritance is nothing but one class extending more than one class. Multiple
Inheritance is basically not supported by many Object Oriented Programming languages such
as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has
to manage the dependency of more than one Parent class. But you can achieve multiple
inheritance in Java using Interfaces.
3.Multilevel Inheritance:
In Multilevel Inheritance a derived class will be inheriting a parent class and as well as
the derived class act as the parent class to other class. As seen in the below
diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In
Short ClassA parent for ClassB and ClassB parent for ClassC.
4.Hierarchical Inheritance:
In Hierarchical inheritance one parent class will be inherited by many sub classes. As per
the below example ClassAwill be inherited by ClassB, ClassC and ClassD. ClassA will be acting
as a parent class for ClassB, ClassC and ClassD.
5.Hybrid Inheritance:
Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again
Hybrid inheritance is also not directly supported in Java only through interface we can achieve
this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be
acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting
as Parent for ClassD.
Example:
import java.io.*;
class ClassA
{
public void dispA()
{
System.out.println("hai");
}
}
public class ClassB extends ClassA
{
public void dispB()
{
System.out.println("hello");
}
public static void main(String args[])
{
ClassB b = new ClassB();
b.dispA();
b.dispB();
}
}
output:
hai
hello
1. List . add Element (item) – add the item specified to the list at the end.
2. List . elementAt(10) –gives the name of the 10th object.
3. List . size –gives the number of objects present.
4. List . removeElement(item) –removes the specified item from list.
5. List . allElements( ) –removes all elements in the list.
6. List . copyInts(array) –copy all the elements from list to array.
7. List . insertElementAt(item , n) –insert the item at ‘n’ position.
8. List . removeElementAt(n) –removes the item stored in the ‘n’ position of the list.
1.Default:
When a Method is set to default it will be accessible to the classes which are defined in
the same package. Any Method in any Class which is defined in the same package can access the
given Method via Inheritance or Direct access.
2.Public Access:
Public Access modifiers Specifies that data Members and Member Functions those are
declared as public will be visible in entire class in which they are defined. Public Modifier is
used when we wants to use the method any where either in the class or from outside the class.
The Variables or methods those are declared as public are accessed in any where , Means in any
Class which is outside from our main program or in the inherited class or in the class that is
outside from our own class where the method or variables are declared.
3.Protected Access:
The Methods those are declared as Protected Access modifiers are Accessible to Only in
the Sub Classes but not in the Main Program , This is the Most important Access Modifiers
which is used for Making a Data or Member Function as he may only be Accessible to a Class
which derives it but it doesn’t allows a user to Access the data which is declared as outside from
Program Means Methods those are Declared as Protected are Never be Accessible to Another
Class The Protected will be Accessible to Only Sub Class and but not in your Main Program.
4.Private Access:
The Methods or variables those are declared as private Access modifiers are not would be
not Accessed outside from the class or in inherited Class or the Subclass will not be able to use
the Methods those are declared as Private they are Visible only in same class where they are
declared. By default all the Data Members and Member Functions is Private, if we never
specifies any Access Modifier in front of the Member and Data Members Functions.
Example :-
import java . io . *;
class super
{
int x;
super(int x)
{
this.x=x;
}
void display()
{
system.out.println("super x:"+x);
}
}
class sub extends super
{
int y;
sub(int x,int y)
{
super(x);
this.y=y;
}
void dispaly()
{
system.out.println(" super x="+x);
system.out.println("sub y='+y);
}
}
class over
{
public static void main(string args[])
{
sub s1=new sub(10,20);
s1.display();
}
}
output:
super x=10
sub y=20
The wrapper classes have number of unique method for handling primitive data types and
object.
Example:
import java.io.*;
class ClassA
{
public void dispA()
{
System.out.println("hai");
}
}
public class ClassB extends ClassA
{
public void dispB()
{
System.out.println("hello");
}
public static void main(String args[])
{
ClassB b = new ClassB();
b.dispA();
b.dispB();
}
}
output:
hai
hello
7)Give a brief account on Method Overloading?(apr-16,apr-14,apr-15)
It is possible to create two or more methods that have same name ,with different
parameter list is referred to as method overloading. When a method is called, it matches up with
the method name and type of arguments.
Rules for overloading:
output:
8
16
8)Write a java program to sort the given set of N numbers in descending order?(nov-15)
import java.io.*;
import java.util.Scanner;
public class Descending_Order
{
public static void main(String args[])
{
int n, temp;
Scanner s = new Scanner(System.in);
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
output:
Enter the elements:1 3 6 2
descending order:6 3 2 1
data member
method
constructor
block
class and interface
Syntax:
class <class_name>
{
data member;
method;
}
we have created a Student1 class that have only one method. We are creating the object of the
Student1 class by new keyword and printing the objects value.
class Student1
{
void disp()
{
system.out.println("hai");
}
class stud
{
public static void main(String args[])
{
Student1 s1=new Student1();
s1.disp();
}
}
output: hai
10 marks
The advantages of method overloading is that it provides an easy way to handle default
parameter values. Assume that a method has one required parameter and two optional
arguments. Three overloaded forms of this method can be defined. These steps are two or three
parameters.
Example :
import java.io.*;
class overload
{
int num(int x)
{
System . out . println(“Double x=”+x);
return(x);
}
int num(int x,int y)
{
System . out . println(“Int x and y=”+x+” “+y);
return(x+y);
}
}
class overloadDemo
{
public static void main (String args[])
{
Overload od=new overload();
Od . num(8);
Od . num(8,8);
}}
output:
8
16
Abstract class:
If a class contain any abstract method then the class is declared as abstract class. An
abstract class is never instantiated. It is used to provide abstraction.
Syntax :abstract class class_name { }
Abstract method:
Method that are declared without any body within an abstract class is known as abstract
method. The method body will be defined by its subclass. Abstract method can never be final
and static. Any class that extends an abstract class must implement all the abstract methods
declared by the super class.
Syntax :abstract returntype methodname ();
import java.io.*;
import java.util.*;
class number
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter the no. of elements");
int n = br.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements");
for(int i=0;i<n;i++)
arr[i] = br.nextInt();
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println("\nsorted array");
System.out.println("\n");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
}
OUTPUT:
Enter the no. of elements
4
Enter the elements
2
1
53
22
sorted array
1
2
22
53
4)write a java program to sort the given set of names in alphabetical order?(apr-13,apr-14)
import java.io.*;
import java.util.Scanner;
public class Alphabetical_Order
{
public static void main(String[] args)
{
int n;
String temp;
Scanner s = new Scanner(System.in);
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter the names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Sorted Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ",");
}
System.out.print(names[n - 1]);
}
}
1.Single Inheritance:
Single Inheritance is the simple inheritance of all, When a class extends another
class(Only one class) then we call it as Single inheritance. The below diagram represents the
single inheritance in java where Class B extends only one class Class A. Here Class B will be
the Sub class and Class A will be one and only Superclass.
2.Multiple Inheritance:
Multiple Inheritance is nothing but one class extending more than one class. Multiple
Inheritance is basically not supported by many Object Oriented Programming languages such
as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has
to manage the dependency of more than one Parent class. But you can achieve multiple
inheritance in Java using Interfaces.
3.Multilevel Inheritance:
In Multilevel Inheritance a derived class will be inheriting a parent class and as well as
the derived class act as the parent class to other class. As seen in the below
diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In
Short ClassA parent for ClassB and ClassB parent for ClassC.
4.Hierarchical Inheritance:
In Hierarchical inheritance one parent class will be inherited by many sub classes. As per
the below example ClassAwill be inherited by ClassB, ClassC and ClassD. ClassA will be acting
as a parent class for ClassB, ClassC and ClassD.
5.Hybrid Inheritance:
Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again
Hybrid inheritance is also not directly supported in Java only through interface we can achieve
this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be
acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting
as Parent for ClassD.
Example:
import java.io.*;
class ClassA
{
public void dispA()
{
System.out.println("hai");
}
}
public class ClassB extends ClassA
{
public void dispB()
{
System.out.println("hello");
}
public static void main(String args[])
{
ClassB b = new ClassB();
b.dispA();
b.dispB();
}
}
output:
hai
hello
6)what is Method Overriding?Explain with an exampleprogram?(nov-15)
A method is overridden when a subclass contains a method in the same name, return type
and parameters (i.e.) the method which are declared static in the super class cannot be overridden
by the super class.
Rules for Method Overriding:
The argument list should be exactly the same as that of the overridden method.
The return type should be the same or a subtype of the return type declared in the original
overridden method in the superclass.
The access level cannot be more restrictive than the overridden method's access level. For
example: If the superclass method is declared public then the overridding method in the
sub class cannot be either private or protected.
Instance methods can be overridden only if they are inherited by the subclass.
A method declared final cannot be overridden.
A method declared static cannot be overridden but can be re-declared.
If a method cannot be inherited, then it cannot be overridden.
A subclass within the same package as the instance's superclass can override any
superclass method that is not declared private or final.
A subclass in a different package can only override the non-final methods declared public
or protected.
Constructors cannot be overridden.
Super :
When a sub class needs to refer to its immediate super class. It can done by using a
keyword called super. There are two types of super ,
1.Call the constructor of the super class.
2.Call the method of the subclass
Example :-
import java . io . *;
class super
{
int x;
super(int x)
{
this.x=x;
}
void display()
{
system.out.println("super x:"+x);
}
}
class sub extends super
{
int y;
sub(int x,int y)
{
super(x);
this.y=y;
}
void dispaly()
{
system.out.println(" super x="+x);
system.out.println("sub y='+y);
}
}
class over
{
public static void main(string args[])
{
sub s1=new sub(10,20);
s1.display();
}
}
output:
super x=10
sub y=20