Java Unit 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

A.

M JAIN COLLEGE (SHIFT-II)


DEPARTMENT OF SOFTWARE APPLICATIONS
PREVIOUS YEAR QUESTION AND ANSWER
PROGRAMMING IN JAVA (UNIT-II)

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
}

2.Define method overloading?(apr-13)


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. syntax:
class identifier
{
returntype methodname()
{statement;}
returntype methodname(parameter list)
{statementn;}
}

3.what is an abstract method?(apr-13)


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 ();

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;

class Sub extends Super


{
statements;
}
8.what is an abstract class?(apr-16)
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. Although it does not provide
100% abstraction because it can also have concrete method. Syntax :abstract class classname { }

9.write any two advantages of vector class?(nov-13)


It is convenient to use vector and also to store objects.
A vector can be used to store a list of objects that may vary in size.
We can add and delete object from the list as we required.

10.List the access control modifiers in java?(apr-14)


Access Controls modifiers are used to defined where a method and Data Member of
class will be used either inside a class ,outside a class ,in inherited class or in main Method. It
tells us about the Scope of Methods where they would be used . Various types of Access
Modifiers are as follows:-
1.Default
2.Public
3.Protected
4.Private
11.what is a constructor?(nov-15,apr-15)
Constructor is a method used to give initial values to instance variable of the objects. A
constructor is never called directly. It is always called with the new operator. The new operator is
used to create an instance of a class, it allocates memory for the object, initialize the data
members and call the constructor method. There are two types of constructors they are:
 Default Constructor (Constructor with no argument)
 Parameterized Constructor
5 marks:

1)Describe different forms of inheritance in java?(apr-13,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:
class Super
{
statements;

class Sub extends Super


{
statements;
}

The types of inheritance which is supported by Java.


1.Single Inheritance
2.Multiple Inheritance (Through Interface)
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance (Through Interface)

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

2)Explain any five methods in the vector class?(nov-15)


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. An instant of vector class are also known as vector content elements that can be
stored and retrieved in several basis.Syntax :Vector name = new vector ( ) ; Example :Vector
int list = new vector (4) ;
Advantages :
 It is convenient to use vector and also to store objects.
 A vector can be used to store a list of objects that may vary in size.
 We can add and delete object from the list as we required.

vector methods are ;

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.

3)Explain different visibility controls in java?(apr-13)


Visibility Controls are used to defined where a method and Data Member of class will
be used either inside a class ,outside a class ,in inherited class or in main Method. It tells us
about the Scope of Methods where they would be used . Various types of Access Modifiers are
as follows:-
1.Default
2.Public
3.Protected
4.Private

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.

4)Describe Method Overriding in Java?(nov-14)


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

1.Call the constructor of the super class :


A sub class can call a constructor method defined by its super class by using the
keyword called super. Syntax : super (parameters);

2.Call the methods of the sub class :


A class can access a hidden member variable through its super class. A member can
be either a method (or) instance of variable. Syntax : super.member ;

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

5)Give a brief account on wrapper classes in java?(nov-14,apr-14)


Wrapper class in java provides the mechanism to convert primitive into object and object
into primitive. autoboxing and unboxing feature converts primitive into object and object into
primitive automatically. The automatic conversion of primitive into object is known and
autoboxing and vice-versa unboxing. One of the eight classes of java.lang package are known as
wrapper class in java. The list of eight wrapper classes are given below:

Data type Wrapper class


boolean Boolean
int Integer
float Float
double Double
long Long
char Character
byte Byte
short Short

The wrapper classes have number of unique method for handling primitive data types and
object.

1.Converting primitive numbers to object number using construct method :


Integer Int val = new Integer (i) ; -primitive integer to Integer object.
Float Float val = new Float (f) ; -primitive float to Float object.
Double Double val = new Double (d) ; -primitive double to double object.
Long Long val = new Long (l) ;

2.Conversion of numbers string to primitive method :


Int I = Intval . intVal( ) ; -convert object to primitive Integer
Float f = Floatval . floatVal( ) ; -convert object to primitive float
Double d = Doubleval . doubleVal ( ) ; -convert object to primitive double
Long l = Longval . longVal ( ) ; -convert object to primitive long

3.Conversion number to string using string ( ) method :


Str = Integer .toString (i) ; -convert primitive integers to string
Str = Float .toString (f) ; -convert primitive float to string
Str = Double .toString (d) ; -convert primitive double to string
Str = Long .toString (l) ; -convert primitive long to string

4.Converting string object to numeric object using static method value of ( )


Double val = Double . value of (str) ; -converts string to double object
Int val = Integer . value of (str) ;
Float val = Float .value of (str) ;
Long val = Long .value of (str) ;

5.Converting numeric string to primitive numeric using parse Int method ( ) :


Int I = Integer .parseInt(str) ; -converting string to primitive Integer
Long I = Long .parseLong(str) ; -converting string to long

6)Write a java program to explain single inheritance?(apr-12)


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: class Super
{
statements;

class Sub extends Super


{
statements;
}
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.

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:

1. Overloading can appear in the same class or a subclass


2. Overloaded methods MUST change its number of argument or its type.
When declaring two or more methods in same name complier differentiate them by its
arguments, so you cannot declare two methods with the same signature
3. Overloaded methods CAN have different return type.
The compiler does not consider return type when differentiating methods, so it?s legal to
change return type of overloaded method
4.Similarly overloaded method can have different access modifiers also.
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
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

9)Describe any five methods in the string class?(apr-12)


