Online Examination System for all learner!!! coming soon..

Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Java : Write a short note on Multilevel hierarchy.

In our previous tutorial we have discussed all about Inheritance. What is it? Why it is required? and its different types! Moreover, we have already come across Simple Inheritance, here we will discuss Multilevel Inheritance. It is nothing but the enhancement of the Simple Inheritance. From the type name it is pretty much clear that Inheritance is done at ‘n’ number of levels, where n>1.
In simple inheritance a subclass or derived class derives the properties from its parent class, but in multilevel inheritance a subclass is derived from a derived class. One class inherits only single class. Therefore, in multilevel inheritance, every time ladder increases by one. The lower most class will have the properties of all the super classes’.
So it will be like this:
  Person
      ↓
Employee
      ↓
Manager
N.B: Multilevel inheritance is not multiple inheritance where one class can inherit more than one class at a time. Java does not support multiple inheritance.
We will understand Multilevel Inheritance using the following example:
Code:
class Person{
  String personName;
  String address;
  Person(String personName,String address){
    this.personName = personName;
    this.address = address;
  }
  void showPersonalDetails(){
    System.out.println("Name is: "+personName);
  }
}

class Employee extends Person{
  String employeeID;
  double salary;
  Employee(String personName,String address,String employeeID,double salary){
    super(personName,address);
    this.employeeID = employeeID;
    this.salary = salary;
  }
}

class Manager extends Employee{
  int numberOfSubordinates;
    Manager(String personName,String address,String employeeID,double salary,int numberOfSubordinates){
    super(personName,address,employeeID,salary);
    this.numberOfSubordinates = numberOfSubordinates;
  }
}

class MultileveleInheritance{
  public static void main(String args[]){
    Person p =  new Person();
    Employee e = new Employee();
    Manager m = new Manager();
  }
}
Output:

Explanation of Code & Output

Here Person, Employee and Manager are three classes. Manager subclass is derived from Employee subclass which is again derived from Person class. All these classes has one to one relation. So Manager class is the most specialized class among the three which will have the access right to all the members of both Employee and Person. On the other hand, Person super class won’t have access to any of the members of its subclasses, same is true for Employee subclass which won’t have access to the any members of Manager subclass.
All these three classes are written in single java file for your understanding but generally these are put in three different java files.
Next we will checkout how to do method overloading in Java.

JAVA : Program to demonstrate concept of Polymorphism (Overriden).

 demonstrate concept of Polymorphism (Overriden).

Sourse Code:

class Funcover

{

public void calc(int x,int y)

{

int z;

z=x*y;

System.out.println("multiplication="+z);

}

}

class Override extends Funcover

{

public void calc(int x,int y)

{

int z;

z=x/y;

System.out.print("division="+z);

}

}

class Overrideex

{

public static void main(String arg[])

{

Funcover f1=new Funcover();

f1.calc(7,6);

Override f2=new Override();

f2.calc(14,2);

}

}

Output:

multiplication=42

division=7 

JAVA : Operators

There are four main types of operator.
  • Arithmetic operators
  • The Bitwise operators
  • Relational operators
  • Boolean logical operators

Arithmetic operators:

  • Addition: This operator is used to add the values. It is represented by “+“. For ex: A=a+4.
  • Subtraction:This operator is used to subtract the values. It is represented by “- “.  For example A = a - 4.
  • Multiplication: This operator is used to multiply the values. It is represented by “*“.
    For exampleA=a*4.
  • Division: This operator is used to divide the values. It is represented by “/“. For example: A=a/4.
  • Modulus: This operator is used to find the remainder of the values when divided. It is represented by “%“. For example A = 2 % 4. Where A = 0.
  • Increment: This operator is used to increases its operand by one. It is represented by  “++“. For example a = a++ which is equal to a= a+1.
  • Decrement: This operator is used to decreases its operands by one. It is represented by “—“. For example a = a— which is equal to a= a-1.

Bitwise operators:

  • Unary NOT: This inverts all of the bits of operand contained and it is represented by “~“. For example: ~00101010 = 11010101
  • And: It produces 1 bit if both operands are also 1 and it is represented by “&“. For example:00101010 , &00001111, 000101010.
  • OR: If either of the operand is one it produces 1 and It is represented by “|“. For example:00101010, |00001111, 00101111.
  • XOR: If either of the bit operand is 1, then result is also one otherwise its 0 and it is represented by “^”. For example: 00101010, ^00001111, 00100101.
  • Left shift: It shifts or moves all of the bits in the particular given value to the left side number of times that is been declared and it is represented by ” ≪ ”.
  • Right shift: It shifts or moves all of the bits in the particular given value to the right side number of times that is been declared and it is represented by ” ≫ ”.

