JAVA THEORY

If you have any queries please leave a message here
Your Message
×


CLASS AND OBJECT




CONSTRUCTOR

A constructor is a member function of a class which initializes objects of a class. In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.

A constructor is different from normal functions in following ways :

Type of Constructor :




INHERITANCE

The technique of deriving a new class from an old one is called inheritance. The old class is referred to as base class and the new class is referred to as derived class or subclass.

The existing class from which the derived class gets inherited is known as the base class. It acts as a parent for its child class and all its properties i.e. public and protected members get inherited to its derived class.




How will you call parameterized constructor and overrided method from parent class in sub-class ?

If sub-class has the same method as declared in the parent class, it is known as method overriding in java.

//Parent Class
class Base
{
private double side;

//Parameterized Constructor
Base(double s)
{
side=s;
}

void AreaCalculation()
{
double area=side*side;
System.out.println("Square Area = "+area);
}
}

//Child Class
class Derived extends Base
{
private double length;
private double breadth;

//Parameterized Constructor
Derived(double l,double b)
{
length=l;
breadth=b;
}

//Method Overriding
void AreaCalculation()
{
if(length==breadth)
{
//Call the Constructor of Parent Class
super(length);
//Call the Method of Parent Class
super.AreaCalculation();
}
else
{
double area=side*side;
System.out.println("Rectangle Area = "+area);
}
}
}




Can a super class object reference a sub-class object ?

Yes we can assign sub class object to super class reference.

//Parent Class
class Base
{
int number;
public void display()
{
System.out.println("Base class display method is called");
}
}

//Child Class
class Derived extends Base
{
int value;
public void display()
{
System.out.println("Derived class display method is called");
}
}

class ObjectReference
{
public static void main(String args[])
{
Base obj=new Derived();
obj.number=50;
obj.display();
System.out.println("Number = "+obj.number);
}
}

NOTE - When a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. This is why we can not access subclass methods.




Illustrate the uses of 'this' and 'super' keywords ?

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Example 1

class Square
{
private double side;
Square(double side)
{
this.side=side;
}
void AreaCalculation()
{
double area=side*side;
System.out.println("Square Area = "+area);
}
}

Example 2

class Square
{
private double side;
Square()
{
//Call Parameterized Constructor
this(0);
}

Square(double side)
{
this.side=side;
}

void AreaCalculation()
{
double area=side*side;
System.out.println("Square Area = "+area);
}
}

More Examples of this Keyword


We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same methods or variables.

//Parent Class
class Base
{
int fact;
void Factorial(int n)
{
fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
}
}

//Child Class
class Derived extends Base
{
void Factorial(int n)
{
//Call the Overriding Method of Parent Class
super.Factorial(n);
System.out.println("Factorial = "+super.fact);
}
}

More Examples of super Keyword




In Java, explain how to call a constructor from another

Calling a constructor from the another constructor of same class is known as Constructor chaining. It can be done using this( ) keyword for constructors in same class or using super( ) keyword to call constructor from the base class.

Example 1

class Test
{
//Default Constructor will call another Constructor using this keyword from same class
Test()
{
//Call Parameterized Constructor 1
this(5);
System.out.println("Default Constructor");
}

//Parameterized Constructor 1
Test(int n)
{
//Call Parameterized Constructor 2
this(n,10);
System.out.println(n);
}

//Parameterized Constructor 1
Test(int x, int y)
{
System.out.println(x*y);
}
}

Example 2

class Base
{
String name;
Base()
{
this("Hi");
System.out.println("Default Constructor of Base Class");
}
Base(String name)
{
this.name=name;
System.out.println("Parameterized Constructor of Base Class");
}
}

class Derived extends Base
{
Derived()
{
System.out.println("Default Constructor of Derived Class");
}
Derived(String name)
{
//Invokes Base Class Constructor
super(name);
System.out.println("Parameterized Constructor of Derived Class");
}
public static void main(String args[])
{
Derived obj=new Derived("Test");
}
}

More Details of Constructor Chaining




what is multithreading ? What are the two different ways to create multithreaded program ?

Multithreading in java is a process of executing multiple threads simultaneously. Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

We use multithreading than multiprocessing because threads share a common memory area. They do not allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation etc.

Advantages of Java Multithreading


There are two ways to create a thread :

  1. By extending Thread class
  2. By implementing Runnable interface


Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.

Commonly used Constructors of Thread class :


Commonly used Methods of Thread class :


The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().


Java Thread Example by extending Thread class

class MultiThread extends Thread
{
public void run()
{
System.out.println("Thread is running...");
}
public static void main(String args[])
{
MultiThread ob1=new MultiThread();
ob1.start();
}
}

Java Thread Example by implementing Runnable interface

class MultiThread implements Runnable
{
public void run()
{
System.out.println("Thread is running...");
}
public static void main(String args[])
{
MultiThread ob1=new MultiThread();
Thread th =new Thread(ob1);
th.start();
}
}

NOTE - If you are implementing Runnable interface in your class, then you need to explicitly create a Thread class object and need to pass the Runnable interface implemented class object as a parameter in its constructor.

More Details on Thread




what is the difference among final, finally and finalize ?

final


finally

NOTE - There are some situations where finally block does not executes e.g if exception occurs in finally block and if you call System.exit( ) in program.

import java.io.*;
public class Test
{
public static void main(String[] args) throws Exception
{
PrintWriter out=null;
try
{
out=new PrintWriter(new File("D:\\Textfile.txt"));
out.write("JAVA PROGRAMMING");
out.flush();
}
catch(IOException e)
{
System.out.println("Catch Block");
}
finally
{
if(out!=null)
{
System.out.println("Closing PrintWriter");
out.close();
}
else
System.out.println("PrintWriter Not Open");
}
}
}

finalize

class Test
{
//Overriding finalize( ) method
public void finalize()
{
System.out.println("Finalize Called");
}
public static void main(String[] args)
{
Test ob1=new Test();
ob1=null;
//Requesting JVM to call Garbage Collector method
System.gc();
}
}



what is the role of garbage collector in Java ?




what are packages ? Explain the steps to create packages.

Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.

Advantages of using a Package in Java


There are two types of packages in Java :

  1. Built-in package - These packages consist of a large number of classes which are a part of Java API.Some of the commonly used built-in packages are : java.io , java.util , java.lang , java.applet , java.awt , java.net etc.

  2. User defined package - These are the packages that are defined by the user.


I have created a class PrimeCheck inside a package name UserDefinePackage. To create a class inside a package, declare the package name in the first statement in your program. A class can have only one package declaration.

package UserDefinePackage;
public class PrimeCheck
{
public int isPrime(int n)
{
int count=0;
for(int i=1;i
{
if(n%i==0)
count++;
}
return count;
}
}

Now lets see how to use this package in another program.

import UserDefinePackage.PrimeCheck;
class PackageTest
{
public static void main(String args[])
{
PrimeCheck pc=new PrimeCheck();
if(pc.isPrime(10)==0)
System.out.println("Prime Number");
else
System.out.println("Not Prime");
}
}

In the above program I have imported the package as UserDefinePackage.PrimeCheck, this only imports the PrimeCheck class. However if you have several classes inside package UserDefinePackage then you can import the package like this (import UserDefinePackage.* ;) , to use all the classes of this package.




Abstract class in Java

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).

Abstract Method

Method that are declared without any body within an abstract class are called 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.

Abstraction in Java

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only important things to the user and hides the internal details. For example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.

Abstraction lets you focus on what the object does instead of how it does.

There are two ways to achieve abstraction in Java

  1. Abstract class (0 to 100%)
  2. Interface (100%)

abstract class Bank
{
abstract double RateOfInterest();
abstract double ATMcharges();
}

class SBI extends Bank
{
double RateOfInterest()
{
return 6.2;
}
double ATMcharges()
{
return 100.0;
}
}

class PNB extends Bank
{
double RateOfInterest()
{
return 6.5;
}
double ATMcharges()
{
return 150.0;
}
}

class InterestCheck
{
public static void main(String args[])
{
Bank ob1=new SBI();
System.out.println("SBI Interest Rate = "+ob1.RateOfInterest()+" %");
System.out.println("SBI ATM Charges = "+ob1.ATMcharges()+" %");
Bank ob2=new PNB();
System.out.println("PNB Interest Rate = "+ob2.RateOfInterest()+" %");
System.out.println("PNB ATM Charges = "+ob1.ATMcharges()+" %");
}
}



Interface in Java

Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how. It is the blueprint of the class.

Difference between Interface and Class

interface Bank
{
double RateOfInterest();
double ATMcharges();
}

class SBI implements Bank
{
public double RateOfInterest()
{
return 6.2;
}
public double ATMcharges()
{
return 100.0;
}
}

class RBL implements Bank
{
public double RateOfInterest()
{
return 6.8;
}
}

class InterestCheck
{
public static void main(String args[])
{
Bank ob1=new SBI();
System.out.println("SBI Interest Rate = "+ob1.RateOfInterest()+" %");
System.out.println("SBI ATM Charges = "+ob1.ATMcharges()+" %");
Bank ob1=new RBL();
System.out.println("RBL Interest Rate = "+ob1.RateOfInterest()+" %");
}
}



How Multilevel Inheritance is done in Java

When a class extends a class, which extends anther class then this is called multilevel inheritance. For example class C extends class B and class B extends class A then this type of inheritance is known as multilevel inheritance.

class Test1
{
protected long fact;
protected double p;
}

class Test2 extends Test1
{
public void Factorial(long n)
{
fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
System.out.println("Factorial = "+fact);
}
}

class Test3 extends Test2
{
public void PowerCalculation(int base, int power)
{
p=1;
for(int i=1;i<=power;i++)
p=p*base;
Syatem.out.println("Answer = "+p);
}
}

class Inheritance
{
public static void main(String args[])
{
Test3 ob=new Test3();
ob.Factorial(10);
ob.PowerCalculation(2,10);
}
}




What is a Wrapper class in Java

Wrapper classes are used to convert any data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.

class Test
{
public static void main(String args[])
{
int a=10;
//Primitive to Integer object conversion
Integer intobj = new Integer(a);
double d=10.25
//Primitive to Double object conversion
Double doubleobj = new Double(b);
float c=20.4
//Primitive to Float object conversion
Float floatobj = new Float(c);
char d='A';
//Primitive to Character object conversion
Character charobj=d;
//Object to Primitive type convertion
int x=intobj;
double y=doubleobj;
char z=charobj;
}
}



What is the difference between character based and byte based io streams in Java

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream.

import java.io.*;
public class CopyFile
{
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
try
{
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int ch;
while ((ch = in.read()) != -1)
{
out.write(ch);
}
}
finally
{
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}

Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter.

import java.io.*;
public class CopyFile
{
public static void main(String args[]) throws IOException
{
FileReader in = null;
FileWriter out = null;
try
{
in = new FileReader("input.txt");
out = new FileWriter("output.txt");
int ch;
while ((ch = in.read()) != -1)
{
out.write(ch);
}
}
finally
{
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us