You are on page 1of 5

Assignment-3

1. What is inheritance and explain different inheritances.


Ans: - INHERITANCE- Inheritance means using the predefined code. This is the
main feature of OOP. With the advantage of inheritance we can use any code
that is previously created by this we uses the code which is previously defined
but always remember that we are only using the same code but not changing
code.
TYPES OF INHERITANCE1. Single Inheritance:- It is damn easy to understand. When a class extends
another one class only we call it as a single inheritance.
A

B
EXClass A
{
public void methodA()
{
System.out.println( Base Class Method);
}
}
{
public void methodB()
{
System.out.println(Child Class Method);
}
public static void main (String args [])
{
B obj= new B();
obj.method A();
obj.method B();

}
}
2. MULTIPLE INHERITANCE- It refers to the concept of one class extending
more than one base class or parent class. The problem with multiple
inheritance is that is derived class will have to manage the dependency on
two base classes
A
B

C
Note (1) - It is very rarely used in software projects. Using this inheritance often
leads to problems in the hierarchy. This results in unwanted complexity.
Note (2) - Most of new languages like small Talk, Java ,C++ do not support
Multiple Inheritance except C++
3. MULTILEVEL INHERITANCE- It refers to a mechanism in Object oriented
technology where one can inherit from a derived class . Thereby making this
derived class the base class for the new class.
A

B
EXAMPLEclass x
{
Public void methodY()
{
System.out.println(Class X method);
}
}
class Y extends X
{
public void methodY()
{

System.out.println( Class Y method)


}
}
class Z extends Y
{
Public void methodZ()
{
System.out.println(Class Z method);
}
Public static void main (String args[])
{
Z obj=newZ();
Obj.methodX();
Obj.methodY();
Obj.methodZ();
}
}
4. HIERARCHICAL INHERITANCEIn below kind of inheritance one class is inherited by many sub classes. In
below example class B,C and D inherits the same Class A. A is parent class of B,C
and D.
A

5. HYBRID INHERITANCE- In simple terms you can say that hybrid inheritance
is a combination of single & multiple inheritance. A hybrid inheritance can be
achieved in the java in a same way as a multiple inheritance can be. Using
interfaces. Yes it is right. By using interfaces you can have a multiple as well
as hybrid inheritance in Java.
A

2. Explain method overloading and overriding ?


Ans

All the methods should differ in either by number or type parameter.


Java supports to define two or more methods with same names within a class.
The method System.out.println() receives multiple parameters.
These methods are known as overloaded methods and process is called
method overloading.

USES

Method overloading when a couple of methods are needed with conceptually


similar functionality with different parameters.
Memory can be saved by implementing method overloading.

METHOD OVERRIDING

Method overriding is the process of writing functionality for methods with


same signature and return type, both in super class and sub class.
There will be reduced to time to invest method signature.
Function can be enhanced.
The behavior can be replaced in sub class.

3. Explain about Super,final,Abstract.


AnsSuper- This super keyboard in Java is a reference variable that is used to
refer immediate parent class object. Whenever you create the instance of
subclass, and instance of parent class is created implicitly.

Final: - The final keyword in java is used to restrict the user from inheriting.
It can be used in many contexts. It can be used for a variable, method or class.

If you mark any variable as final, you cant change the value of that variable.
If you mark any method as final, you cannot override it.
If you make any class as final, you cannot extend it.

4. What is Exception? Explain Exception Handling in JAVA.

Ans:Exception:- In java, exception is an event that disrupts the normal flow


of the program. It is an object which is thrown at runtime.
Exception handling : ClassNotFound, IO etc.

It is a mechanism to handle runtime errors such as

Advantages: The core advantages of exception handling is to maintain the normal flow of
the application. Exception normally disrupts the normal flow of the application.
That is why we use exception handling.
Keywords:

Try
Catch
Finally
Throw
Throws

5. Explain Package, Interface.


Ans. Package: - It is a group of similar type of classes, interfaces and sub
packages
These are 2 forms.

Built in package
User defined package

Advantages: o
o
o

It provides access protection.


It removes naming collision
It is used to categorize the classes and interfaces so that they can be
easily maintained.

Interfaces: An interface is a blueprint of a class. It has static constants and


abstract methods only. It cannot be instantiated just like abstract class.
Advantages: o
o

It is used to achieve full abstraction


It can be used to achieve loose coupling.

You might also like