Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 - 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Class: Employee

empid,empname

Class: work_profile
dept,job

Class: salary_details
basic_salary
Final variables and Methods
• All methods and variables can be overridden by
default in subclasses.
• To prevent the subclasses from overriding the
members of the superclass, declare them as final
using final keyword.
final int SIZE=10;
final void showstatus(..) {…}
• Defining method final ensures that functionality of
defined method will not be altered.
• Similarly the value of final variable never be
changed.
Final Classes
• A class that can not be sub-classed is called final class.

final class Aclass


{

}

final class Bclass extends Someclass


{

}
State the use of final keyword with respect to inheritance.
• Final keyword final has three uses.
1. First, it can be used to create the equivalent
of a named constant.
2. Using final to Prevent Overriding while
method overriding is one of Java’s most
powerful features,
– To disallow a method from being overridden,
specify final as a modifier at the start of its
declaration. Methods declared as final cannot be
overridden.
3. A class that can not be sub-classed is called
final class.
Finalizer Methods
• Constructors are used to initialize an object when it is
declared. This process is known as “Initialization”.
• Similarly, java supports a concept called “finalization”.
• Java run time is automatic garbage collecting system.
It automatically frees up the memory resources used
by the objects.
• But objects may hold other non-object resources.
• To free these resources we must use a finalizer
method
finalize( )
Abstract Methods and Classes
• A class declared as abstract is known as abstract class.
• It needs to be extended and its method implemented.
• It cannot be instantiated.
Syntax to declare the abstract class
abstract class <class_name>{ }
Abstract Method
• A method that is declared as abstract and does not have
implementation is known as abstract method.
Syntax to define the abstract method

abstract return_type <method_name>();


//no braces{}
Example
abstract class Bike{
abstract void run( );
}
class Honda extends Bike
{
void run()
{ System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda();
obj.run();
}
}
//example of abstract class having constructor, field and method
abstract class Bike
{ int limit=30;
Bike( )
{ System.out.println("constructor is invoked"); }
void getDetails() {
System.out.println("it has two wheels"); }
abstract void run();
}
class Honda extends Bike{
void run(){ System.out.println("running safely.."); }
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.getDetails();
System.out.println(obj.limit); } }
Dynamic method dispatch in Java
class A class C extends A class Dispatch
{ { {
void m1() public static void main(String
// overriding m1()
{ args[])
System.out.println("Inside A's void m1() {
m1 method"); { // object of type A
} System.out.println("Inside A a = new A();
} C's m1 method"); // object of type B
class B extends A B b = new B();
} // object of type C
{
} C c = new C();
// overriding m1()
// obtain a reference of type A
void m1()
A ref;
{
// ref refers to an A object
System.out.println("Inside B's
ref = a;
m1 method");
// calling A's version of m1()
}
ref.m1();
}
// now ref refers to a B object
ref = b;
Dynamic method dispatch in Java
class Dispatch
{
public static void main(String args[])
{
A a = new A();
B b = new B();
C c = new C();
A ref;
ref = a;
ref.m1();
ref = b;
ref.m1();
ref = c;
ref.m1();
}
}
Dynamic method dispatch in Java
• Mechanism by which a call to an overridden method is
resolved at run time, rather than compile time.
• When an overridden method is called through a
superclass reference, Java determines which version
(superclass/subclasses) of that method is to be
executed based upon the type of the object being
referred to at the time the call occurs. Thus, this
determination is made at run time.
• At run-time, it depends on the type of the object being
referred to (not the type of the reference variable)
that determines which version of an overridden
method will be executed
Dynamic method dispatch in Java
• A superclass reference variable can refer to a subclass
object. This is also known as upcasting.
• Java uses this fact to resolve calls to overridden methods at
run time.
• Therefore, if a superclass contains a method that is
overridden by a subclass, then when different types of
objects are referred to through a superclass reference
variable, different versions of the method are executed.

You might also like