Java Inheritance and Its Types Explained
Java Inheritance and Its Types Explained
Dynamic method Dispatch– Abstract classes and methods- Final classes-Object Class –
Inner Class - Packages – Defining Packages – Finding Packages And CLASSPATH -Importing
Packages - Interfaces – Defining an Interface, Implementing Interface and Extending
Interfaces .
[Link]
● The process of deriving a new class from an old program is called inheritance.
● Old class of java is called as base class or super class or parent class and the new
class of java is called as subclass/derived class/child class.
● Inheritance represents the IS-A relationship which is also known as
a parent-child relationship.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality
Types of Inheritance:
[Link] a superclass:
The sub class (the class that is derived from another class) is called a derived class. The
class from which it's derived is called the base class or super class.
The following figure illustrates these two types of classes:
[Link] a subclass:
Subclass is a class which is formed newly
A
A
D
B C
B
(c )Multilevel (d)Multiple
inheritance inheritance
A A B
C
C
Single inheritance
The method of inheriting the properties from one super class to one sub class is called
single inheritance.
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void bark(){[Link]("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
[Link]();
}}
Output:
barking...
eating...
Example program2
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class Bedroom extends Room //derived class using base class named Room
{
int height;
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
// Main class
public classsingleinheritance
{
public static void main(String ars[])
int volume1=[Link]();
[Link]( Area1= +area1);
Output:
Area1=168
Volume1=1680
class A
{
int x;
int y;
x=p;
y=q;
return(0);
}
void Show()
{
[Link](x);
}
class B extends A
{
public static void main(String args[])
{
B a = new B();
[Link](5,6);
[Link]();
}
Output:
5
Multilevel Inheritance
A general necessity in object oriented programming is the use of a derived class as a super
class.
class A
{
………………
………………
}
……………….
class B extends A
{
}
class C extends B
{
}
Output:
weeping...
barking...
eating...
Example Program2:
class students //base class
{
private int sno;
private String sname;
public void setstud(int no,String name)
{
sno=no;
sname=name;
}
public void putstud()
{
[Link]( Student No: +sno);
[Link]( Student Name: +sname);
}
}
{
mark1=m1;
mark2=m2;
}
}
class finaltot extends marks // derived class
{
private int total;
total=mark1+mark2;
}
[Link](100, ABC );
[Link](78,89);
[Link]();
[Link]();
[Link]();
[Link]();
}
}
class Base
{
void bmsg()
{
[Link]( Welcome to base class );
}
}
{
[Link]( Derive1msg );
}
}
void derive2msg()
{
[Link]( Derive2msg );
}
}
class Multilevel
{
public static void main(String args[])
{
d2.derive1msg();
[Link]();
}
}
Output:
Derive2msg
Derive1msg
Welcome to base class
Hierarchical inheritance
Class A is a super class of both class B and class C i.e one super class has many sub
classes. Some features of one level are shared by many lower level cases
ACCOUNT
CURRENT
SAVINGS
FIXED DEPOSIT
MEDIUM LONG
SHORT
public class A
{
void DisplayA()
{
[Link]( I am in A );
}
}
{
void DisplayB()
{
[Link]( I am in B );
}
}
{
void DisplayC()
{
[Link]( I am in C );
}}
public class Mainclass
{
[Link]();
[Link]();
[Link]();
[Link]();
}
Output:
Calling for subclass C
I am in A
I am in C
I am in B
The private members of a class cannot be openly accessed external class. Only functions of
that class can access the private data fields directly. As discussed previously, however,
occasionally it may be essential for a subclass to access a private member of a base class. If
you make a private member public, then someone can access that member. So, if a member
of a base class wants to be (directly) accessed in a subclass and yet still stop its direct
access external class, you must declare that member as protected.
Following program illustrates how the functions of a subclass can directly access a
protected member of the base class
For example, let's consider a series of classes to describe two types of shapes: rectangles
and triangles. These two shapes have definite general properties height and a width (or
base).
This could be depicted in the world of classes with a class Shapes from which we can derive
the two other ones : Rectangle and Triangle
[Link] = height;
[Link] = width;
}
}
{
public double getArea()
{
return height * width; //accessing protected members
}
}
{
return height * width / 2; //accessing protected members
}
}
{
public static void main(String[] args)
{
//Create object of Rectangle.
[Link](5,10);
}
}
Output :
[Link] Constructor
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
The subclass constructor uses the keyword super to call up the constructor method of
the superclass. Keyword super is used subject to the subsequent conditions.
2. The call to super class constructor must show as the first statement inside
the subclass constructor.
[Link] parameters in the super class must equal to the order and type of the
instance variable declared in the base class
class Animal
{
Animal()
{
[Link]("animal is created");
}
}
class Dog extends Animal{
Dog()
{
super();
[Link]("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}
}
Output:
animal is created
dog is created
The super keyword in Java is a reference variable which is used to refer immediate parent
class object.
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
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 fields.
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
[Link](color);//prints color of Dog class
[Link]([Link]);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}}
Output
black
white
In the above example, Animal and Dog both classes have a common property color. If we
print color property, it will print the color of current class by default. To access the parent
property, we need to use super keyword.
The super keyword can also be used to invoke parent class method. It should be used if
subclass contains the same method as parent class. In other words, it is used if method is
overridden.
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void eat(){[Link]("eating bread...");}
void bark(){[Link]("barking...");}
void work(){
[Link]();
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}}
Output:
eating...
barking...
In the above example Animal and Dog both classes have eat() method if we call eat()
method from Dog class, it will call the eat() method of Dog class by default because priority
is given to local.
The super keyword can also be used to invoke the parent class constructor. Let's see a
simple example:
class Animal{
Animal(){[Link]("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
[Link]("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
Output:
animal is created
dog is created
Note: super() is added in each class constructor automatically by compiler if there is no
super() or this().
class Animal{
Animal(){[Link]("animal is created");}
}
class Dog extends Animal{
Dog(){
[Link]("dog is created");
}
}
class TestSuper4{
public static void main(String args[]){
Dog d=new Dog();
}}
Output:
animal is created
dog is created
Let's see the real use of super keyword. Here, Emp class inherits Person class so all the
properties of Person will be inherited to Emp by default. To initialize all the property, we
are using parent class constructor from child class. In such way, we are reusing the parent
class constructor.
class Person{
int id;
String name;
Person(int id,String name){
[Link]=id;
[Link]=name;
}
}
class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
[Link]=salary;
}
void display(){[Link](id+" "+name+" "+salary);
}
}
class TestSuper5{
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
[Link]();
}}
Output:
1 ankit 45000
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has
been declared by one of its parent class, it is known as method overriding.
Let's understand the problem that we may face in the program if we don't use method
overriding.
Vehicle is running
Problem is that I have to provide a specific implementation of run() method in subclass that
is why we use method overriding.
In this example, we have defined the run method in the subclass as defined in the parent
class but it has some specific implementation. The name and parameter of the method are
the same, and there is IS-A relationship between the classes, so there is method overriding.
Consider a scenario where Bank is a class that provides functionality to get the rate of
interest. However, the rate of interest varies according to banks. For example, SBI, ICICI and
AXIS banks could provide 8%, 7%, and 9% rate of interest.
Java method overriding is mostly used in Runtime Polymorphism which we will learn in
next pages.
//Java Program to demonstrate the real scenario of Java Method Overriding
//where three classes are overriding the method of a parent class.
//Creating a parent class.
class Bank{
int getRateOfInterest(){return 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
[Link]("SBI Rate of Interest: "+[Link]());
[Link]("ICICI Rate of Interest: "+[Link]());
[Link]("AXIS Rate of Interest: "+[Link]());
}
}
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
It is because the static method is bound with class whereas instance method is
bound with an object. Static belongs to the class area, and an instance belongs to the
heap area.
There are many differences between method overloading and method overriding in java. A
list of differences between method overloading and method overriding are given below:
4) Method overloading is the example of compile time Method overriding is the example
polymorphism. of run time polymorphism.
Dynamic method dispatch or run-time polymorphism is the mechanism through which the
correct version of an overridden method is called at runtime. When a subclass overrides a
method from its superclass, the overridden method in the subclass is executed when called
on an instance of the subclass, even if the reference to the object is of the superclass type.
In the previous example, when we call [Link]() in the loop, the appropriate
makeSound() method defined in either Dog or Cat is executed based on the actual type of
the object.
It is a powerful feature because it allows for flexibility in the way we write code. We can
write methods in the superclass that are common to all subclasses, and then have specific
behavior defined in each subclass.
Dynamic method dispatch is particularly useful in scenarios where we want to write code
that operates on a general type but can be specialized by subclasses. It promotes code
reusability and allows for cleaner and more modular code.
For example, if we were building a game with different types of characters (for example,
warriors, mages, archers), we could have a Character superclass with the attack() method.
Each specific character type (warrior, mage, archer) would then override the attack()
method with its own implementation. It allows us to write code that can handle any type of
character without knowing the specific details of how each one attacks.
Complete Java program that demonstrates dynamic method dispatch along with input and
output.
[Link]
class Animal {
void makeSound() {
[Link]("Generic Animal Sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
[Link]("Bark");
}
}
class Cat extends Animal {
@Override
void makeSound() {
[Link]("Meow");
}
}
public class DynamicMethod {
public static void main(String[] args) {
Animal[] animals = {new Dog(), new Cat()};
for (Animal animal : animals) {
[Link]();
}
}
}
Output:
Bark
Meow
In the Main class, we have created an array of Animal objects called animals and populate it
with an instance of Dog and an instance of Cat. We then iterate through the animals array
using a for-each loop and call the makeSound() method on each element. Due to dynamic
method dispatch, the appropriate version of makeSound() from either Dog or Cat will be
executed based on the actual type of the object.
Dynamic method dispatch is a powerful feature of Java that enables polymorphism and
promotes code reusability and modularity. By allowing objects to take on multiple forms,
Java provides a flexible and extensible platform for building complex applications.
Understanding dynamic method dispatch is essential for writing efficient and maintainable
code in Java, especially in scenarios where you want to work with objects at a higher level
of abstraction. It is a fundamental concept in object-oriented programming that every Java
developer should master.
Syntax
Mostly, we don't know about the implementation class (which is hidden to the end
user), and an object of the implementation class is provided by the factory method.
A factory method is a method that returns the instance of the class. We will learn
about the factory method later.
✔ class A is a base class consists of two methods namely fun1() and fun2(),
class B extends A
{
}
class C extends A
{
}
}
B b=new B();
C c=new C();
b.fun1();
b.fun2();
c.fun1();
c.fun2();
}
Output:
B:In fun1
A:In fun2
C:In fun1
A:In fun2
Example2:
{
void fun()
{
[Link]( Derived fun() called );
}
}
class Main()
{
public static void main(String args[])
{
Base b=new Derived();
[Link]();
}
}
Output:
If you make any variable as final, you cannot change the value of final variable. The
final variable is constant always.
For example:
final int a=10;
The final keyword can also be useful to the method. The method using final keyword cannot
be overridden.
Java program which makes use of the keyword final for declaring the method
class B{
final int i=90;//final variable
void run(){
i=400;
}
public static void main(String args[]){
B obj=new B();
[Link]();
}
}//end of class
Output:
Java program which makes use of the keyword final for declaring the method
class Test
{
final void fun() //final keyword used in method
{
}
class Test1 extends Test
{
final void fun()
{
[Link]( Hello,this function declared using final );
}
}
Output:
1 error
Example mentioned above, on execution shows the error. since fun method is declared with
the keyword final and it cannot be overridden in sub class.
Final Class
Example1:Final Class
{
void fun()
{
[Link]( This is the function of base class );
}
{
[Link]( This is the function of derived class );
}
}
Output:
1 error
Example2:Final Class
class point
{
intx,y;
}
{
int color;
}
final class Colored3dPoint extends ColoredPoint
{
int z;
}
Class FinalClassDemo
{
public static void main(String args[])
{
[Link]=1;
cObj.x=5;
cObj.y=8;
[Link]( x= +cObj,x);
[Link]( y= +cObj,y);
[Link]( z= +cObj,z);
}
}
Output:
x=5
y=8
z=10
Color=1
Object class is a special class in [Link] no inheritance is precise for the classes then all
those classes are derived class of the Object class. We can consider ,Object is a
superclass of all other classes by default. therefore
A reference variable of type Object can refer to any object of additional classes.
If we call up the toString method, by default then it gives a string which describes the
object. This returned string contains the character @ and object s memory address in
hexadecimal form.
We can identify with the idea of toString() method by using as it is and overriding it with
appropriate string.
Example:Illustration1
class A extends Object
{
}
class B extends A
{
}
class ObjectClassDemo
{
public static void main(String args[])
{
A obj=new A();
}
}
Output:
obj:A@3e25a5
[Link]():A@3e25a5
Example:Illustration
class A extends Object
{
public String toString()
//method is overriden
{
String str= Hello ;
return str;
}
}
class B extends A
{
}
class ObjectClassDemo
{
A obj=new A();
[Link]( Obj: +obj);
Output:
Obj:Hello
[Link]():Hello
Example2:
Import [Link].*;
class StringDemo
{
public static void main(String args[])
{
Point c=new Point(10,20); //Explicitly call toString() on object as part of string
concatenation
[Link](s);
}
Output:
C=[Link][x=10,y=20]
C=[Link][x=10,y=20]
[Link][x=10,y=20] testing
The method equals is helpful for comparing values given by two objects.
Example1:
class A extends Object
{
int a=10;
public Boolean equals(Object obj)
{
if(obj instanceof B)
{
return a==((B)obj).b;
}
else
return false;
}
}
class B extends A
{
int b=10;
}
class ObjectClassDemo1
{
public static void main(String args[])
{
A obj1=new A();
B obj2=new B();
Output:
Inner classes are the nested classes. We can simply represent that are defined inside the
other [Link] below syntax defining the inner class is
1. Nested classes represent a particular type of relationship that is it can access all the
members (data members and methods) of the outer class, including private.
2. Nested classes are used to develop more readable and maintainable code because it
logically group classes and interfaces in one place only.
3. Code Optimization: It requires less code to write.
There are two types of nested classes non-static and static nested classes. The non-static
nested classes are also known as inner classes.
Syntax:
[Link] classes
A class created within class and outside method. It is also known as a regular inner
class. It can be declared with access modifiers like public, default, private, and
protected.
Syntax:
class Outer{
//code
class Inner
{
//code
}
}
Example
we are creating a msg() method in the member inner class that is accessing the private data
member of the outer class.
[Link]
class TestMemberOuter1{
private int data=30;
class Inner{
void msg(){[Link]("data is "+data);}
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
[Link] in=[Link] Inner();
[Link]();
}
}
[Link] classes
Syntax:
[Link]
public class localInner1{
private int data=30;//instance variable
void display(){
class Local{
void msg(){[Link](data);}
}
[Link] classes
A class created for implementing an interface or extending class. The java compiler decides
its name
It should be used if you have to override a method of class or interface. Java Anonymous
inner class can be created in two ways:
[Link]
abstract class Person{
abstract void eat();
}
class TestAnonymousInner
{
public static void main(String args[])
{
Person p=new Person(){
void eat(){[Link]("nice fruits");}
};
[Link]();
}
}
Output:
nice fruits
✔ A class is created, but its name is decided by the compiler, which extends the Person
class and provides the implementation of the eat() method.
✔ An object of the Anonymous class is created that is referred to by 'p,' a reference
variable of Person type.
PACKAGES
Types:
1. Java APIpackages
2. User definedpackages
Naming Conventions
Can be named using standard naming rules
1. packages start with lowercaseletters.
2. class names start with uppercaseletters.
3. Methods start with lowercase
letters. Ex: double
y=[Link](x)
[Link] :package
Math: class name
sqrt: method name.
Benefits of Packages
● The classes enclosed in the packages of other programs can be simplyreused
● They give a way to “hide” classes thus preventing from new programs orpackages
● Also supply a way for separating “design” from“coding”
● Two different classes in 2 various packages can have similarname.
Java user defined package
Creating a user defined Package
● First declare the package name using the package keyword continued by a
package name
● This must be the initial statement in a java sourcefile.
● Then you can define a class just as we usually define aclass
package firstpackage;//package
declaration public class Firstclass//class
definition
{
Body
}
Creating our own package or user defined packages follows the following steps
1. Declare the package at the
starting of a file
packagepackagename;
2. Define the class that is to be place in the package & declare itpublic.
3. Create a subdirectory below the directory where the main source files arestored
4. Keep the listing as the [Link] file in the subdirectorycreated
5. Compile the [Link] generates class file in thesubdirectory.
● Java also provides the concept of packagehierarchy
● This is done by specifying many names in a package statement,separated bydots.
package [Link];
Accessing a Package
● In java programming package can be accessed either using a fully qualified class
name or using another shortcut method through the importstatement.
● The general form of importstatement
import package1[.package2][.package3].classname;
● The system must end with asemicolon(;)
● The import statement should become visible before any class definitions in a
source file.
` Ex:
Importing a particular class
import [Link];
● After defining, all the fields of the class Myclass can be straightly accessed using
the class name or its objects can be used directly without specifying the
packagename.
Example:
package
package1;
public class
ClassA
{
public void displayA()
{
[Link](“Class A”);
}
}
import
[Link];
class Test
{
public static void main(String args[])
{
ClassAobjectA=new
ClassA();
[Link]();
}
}
output :
ClassA
Example
package
package2; public
class ClassB
{
protected int m=10;
public void display()
{
[Link](“Class
B”);
[Link](“m=”+
m);
}
}
Example:
import
[Link];
import package2.*;
class Test2
{
public static void main(String args[])
{
ClassAobjA=new
ClassA();
ClassBobjB=new
ClassB();
[Link]();
[Link]();
}
}
}
Output:
Class A
Class B
M =10
Example
import [Link];
class ClassC extends
ClassB
{
int n=20;
void displayC()
{
[Link](“Class
C”);
[Link](“m=”
+m);
[Link](“n=”+
n);
}
}
class Test2
{
public static void main(String args[])
{
ClassCobjC=new
ClassC();
[Link]();
[Link]();
}
}
OUTPUT: Class B
M=10
Class C
m=10 n=20
2.5. INTERFACES
[Link] interfaces
It is a type of a class but cannot be instantiated the new operator. Like classes, interface will
have functions and variables but with a most important difference Interfaces can have only
abstract functions and final members. It won t be instantiated/implemented or extended.
This means that interfaces do not identify any code to execute these functions and data
members have only constants. Therefore, it is the duty of the class that implements an
interface to develop the code for implementation of such functions
Syntax:
interface interfacename
{
Variables declaration;
Methods declaration;
}
In other words, Interface fields are public, static and final by default, and the
methods are public and abstract.
returntype methodname(parameter_list)
Example1:
interface Item
{
static final int code=100;
}
Example2:
interface Area
{
void show();
}
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface.
[Link] interfaces
Syntax:
class classname implements interfacename
{
body of class
}
Example program1:
interface Area
{
final static float pi=3.14F;
return(x*y);
}
return(pi*x*x);
}
}
class interfacetest
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
area=rect;
}
}
Output:
Area of Rectangle:200
Area of Circle:314
class student
{
int rollno;
Rollno=no;
}
Void putno()
{
[Link]( Rollno: +rollno);
}
}
{
float mark1,mark2;
mark1=m1;
mark2=m2;
}
void putmarks()
{
[Link]( Mark1: +mark1);
[Link]( Mark2: +mark2);
}
}
interface sports
{
floatsportwt=6.0F;
voidputwt();
}
{
float total;
}
void display()
{
total=mark1+mark2;
putno();
putmarks();
putwt();
[Link]( Total Score: +total);
}
class Hybrid
{
[Link](50.0F,50.F);
[Link]();
}
}
Output:
Rollno:100
Mark1:50.0
Mark2:50.0
Sportswt:6.0
Total Score:100.0
Example2:
interface interface1
{
public void show_val();
class Base
{
int val;
public void set_val(int i)
{
val=i;
}
}
class B extends Base implements interface1
{
public void show_val()
{
[Link]( The value of b= +val*5);
}
}
class multipleinherit
{
obj_A.set_val(10);
obj_B.set_val(20);
obj_A.show_val();
obj_B.show_val();
}}
Output:
The value of a=10
The value of b=100
[Link] interfaces
The sub interface will take over all the data members of the base interface using
extends keyword
Syntax:
body of name2
}
2 MARK QUESTIONS AND ANSWERS
Inheritance can be defined as the process where one object acquires the properties of
another. With the use of inheritance the information is made manageable in a hierarchical
[Link] resulting classes are known as derived classes, subclasses, or child classes. Older
class is known as super class.
Java Abstract classes are used to declare common characteristics of subclasses. An abstract
class cannot be instantiated. It can only be used as a superclass for other classes that extend
the abstract class. Abstractclasses are declared with the abstract keyword. Abstract classes
are used to provide a template or design for concrete subclasses down the inheritance tree.
3. You can create an abstract class that contains only abstract methods. On the other
hand, you can create an interface that declares the same methods. So can you use
abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case the
interface is your only option
Polymorphism means when an entity behaves differently depending upon the context its
being used. Moreover In other words Polymorphism is the capability of an action or method
to do different things based on the object that it is acting upon. Means polymorphism
allows you define one interface and have multiple implementations. That being one of the
basic principles of object oriented programming.
A child class can override a method in its parent. An overridden method is essentially
hidden in the parent class, and is not invoked unless the child class uses the super keyword
within the overriding method.
The runtime system [JVM]during runtime determines the appropriate method call based on
the class of the object. This feature is called as Polymorphism. All the methods in java are
dynamically resolved. This cannot be determined by the Compiler.
8. What is an Abstract classes and Methods?
When an abstract class is subclassed, the subclass usually provides implementations for all
of the abstract methods in its parent class. However, if it does not, the subclass must also be
declared abstract.
First, you declare an abstract class, GraphicObject, to provide member variables and
methods that are wholly shared by all subclasses, such as the current position and the
moveTo method. GraphicObject also declares abstract methods for methods, such as draw
or resize, that need to be implemented by all subclasses but must be implemented in
different ways.
It was noted that a class that implements an interface must implement all of the interface's
methods. It is possible, however, to define a class that does not implement all of the
interface methods, provided that the class is declared to be abstract. For example, abstract
class X implements Y {
class XX extends X {
//implements the remaining method in Y
}
In this case, class X must be abstract because it does not fully implement Y, but class XX
does, in fact, implement Y.
The Object class defines the basic state and behavior that all objects must have, such as the
ability to compare oneself to another object, to convert to a string, to wait on a condition
variable, to notify other objects that a condition variable has changed, and to return the
object's class.
Reflection is commonly used by programs which require the ability to examine or modify
the runtime behavior of applications running in the Java virtual machine. This is a relatively
advanced feature and should be used only by developers who have a strong grasp of the
fundamentals of the language.
A class browser needs to be able to enumerate the members of classes. Visual development
environments can benefit from making use of type information available in reflection to aid
the developer in writing correct code.
Debuggers need to be able to examine private members on classes. Test harnesses can
make use of reflection to systematically call a discoverable set APIs defined on a class, to
insure a high level of code coverage in a test suite.
Performance Overhead
Security Restrictions
Exposure of Internals
Objects in Java are referred using reference types, and there is no direct way to copy the
contents of an object into a new object. The assignment of one reference to another merely
creates another reference to the same object. Therefore, a special clone() method exists for
all reference types in order to provide a standard mechanism for an object to make a copy
of itself. Here are the details you need to know about cloning Java objects.
Inner class methods can access the data from the scope in which they are defined
including data that would otherwise be private.
Inner classes can be hidden from other classes in the same package.
20.What are the properties of proxy class ?
• Proxy classes are created on the fly in the running program.
• Once they are created they are just like any other class in the V.M.
10. Develop a message abstract class which contains playMessage abstract method.
Write a different sub-classes like TextMessage, VoiceMessage and FaxMessage classes for to
implementing the playMessage method.
11. Develop a abstract Reservation class which has Reserve abstract method. Implement
the sub-classes like ReserveTrain and ReserveBus classes and implement the same.
13. Develop a Library interface which has drawbook(), returnbook() (with fine),
checkstatus() and reservebook() methods. All the methods tagged with public.
14. Develop an Employee class which implements the Comparable and Cloneable
interfaces. Implement the sorting of persons (based on name in alphabetical). Also
implement the shallow copy (for name and age) and deep copy (for DateOfJoining).
1. Develop a static Inner class called Pair which has MinMax method for finding min
and max values from the array.
[Link] the help of real time application explain object cloning in java.
[Link] some of the classes available under Lang package and develop your own applications..