Relational operators:

  • Equal to- This relation operator shows that the values are equal to each other and it is represented by “==”.
  • Not equal to- This relation operator shows that the values are not equal to each other and it is represented by “!=”.
  • Greater than- This relation operator shows that one value is greater when compared to other and it is represented by “>”. 
  • Less than-This relation operator shows that one value is less when compared to other and it is represented by “<”.
  • Greater than or equal to- This relation operator shows that one value is greater or equal but not less when compared to other and it is represented by “>=”.
  • Less than or equal to-This relation operator shows that one value is smaller or equal but not greater when compared to other and it is represented by “<=”.

Boolean logical operators:

  • Logical AND- It is represented by “&”.  For example : A & B = If a is false and b is true it results as false, but is both are true it results as true. Similarly when both are false it results as false. 
  • Logical OR- It is signed |. For example: A | B = If a is false and b is true it results as true, but is both are true it results as true. Similarly when both are false it results as false. 
  • Logical XOR- It is shown using “^”. For example: A ^ B = If a is false and b is true it results as true, but is both are true it results as false. Similarly when both are false it results as false. 
  • Logical Unary Not- It is depicted as “!”. For example: ! A = If a is false it results as true, but if a is true it results as false.

Assignment operators:

The assignment is the single equal sign that is represented by “=”.  The general representation
var=expression;
In this the variable, which is represented as var should be compatible with the type of expression.
Int a, b, c;
a = b = c = 100;

The ? Operator
This is used for replacement of if then else statements and it is represented as “?”.
For example:
expression a ? expression b : expression c
This above example states that if the expression a is true then expression b is evaluated otherwise expression c is evaluated.

JAVA : Aarray

  • It is a group of similar variables that would be referred by a common name.
  • The element available in the array is accessed through index.
  • This can be created and may have one or more dimension.

Types of arrays:

There are two main types of arrays are:
1. One-dimensional arrays
2. Multi-dimensional arrays

One-dimensional Arrays:

It’s a list of similar types of data. Before you create array you need to create a variable of any type.
Syntax:
Type variable-name [ ];
Int year_month [ ];

Multi-dimensional Arrays

It is arrays of arrays need to specify each dimensional array variables, additional index using other square brackets represented as.
Int twoD [ ] [ ] = new int [6] [7];
<<Previous Page                     Download PDF                    Next Page>> 

JAVA : Variables

It is basic unit of storage and variables have scope, visibility and lifetime.

Declaration of variables:

Int x, y, z;
Or
Int x=1, y=2, z=5;

Type conversion and casting:

If there is any compatibility existing between two types then java would automatically performs the conversion.

Java automatic conversion:

  • It will be performed when two conditions are met.
  • When the two types are compatible;
  • Destination type is larger than the source type.
  • After the two conditions are been satisfied a widening conversion takes place.

Casting compatible types:

  • It is a simple and explicit type of conversion.
  • When a floating type of conversion is attached to an integer type this type of conversion is called truncation.
  • It is a kind of conversion sometime called narrowing conversion
  <<Previous Page                     Download PDF                    Next Page>> 

JAVA : Data Type

Integers:

It includes whole valued signed numbers which are as follow.

Byte- Whose width is 8 and it ranges from -128 to 127. It is the smallest integer type. It is useful  when user is dealing with stream or flowing data from a network or a file.
Declaration of two byte variables called x and y is as follow
Byte x, y;

Short- The width of this type of integer is 16 and it ranges from -32,768 to 32,767. It is the least commonly used data type in java.
Declaration:
Short a;
Short b;

Int- The width of this type of integer is 32 and it ranges from -2,147,483,648 to 2,147,483,647. It is more efficient as compared to byte and short. It is commonly used to control loops and indexed arrays.

Declaration:
Int a;

Long-  The width of this type of integer is 64 and it ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. This is used for those values which are large enough that the integer cannot handle them.
Declaration:
Int light speed;
Long days;
Long second;
Seconds = days *24 *60*60;

Floating point type:

This is for real numbers used for calculation such as square roots, sine and cosine. These are of two types.

Float- Width in bits is 32 and range is from 1.4e-045 to 3.4e + 038. Float is used as a variable type for the fractional component, but specifies single precision. It can be used in representing dollars and cents.
Declaration:
Float hightemp;

Double- Width in bits is 64 and range is from 4.9e-324 to 1.8e + 308.it is used and is optimized in such a way that I can be used for high speed mathematic calculation. It has double precision which is faster than the single precision. Function such as sin()and sqrt() etc, return double values.
Declaration:
Double pi, r, a;
r = 10.4;
pi = 3.14;
a = pi* r * r;

Characters:

  • The data type used store or declare character in java is char.
  • It is 16-bit type in java. Range of a char is 0 to 65,536.
  • There are no chars which are negative in nature.
Program demonstration for char
Class charExample
{
Public static void main (String args []) {
Char a, b;
a=88;
b=’y’;
system.out.print (“a and b: “);
system.out.println (a + “ “ +b);
}
}