The java.lang.String class provides a lot of methods to work on string with the help of
these methods, we can perform operations on string such as trimming, concatenating, converting,
comparing, replacing strings.
1.charAt()
charAt() function returns the character located at the specified index.
String str = "studytonight";
System.out.println(str.charAt(2));
Output:u
2.equalsIgnoreCase()
equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or
lower case doesn't matters with this fuction ).
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
Output:true
3.length()
length() function returns the number of characters in a String.
String str = "Count me";
System.out.println(str.length());
Output:8
4.replace()
replace() method replaces occurances of character with a specified new character.
String str = "Change me";
System.out.println(str.replace('m','M'));
Output:Change Me
5.toLowerCase()
toLowerCase() method returns string with all uppercase characters converted to
lowercase.
String str = "ABCDEF";
System.out.println(str.toLowerCase());
Output:abcdef
6.trim()
This method returns a string from which any leading and trailing whitespaces has been removed.
String str = " hello ";
System.out.println(str.trim());
Output:hello

10)write the general syntax of a class and describe with an example?(nov-13)


A class is a group of objects that has common properties .It is a template from which objects
are created. A class in java contain;

 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

1) What Is Method Overloading?Explain With An Example Program?(apr12)


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:

1. Overloading can appear in the same class or a subclass

2. Overloaded methods MUST change its number of argument or its type.


When declaring two or more methods in same name complier differentiate them by its
arguments, so you cannot declare two methods with the same signature

3. Overloaded methods CAN have different return type.


The compiler does not consider return type when differentiating methods, so it?s legal to
change return type of overloaded method

4.Similarly overloaded method can have different access modifiers also.

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

2)Elucidate abstract classes and their usage with neat diagram?(nov-13)

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 ();

Example of Abstract class


abstract class A
{
abstract void callme();
}
class B extends A
{
void callme()
{
System.out.println("this is callme.");
}
class x
{
public static void main(String[] args)
{
B b=new B();
b.callme();
}
}
Output :
this is callme.

An Abstract Class Example:


Classes Rectangle, Line, Bezier, and Circle Inherit from Graphic Object.First, you declare an
abstract class, Graphic Object, to provide member variables and methods that are wholly shared
by all subclasses, such as the current position and the moveTo method. Graphic Object also
declares abstract methods for methods, such as draw or resize, that need to be implemented by all
subclasses but must be implemented in different ways. The Graphic Object class can look
something like this:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
Each nonabstract subclass of GraphicObject, such as Circle and Rectangle, must provide
implementations for the draw and resize methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}

3)Write a java program to sort the given set of n numbers?(nov-14)

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]);
}
}

output:Enter the names:


orange
apple
sorted order
apple
orange

5)Explain various types of inheritance in java?(apr-16)


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:
class Super
{
statements;
}
class Sub extends Super
{
statements;
}

The types of inheritance which is supported by Java.


1.Single Inheritance
2.Multiple Inheritance (Through Interface)
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance (Through Interface)

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

1.Call the constructor of the super class :


A sub class can call a constructor method defined by its super class by using the
keyword called super.Syntax : super (parameters);

2.Call the methods of the sub class :


A class can access a hidden member variable through its super class. A member can
be either a method (or) instance of variable. Syntax : super.member ;

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

7)Explain about string manipulation in java?


Strings, which are widely used in Java programming, are a sequence of characters. In the
Java programming language, strings are objects.The Java platform provides the String class to
create and manipulate strings.The java.lang.String class provides a lot of methods to work on
string with the help of these methods, we can perform operations on string such as trimming,
concatenating, converting, comparing, replacing strings.
1.charAt()
charAt() function returns the character located at the specified index.
String str = "studytonight";
System.out.println(str.charAt(2));
output:u
2.equalsIgnoreCase()
equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or
lower case doesn't matters with this fuction ).
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
Output:true
3.length()
length() function returns the number of characters in a String.
String str = "Count me";
System.out.println(str.length());
Output:8
4.replace()
replace() method replaces occurances of character with a specified new character.
String str = "Change me";
System.out.println(str.replace('m','M'));
Output:ChangeMe
5.toLowerCase()
toLowerCase() method returns string with all uppercase characters converted to
lowercase.
String str = "ABCDEF";
System.out.println(str.toLowerCase());
Output:abcdef
6.trim()
This method returns a string from which any leading and trailing whitespaces has been
removed.
String str = " hello ";
System.out.println(str.trim());
Output:hello
7.toString() with Concatenation
Whenever we concatenate any other primitive data type, or object of other classes with a
String object,toString() function or valueOf() function is called automatically to change the other
object or primitive type into string, for successful concatenation.
int age = 10;
String str = "He is" + age + "years old.";
In above case 10 will be automatically converted into string for concatenation
using valueOf() function.
8.toUpperCase()
This method returns string with all lowercase character changed to uppercase.
String str = "abcdef";
System.out.println(str.toUpperCase());
Output:ABCDEF
Example:
import java.io.*;
class vowel
{
public static void main(string args[]
{
string s;
char c[]=new char[90];
char v[]={'a','e','i','o','u','A','E','I','O','U'};
int v1=0,sp=0,n=0,ch=0;
system.out.println("Enter the string");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readline();
c=s.toCharArray();
for(int i=0;i<s.length();i++)
{
if(c[i]>=48&&c[i]<58)
n++;
else
if(c[i]>=65&&c[i]<91)||c[i]>=96&&c[i]
{
ch++;
for(int k=0;k<10;k++)
{
if(c[i]==v[k])
v1++;
}
}
else
{
sp++;
}
}
}
system.out.println("no.of vowels"+v1);
system.out.println("no.of digits"+n);
system.out.println("no.of speci char"+sp);
int con=ch-v1;
system.out.println("no.of constants"+con);
}
}
output:
Enter the string: 9
no.of vowels:0
no.of digits:1
no.of speci char:0
no.of constants:0

You might also like