Java Programming7
Java Programming7
class B extends A {
void meth() { // ERROR! Can't
override. System.out.println("Illegal!");
}
}
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.
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.
Output:
k: 3
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
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.
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();
}
}
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
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());
}
}
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: