Explaining Inheritance Support In Java Information Technology Essay

Ans:Inheritance creates a new class definition by building upon an existing definition (you extend the original class). Inheritance is the process by which one object acquires the properties of another object. Inheritance allows well-tested procedures to be reused and enables changes to make once and have effect in all relevant places.

The new class can, in turn, can serve as the basis for another class definition:

@ all Java objects use inheritance

@ every Java object can trace back up the inheritance tree to the generic class Object

The keyword extends is used to base a new class upon an existing class.Several pairs of terms are used to discuss class relationships (these are not keywords)

@ note that traditionally the arrows point from the inheriting class to the base class, and the base class is drawn at the top – in the Unified Modeling Language (UML) the arrows point from a class to another class that it depends upon (and the derived class depends upon the base class for its inherited code)

@ the parent class/child class terms are not recommended, since parent and child is more commonly used for ownership relationships (like a GUI window is a parent to the components placed in it)

Java supports only four types of inheritance:

@ Single Inheritance

@ Multi Level Inheritance

@ Hierarchical Inheritance

@ Hybrid inheritance.

SINGLE INHERITANCE

Single Inheritance the type of inheritance where there is only one deriived class for base class.

class Room

{

int length;

int breadth;

Room(int x,int y)

{

length=x;

breadth=y;

}

int area()

{

return(length*breadth);

}

}

class BedRoom extends Rom // inheriting Room

{

int height;

BedRoom(int x,int y,int z)

{

super(x,y); // pass values to superclass

height=z;

}

int volume()

{

return(length*breadth*height);

}

}

class InherTest

{

public static void main(String args[])

{

BedRoom room1=new BedRoom(14,12,10);

int area1=room1.area(); //superclass method

int volume1=room1.volume(); //baseclass method

System.out.println(“Area1=”+area1);

System.out.println(“Volume1=”+Volume1);

}

}

Output is:

Area1=168

Volume1=1680

MULTI-LEVEL INHERITANCE

public class A {

public String getName(){

return “Neetu”;

}

}

public class B extends A {

public int getAge() {

return 21;

}

}

public class C extends B {

public String getAddress(){

return “Tarn Taran, Amritsar”;

}

}

public class D extends C {

public void print {

System.out.println(getName());

System.out.println(getAge());

System.out.println(getAddress());

}

}

This method would print the following in the console:

Neetu

21

Tarn Taran , Amritsar

HYBIRD INHERITANCE

Java does not support the ability for one class to extend more than one other class. However, Java does allow for one class to implement any number of interfaces.

Let us consider the below example for hybrid inheritance.

public class A extends C implements D, E {

}

Here A extends C which is direct single inheritance, By implementing D & E we are trying to achieve multiple inheritance to the limit that Java allows us. And if C in turn has extended some other class then we have multi level inheritance as well.

This can be termed as Hybrid inheritance. The presence of more than one form of inheritance.

HIERARCHICAL INHERITANCE

It is a type of inheritance where one or more derived classes is derved from common( or one ) base class .

class Info

{

int pid;

char branch;

char year;

Info(int p,char ch,char y)

{

pid = p;

branch = ch;

year = y;

}

void display()

{

System.out.println(“nPIDt: “+pid);

System.out.print(“Brancht: “);

if(branch == ‘i’)

System.out.println(“Information Technology”);

if(branch ==’e’)

System.out.println(“Electronics and Telecommunication”);

if(branch ==’c’)

System.out.println(“Computer Science”);

Read also  World Where Technology Is Everywhere Information Technology Essay

System.out.print(“Yeart: “);

if(year == ‘f’)

System.out.println(“FE”);

if(year == ‘s’)

System.out.println(“SE”);

if(year == ‘t’)

System.out.println(“TE”);

}

}

class Fe extends Info

{

int c;

int cpp;

Fe(int p,char ch,char y,int m1,int m2)

{

super(p,ch,y);

c = m1;

cpp = m2;

}

void fdisplay()

{

display();

System.out.println(“Performance:”);

System.out.println(“tCt”+c);

System.out.println(“tC++t”+cpp);

}

}

class Se extends Info

{

int vb;

int html;

Se(int p,char ch,char y,int m1,int m2)

{

super(p,ch,y);

vb = m1;

html= m2;

}

void sdisplay()

{

display();

System.out.println(“Performance:”);

System.out.println(“tVBt”+vb);

System.out.println(“tHTMLt”+html);

}

}

class Te extends Info

{

int matlab;

int java;

Te(int p,char ch,char y,int m1,int m2)

{

super(p,ch,y);

matlab = m1;

java = m2;

}

void tdisplay()

{

display();

System.out.println(“Performance:”);

System.out.println(“tMATLABt”+matlab);

System.out.println(“tSJavat”+java);

}

}

class Language

{

public static void main(String args[])

{

Fe F = new Fe(1074,’i’,’f’,9,8);

Se S = new Se(1064,’e’,’s’,6,8);

Te T = new Te(1054,’c’,’t’,9,9);

F.fdisplay();

S.sdisplay();

T.tdisplay();

}

}

Q2. How is method overloading different from method overriding?

Ans: Overriding

So long as a class and its methods are not marked final, a subclass can declare a method with the same signature as one in a superclass and its own implementation. A typical example is the String toString() method, which is declared in the Object class. All objects implicitly inherit this method. The default implementation is to output the class name combined with its hash code in hexadecimal. The String class overrides the toString() method to give a Unicode representation of string content of the object. Any other object may override toString() with its own method body, to return its.

Overloading

In Java method overloading means creating more than a single method with same name with different signatures. In the example three methods are created with same name. Java understands these methods with there signatures. Java identifies the methods by comparing their signatures like return types, constructor parameters & access modifier used.

Overriding versus Overloading

When we override a method definition, the new method definition given in the derived class has the exact same number and types of parameters. On the other hand, if the method in the derived class were to have a different number of parameters or a parameter of a different type from the method in the base class, then the derived class would have both methods. That would be overloading. For example, suppose we added the following method to the definition of the class Student:

public String getName(String title)

{

return (title + getName());

}

In this case, the class Student would have two methods named getName: It would still inherit the method getName from the base class Person , and it would also have the method named getName that we just defined. This is because these two methods called getName have different numbers of parameters.

If we get overloading and overriding confused, we do have one small consolation. They are both legal. So, it is more important to learn how to use them than it is to learn to distinguish between them. Nonetheless, we should learn the difference between them.

Q3. Write a program to create a class Person having name, age and address as its fields. Derive a class Student from Person and add Roll number and Section details. Write appropriate constructors and methods to initialize, modify and display value of attributes. Modify the above program to create Teacher class from Person adding qualification and experience details?

Ans:-

Read also  Role Of Multimedia In Todays Society Information Technology Essay

public class Person

{

String name;

int age;

String address;

public Person()

{

name=”Neetu”;

age=21;

address=”Fatehabad,Tarn Taran”;

}

public void display()

{

System.out.println(“Name is : “+name);

System.out.println(“Age is : ” +age);

System.out.println(“Address is : ” +address);

}

}

public class Student extends Person

{

int roll_number;

String section=null;

public Student(int a,String b)

{

roll_number=a;

section=b;

}

public void disp()

{

System.out.println(“Roll Number is : “+roll_number);

System.out.println(“Section is :” +section);

}

public static void main(String args[])

{

Student z=new Student(5,”D”);

z.display();

z.disp();

}

}

public class Teacher extends Person

{

String qualification;

String experiance;

public Teacher(String c,String k)

{

qualification=c;

experiance=k;

}

public void show()

{

System.out.println(“Qualification is : ” +qualification);

System.out.println(“Experiance is : ” +experiance);

}

public static void main(String args[])

{

Teacher v=new Teacher(“B.tech IT”,”4 Yrs.”);

v.display();

v.show();

}

}

Q4. Interfaces support multiple inheritance but classes does not. Explain?

Ans:Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods. Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn’t get an implementation free ride.Java support method overriding means both superclass and subclass can have the same method name and number of arguments.If java support multiple inheritance, ambiguity problem will arise if two or more superclass have the same method name, and the super keyword will not be able to decide which superclass to call.TO remove such ambiguity problem java doen not support multiple inheritance through extend but it support it through interface.

Java does not support multiple inheritance , because of ambiguity 0r confusion problem.but multiple inheritance can be achieved through the concept called interfaces.

A class can implement one or more interfaces.

class A extends B extends C

{

//Not possible

}

class A extends B implements C

{

// Possible

}

from interface to class we are inheriting use the keyword implements. In the case of classes inheriting multiple classes it is not allowed because it leads to a variety of problems, including the diamond problem. For instance, if I have two supertypes with different implementations of the same method signature, which one should be used in the subtype.

PART – B

Q5. Write a program to organize and use classes and interfaces amongst various packages?

Ans: BASIC PACKAGES IN JAVA

1. java.io – input output operations

2. java.lang – basic language functionalities

3. java.math – arithmetic

4. java.net – networking applications

5. java.sql – database operations

6. jassva.awt – user interfaces, painting graphics and images

PROGRAM:

package bus;

public interface Sum2

{

int f=21;

int s=10;

public void Addition();

}

package hum;

public class FirstClass implements Sum2

{

int t,m;

public FirstClass()

{

t=10;

m=3;

}

public void Addition(){}

public void disp()

{

int n=f+s;

int o=t+m;

System.out.println(“Sum of numbers inherited from interface Sum :”+n);

System.out.println(“Sum of numbers initialised in constructer :”+o);

}

}

package tum;

import hum.FirstClass;

public class SecondClass extends FirstClass

{

public static void main(String args[])

{

SecondClass x=new SecondClass();

x.disp();

}

}

Q6. What are the advantages of using packages? How it provides data protection?

Ans: Packages is a very important concept in Java. In our day to day life, as a software developer, sometimes we need to handle small projects and at other times, big projects. In small projects, it is a normal practice to keep all the related files in a single directory. For example, all Java files use a single directory, all CSS files in some other directory, etc. But as the number of files increase when the project becomes big, it really becomes difficult to maintain all the files in the same directory. In Java this problem can be avoided by the use of packages. Packages provide an alternative to creating procedures and functions as stand-alone schema objects, and they offer the following advantages:

Read also  Negative Aspects of IT on Social Communication

Modularity: Logically related programming structures can be encapsulated in a named module. Each package is easy to understand, and the interface between packages is simple, clear, and well-defined.

Easier Application Design: Package specification and package body can be coded and compiled separately. A package specification can be coded and compiled without its body. Then, stored subprograms that reference the package can also be compiled. A user does not need to fully define the package body until he is ready to complete the application.

Hiding Information: The programming constructs declared in the package specification are public (visible and accessible to applications). The programming constructs declared in the package body are private (hidden and inaccessible to applications). The package body hides the definitions of the private constructs so that only the package is affected (not the application or any calling program) if the definitions change. This enables a user to change the implementation without having to recompile calling programs. Also, hiding the implementation details from users protects the integrity of the package.

Added Functionality: Packaged public variables and cursors persist for the duration of a session. In this way, they can be shared by all subprograms that execute in the calling environment. They also enable a user to maintain data across transactions without having to store it in the database. Private constructs also persist for the duration of the session, but can only be accessed within the package.

Better Performance: When a packaged subprogram is called for the first time, the entire package is loaded into the memory. As a result, later calls to related subprograms in the package do not require any further disk I/O. Packaged subprograms also stop cascading dependencies and thereby avoid unnecessary compilations.

Overloading: Procedures and functions, when used within a package, can be overloaded, i.e., multiple subprograms can be created with the same name in the same package, each taking different number of parameters or different types of parameters.

Q7. Write a program in Java to multiply a 3X4 matrix with a 4X5 matrix?

Ans: class MatrixMultiply{

public static void main(String[] args) {

int array[][] = {{5,6,7},{4,8,9}};

int array1[][] = {{6,4},{5,7},{1,1}};

int array2[][] = new int[3][3];

int x= array.length;

System.out.println(“Matrix 1 : “);

for(int i = 0; i < x; i++) {

for(int j = 0; j <= x; j++) {

System.out.print(” “+ array[i][j]);

}

System.out.println();

}

int y= array1.length;

System.out.println(“Matrix 2 : “);

for(int i = 0; i < y; i++) {

for(int j = 0; j < y-1; j++) {

System.out.print(” “+array1[i][j]);

}

System.out.println();

}

for(int i = 0; i < x; i++) {

for(int j = 0; j < y-1; j++) {

for(int k = 0; k < y; k++){

array2[i][j] += array[i][k]*array1[k][j];

}

}

}

System.out.println(“Multiply of both matrix : “);

for(int i = 0; i < x; i++) {

for(int j = 0; j < y-1; j++) {

System.out.print(” “+array2[i][j]);

}

System.out.println();

}

}

}

Order Now

Order Now

Type of Paper
Subject
Deadline
Number of Pages
(275 words)