Multi-level Inheritance
Multi-level Inheritance
is a process in which we have more than 2 classes.
A is base to B.
B is child to A.
B is base to C.
C is child to B.
Order of execution of
classes:
1st = A
2nd = B
3rd = C
Multiple Inheritance
Multiple Inheritance
is a process in which we have multiple base classes and single child class. But
in Java, we can’t do multiple inheritance using classes.
D extends A.
D extends B.
D extends C.
Interface
Interfaces are
blocks in which we can declare methods but can’t provide the definition. All
the methods declared in interface need to be defined always. We need to
implement those methods in classes. By default, the methods in interfaces are public.
interface
interface_name
{
method declaration;
method declaration;
:
}
Inheritance format:
class
class_name implements interface_name
e.g.
showing the importance of public access in interface:
Without
using public (containing error):
Using
public (no error):
Reason:
By
default, the methods in interfaces
are public while they are default in class. So, we need to specify the access specifier as public.
e.g.
for Multiple inheritance:
Ques.
1. Assume
that a bank maintains 2 kinds of accounts for its customers. One is saving and the
other is current account. The saving account provides compound interest and
withdrawal facility but no checkbook facility. The current account provides checkbook
facility but no interest. The current account holder should also maintain a
min. balance and if the balance falls below the level, service charges are
imposed.
2. Create
a class “Account” that stores - customer name, account no., and type of
account. From this, derive the classes “current” and “saving” account to make
them more specific to their requirements. Include the necessary methods to
achieve the following task:
1. Accept
deposit from customer and update the balance.
2. Display
the balance.
3. Compute
and deposit interest.
4. Permit
withdrawal and update the balance.
5. Check
the min. balance, impose penalty if necessary and update the balance.
Do not use any constructor. Use methods
to initialize the class members.
Abstract Classes
Abstract class is a class in which we
have at least one abstract method. Abstract class always behaves as a base
class.
Format:
abstract class class_name
{
}
No comments:
Post a Comment