0% found this document useful (0 votes)
29 views

Java Programming7

1) Final can be used to create constants, prevent inheritance of classes, and prevent overriding of methods. 2) Abstract classes contain abstract methods that subclasses must implement. Abstract methods have no body but subclasses must provide an implementation. 3) Dynamic method dispatch refers to calling the proper overridden method at runtime based on the actual object type, rather than the reference type. This allows for runtime polymorphism in Java.

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java Programming7

1) Final can be used to create constants, prevent inheritance of classes, and prevent overriding of methods. 2) Abstract classes contain abstract methods that subclasses must implement. Abstract methods have no body but subclasses must provide an implementation. 3) Dynamic method dispatch refers to calling the proper overridden method at runtime based on the actual object type, rather than the reference type. This allows for runtime polymorphism in Java.

Uploaded by

preetham a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Using Final keyword:

We can use final key word in three ways:


o Used to create equivalent of a named constant

Final datatype identifier = .............. ; o Used to prevent inheritance


Final class …………..
o Used to avoid overloading
Final return type ………….

Using final to Prevent Overriding:


While method overridingul features,is thereonewill ofbetimesJava’swhenyou most will want to prevent it from
occurring. 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.
The following fragment illustrates final:
class A {
final void meth() {
System.out.println("This is a final method.");
}
}

class B extends A {
void meth() { // ERROR! Can't
override. System.out.println("Illegal!");
}
}

Using final to Prevent Inheritance:

Sometimes you will want to prevent a class from being inherited. To do this, precede the class
declaration with final. Declaring a class as final implicitly declares all of its methods as final,
too. As you might expect, it is illegal to declare a class as both abstract and final since an
abstract class is incomplete by itself and relies upon its subclasses to provide complete
implementations.

Here is an example of a final class:


final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass
A // ...
}
As the comments imply, it is illegal for B to inherit A since A is declared as final.

Polymorphism Method overriding:

In a class hierarchy, when a method in a sub class has the same name and type signature
as a method in its super class, then the method in the sub class is said to be override the
method in the sub class.
When an overridden method is called from within a sub class, it will always refers to the
version of that method defined by the sub class.
The version of the method defined in the super class is hidden.
In this situation, first it checks the method is existed in super class are not. If it is existed
then it executes the version of sub class otherwise it gives no such method found
exception.

Note: Methods with different signatures overloading but not overriding.


// Method
overriding. class A {
int i, j;
A(int a, int b)
{ i = a;
j = b;
}
// display i and
j void show() {
System.out.println("i and j: " + i + " " + j);
}
}
class B extends A
{ int k;
B(int a, int b, int c)
{ super(a, b);
k = c;
}
// display k –this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}}

Output:
k: 3

Dynamic method dispatch

o It is a mechanism by which a call to an overridden method is resolved at run time rather


then compile time.
o It is important because this is how java implements runtime polymorphism.
o Before going to that we must know about super class reference sub class object.

// Dynamic Method Dispatch


class A {
void callme() {
System.out.println("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
System.out.println("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
System.out.println("Inside C's callme method");
}
}

class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A's version of callme
r = b; // r refers to a B object

r.callme(); // calls B's version of callme


r = c; // r refers to a C object r.callme();
// calls C's version of callme
}
}

Output:
Inside A’s callme method
Inside B’s callme method
Inside C’s callme method

Abstract class:

o An abstract method is a method that is declared with only its signatures with out
implementations.
o An abstract class is class that has at least one abstract method.

The syntax is:

Abstract class class-name


{
Variables Abstract
methods; Concrete
methods;
.
.
.}

o We can’t declare any abstract constructor. o Abstract class should not include any abstract static method.
o Abstract class can’t be directly instantiated with
o Any sub class of abstract class must be either implements all the abstract methods in the
super class or declared it self as abstract.
o Abstract modifiersubclassresponsibilitiesreferred .sebecauofasnoimplementation―
of methods. Thus, a sub class must overridden them.
// A Simple demonstration of
abstract. abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A
{ void callme() {
System.out.println("B's implementation of callme.");
}
}

class AbstractDemo {
public static void main(String args[]) {
B b = new B();
al
lme();

b.
callme
too();
}
}

// Using abstract methods and classes.


abstract class Figure {
double dim1;.

double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}

// area is now an abstract method


abstract double area();
}
class Rectangle extends Figure
{ Rectangle(double a, double b)
{ super(a, b);
}
// override area for
rectangle double area() {
System.out.println("Inside Area for
Rectangle."); return dim1 * dim2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for
Triangle."); return dim1 * dim2 / 2;
}
}

class AbstractAreas
{
public static void main(String args[])
{
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is
created figref = r;
System.out.println("Area is " +
figref.area()); figref = t;
System.out.println("Area is " + figref.area());
}
}

Packages and Interfaces : Defining, Creating and Accessing a Package, Understanding


CLASSPATH, importing packages, differences between classes and interfaces, defining an
interface, implementing interface, applying interfaces, variables in interface and extending
interfaces.
.Defining Package:

o Generally, any java source file contains any (or all) of the following internal parts: o
A single package statement ( optional)
o
o Any number of import statements ( optional)
o A single public class declaration (required)
o Any number of classes private to the package (optional)

o Packages and Interfaces are two of the basic components of a java program.
o Packages are collection of related classes.
o Packages are containers for classes that are used to keep the class name compartmentalized.
o Packages are stored in an hierarchical manner and are explicitly imported into new class
defination.
o Java packages are classified into two types:

You might also like