Booleans:

  • It is a primitive type for logical values.
  • This tends have only one of two possible values, true or false.
  • It is governed by if or for control state
  <<Previous Page                     Download PDF                    Next Page>> 

JAVA : Features of JAVA

Object-oriented programming:

  • This is the core feature of java.
  • This is to manage the increase in the complexity.
  • It provides a very sophisticated and well defined interface for the data.
  • It is also known as data controlling access code.
  • Another important feature of java being object oriented is abstraction.
  • Complexity can be managed using abstraction.

The three OOP principles:

  • Encapsulation- Its agenda is to manipulate the data and keep the data isolated and safe from the external interference and misuse. The encapsulation is done by the use of the protective wrapper. This prevents the external sources from accessing the data or the code.
  • Inheritance- In this the object would acquire the property of other object present. Itjust follows the concept of the hierarchical classification. This consists of classes, sub classes. Inheritance also is linked or interacts with encapsulation as well.
  • Polymorphism- It is means many ways to carry out the method but from one input.
 Byte code
This is highly optimized by set of instructions designed which is designed to be executed by Java virtual machine that is JVM.

JVM

  • It was designed as an interpreter for the byte code.
  • Another feature of java program is that it is simple.
  • This enables the professionals to learn.
  • Work in a very effective manner but it is also very easy to understand.

Robust

The ability that includes creating a robust program that can be a multiplatform program
are given a very high priority in design of Java.

Multithreading

The real world requirements are met by java which helps to achieve the requirement of
creating interactive and networked programs.

High performance

  • The advantage of being a multi platform functioning program helps to find the cross platform solution. 
  • It provides benefits of being an platform independent code with the help of java run time system.

Distributed

  • This is because it is been designed for the internet which has a distributed environment because of the handling of TCP/IP protocols.
  • This allows the program to find out methods across a network.
  • URL is used in this to access a file on internet.
  • This property supports RMI (Remote Method Invocation).

Dynamic

  • This is the action that is taken during the run-time such as to resolve, verify and add objects.
  • It provides us the function which will allow us to link code dynamically that will be safe.

Simple program

/* Call this file “Example.java” */
Class Example {
Public static void main (string args []) {
System.out.println (“this is a simple java program.”);
}
}

Command line argument to pass the class name is
C :\> java Example
Simple output of the above program
this is a simple java program.
Calling of the file in java cmd
Calling of the file: “Example.java”. 

Related Questions: 

  1. How JAVA differs from C++

JAVA : Introduction

  • Java was designed and conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank and Mike Sherdan, which was done at Sun Microsystems in year 1991.
  • It took almost 18 months for java to come into existence as a working version.
  • Initially java was known as “Oak”, which was then renamed as “Java” in year 1995. Since java had much had much of its character designed from C and C++.
  • This character inherited by the two well known and simple programming makes java more appealing to computer and it giants which would lead to a large scale success.
  • But java is misunderstood as the sophisticated internet version representation of C++.
  • It has significant difference practically and philosophically when compared to C++.
  • If you have good knowledge in C++ then you will find java as your cup of tea and you will at ease using and understanding java.

Therefore, there are two main reasons for the evolution of the computer languages. Java had enhanced and refined the object oriented scenario of C++. This gave more features to the users which are as follows:

  •  Multithreading.
  • Library which would provide easy internet access.
  • One of the java’s magic was the byte code. Byte code is set of instruction which is highly optimized and designed to be executed by JVM (Java Virtual Machine). It is an interpreter for byte code. This lead to the design of truly portable programs.

Java redesigned the internet with new feature and networked program which are as follows:

  • Applets - It is a kind of java program that is to be transmitted over and executedautomatically by java compatible web browsers.
  • Security - It provided the security of downloading various applets and programs from internet without containing any virus or Trojan horses.
  • Portability- Since there is large and different kind of operating systems therefore it provides the freedom of running in any operating system so its program can be used in different OS without any issues of compatibility.

The evolutions in java are as follows:

  • Java 1.0
  • Java 1.1
  • Java 1.2
  • J2SE
  • J2SE 1.2
  • J2SE 1.3
  • J2SE 1.4
  • J2SE 5
  • J2SE 5 made various changes to Java 

The new feature that was added is as follow:

  • Generics
  • Annotations
  • Auto boxing and auto-unboxing
  • Enumerations
  • Enhanced, for-each style for loop
  • Variable-length arguments
  • Static import
  • Formatted I/O
  • Concurrency utilities
 In J2SE 5, and the developers kit was called JDK 5. 1.5 used as internal version numberand this is referred as developer version number.Java became the center of innovation in computer technological world. The existence ofJVM and byte code changed the scenario of security and portability in the programmingworld. The way the new ideas are put into the language has been redefined by the JCP i.e.java community process.
<<Previous                        Downlod PDF                        Next>>

Related Questions:


    C Programming Online Test 1

    Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answ...