Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

JAVA-Module-3-Chapter1 JITD

Uploaded by

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

JAVA-Module-3-Chapter1 JITD

Uploaded by

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

Object Oriented Programming with JAVA BCS306A

MODULE-III
CHAPTER 1
Inheritance
 Inheritance is one of the cornerstones of object-oriented programming because it allows the
creation of hierarchical classifications.
 Using inheritance, you can create a general class that defines traits common to a set of related
items.
 This class can then be inherited by other, more specific classes, each adding those things that are
unique to it.
 It is the mechanism in java by which one class is allow to inherit the features (instance
variables and methods) of another class.

2.1 Inheritance Basics


2.1.1 Super Class and Sub Class
 Super Class: The class whose features are inherited is known as super class(or a base class or a
parent class).
 Sub Class: The class that inherits the other class is known as sub class(or a derived class,
extended class, or child class). It inherits all of the instance variables and methods defined by the
superclass and adds its own, unique elements. The subclass can add its own instance variables
and methods in addition to the superclass instance variables and methods.

2.1.2 Advantages
1. Code Reusability: Inheritance in Java allows for code reuse, saving you ample time and effort
during software development. Sub-class can reuse some of the needed code of the superclass while
inheriting methods and properties.

2. Facilitates Polymorphism: Polymorphism is the ability of objects to take on multiple forms.


Child classes can override the methods of the parent class to change their behaviour in various ways.

3. Class Hierarchy: In Java, inheritance enables you to create a class hierarchy, which is useful for
modelling real-world objects and establishing their relationships.

4. Code Maintainability: With Java inheritance, it is easier to maintain code as developers need to
make changes only in the single location inherited by the child class.

5. Code Modularity: Inheritance divides code into parent and child classes, which helps to write
more organised and modular code.

6. Abstraction: Inheritance in Java enables you to create abstract classes defining a common
interface for a group of related classes. Hence, promoting abstraction and encapsulation, which
makes it easier to maintain and extend code.

7. Code Flexibility: Sub-class can add their methods and fields along with the features inherited
from the superclass, which allows code flexibility.

2.1.3 Types of Inheritance in Java


 There are three different types of inheritance supported by Java.
i. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass. In
image below, the class A serves as a base class for the derived class B.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 1


Object Oriented Programming with JAVA BCS306A

ii. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class
and as well as the derived class also act as the base class to other class. In below image, the class A
serves as a base class for the derived class B, which in turn serves as a base class for the derived
class C.

iii. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base
class) for more than one sub class. In below image, the class A serves as a base class for the derived
class B,C and D.

 Multiple Inheritance and Hybrid inheritance: Please note that Java does not support multiple
inheritance and hybrid inheritance with classes. In java, we can achieve these inheritances only
through Interfaces.

2.1.4 Extends Keyword


 To inherit a class, you simply incorporate the definition of one class into another by using the
extends keyword.
 The general form of a class declaration that inherits a superclass is shown here:
class subclass-name extends superclass-name
{
// body of sub class
}
 The following program creates a superclass called A and a subclass called B. Notice how the
keyword extends is used to create a subclass of A.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 2


Object Oriented Programming with JAVA BCS306A
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}

// Create a subclass by extending class A.


class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}

class SimpleInheritance {
public static void main(String args[ ]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb: "); Output:
superOb.showij(); Contents of superOb:
subOb.i = 7; i and j: 10 20
subOb.j = 8; Contents of subOb:
subOb.k = 9; i and j: 7 8 k: 9
System.out.println("Contents of subOb: "); Sum of i, j and k in subOb:
subOb.showij(); i+j+k: 24
subOb.showk();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}
 As you can see, the subclass B includes all of the members of its superclass, A.
 This is why subOb can access i and j and call showij( ).
 Also, inside sum( ), i and j can be referred to directly, as if they were part of B.
 Even though A is a superclass for B, it is also a completely independent, stand-alone class.

2.1.5 Member Access and Inheritance


 Although a subclass includes all of the members of its superclass, it cannot access those
members of the superclass that have been declared as private.
 A class member that has been declared as private will remain private to its class. It is not
accessible by any code outside its class, including subclasses.
 For example, consider the following simple class hierarchy: This program will not compile
because the reference to j inside the sum( ) method of B causes an access violation. Since j is
declared as private, it is only accessible by other members of its own class. Subclasses have no
access to it.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 3


Object Oriented Programming with JAVA BCS306A

2.1.6 A More Practical Example


 Here, the final version of the Box class developed in the preceding chapter will be extended to
include a fourth component called weight.
 Thus, the new class will contain a box’s width, height, depth, and weight.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 4


Object Oriented Programming with JAVA BCS306A

 BoxWeight inherits all of the characteristics of Box and adds to them the weight component.
 It is not necessary for BoxWeight to re-create all of the features found in Box. It can simply
extend Box to meet its own purposes.
 A major advantage of inheritance is that once you have created a superclass that defines
the attributes common to a set of objects, it can be used to create any number of more
specific subclasses.
 Once you have created a superclass that defines the general aspects of an object, that superclass
can be inherited to form specialized classes. Each subclass simply adds its own unique attributes.
This is the essence of inheritance.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 5


Object Oriented Programming with JAVA BCS306A

2.2 A Superclass Variable can Reference a Subclass Object


 A reference variable of a superclass can be assigned a reference to any subclass derived
from that superclass.
 For example, consider the following:

 Here, weightbox is a reference to BoxWeight objects, and plainbox is a reference to Box objects.
 Since BoxWeight is a subclass of Box, it is permissible to assign plainbox a reference to the
weightbox object.
 It is important to understand that it is the type of the reference variable—not the type of the
object that it refers to—that determines what members can be accessed.
 That is, when a reference to a subclass object is assigned to a superclass reference variable, you
will have access only to those parts of the object defined by the superclass.
 This is why plainbox can’t access weight even when it refers to a BoxWeight object.
 If you think about it, this makes sense, because the superclass has no knowledge of what a
subclass adds to it.
 This is why the last line of code in the preceding fragment is commented out.
 It is not possible for a Box reference to access the weight field, because Box does not define one.

2.3 Using super


 In the preceding examples, classes derived from Box were not implemented as efficiently or as
robustly as they could have been.
 For example, the constructor for BoxWeight explicitly initializes the width, height, and depth
fields of Box( ).
 Not only does this duplicate code found in its superclass, which is inefficient, but it implies that
a subclass must be granted access to these members.
 However, there will be times when you will want to create a superclass that keeps the details of
its implementation to itself (that is, that keeps its data members private).
 In this case, there would be no way for a subclass to directly access or initialize these variables
on its own.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 6


Object Oriented Programming with JAVA BCS306A
 Since encapsulation is a primary attribute of OOP, it is not surprising that Java provides a
solution to this problem.
 Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
 super has two general forms.
 The first calls the superclass’ constructor.
 The second is used to access a member of the superclass that has been hidden by a
member of a subclass.

1. Using super to Call Superclass Constructors


 When a subclass calls super( ), it is calling the constructor of its immediate superclass.
 Thus, super( ) always refers to the superclass immediately above the calling class.
 A sub class can call a constructor defined by its super class by use of the following form of
super:
super(arg-list);
 Here, arg-list specifies any arguments needed by the constructor in the superclass.
 super( ) must always be the first statement executed inside a subclass’ constructor.

 Here, BoxWeight( ) calls super( ) with the arguments w, h, and d. This causes the Box( )
constructor to be called, which initializes width, height, and depth using these values.
 BoxWeight no longer initializes these values itself.
 It only needs to initialize the value unique to it: weight. This leaves Box free to make these
values private if desired.
 Since constructors can be overloaded, super( ) can be called using any form defined by the
superclass. The constructor executed will be the one that matches the arguments.
 For example, here is a complete implementation of BoxWeight that provides constructors for the
various ways that a box can be constructed.
 In each case, super( ) is called using the appropriate arguments.
 Notice that width, height, and depth have been made private within Box.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 7


Object Oriented Programming with JAVA BCS306A

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 8


Object Oriented Programming with JAVA BCS306A

 When a subclass calls super( ), it is calling the constructor of its immediate superclass.
 Thus, super( ) always refers to the superclass immediately above the calling class.
 This is true even in a multileveled hierarchy.
 Also, super( ) must always be the first statement executed inside a subclass constructor.

2. Super can be used to refer immediate parent class instance variables and methods
 Super acts somewhat like this, except that it always refers to the superclass of the sub class in
which it is used. This usage has the following general form:
super.member
 Here, member can be either a method or an instance variable.
 This form of super is most applicable to situations in which member names of a subclass hide
members by the same name in the superclass.
 Consider this simple class hierarchy:
// Using super to overcome name hiding.
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) { Output:
super.i = a; // i in A i in superclass: 1
i in subclass: 2
i = b; // i in B
}
void show() {
System.out.println("i in superclass: " + super.i);
System.out.println("i in subclass: " + i);
}
}

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 9


Object Oriented Programming with JAVA BCS306A
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
subOb.show();
}
}

2.4 Creating a Multilevel Hierarchy


 In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the
derived class also act as the base class to other class.
 As shown in the figure, the class A serves as a base class for the derived class B, which in turn
serves as a base class for the derived class C.
 Given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A.
 When this type of situation occurs, each subclass inherits all of the traits found in all of its
super classes. In this case, C inherits all aspects of B and A.

 Consider the following program. In this program, the subclass BoxWeight is used as a superclass
to create the subclass called Shipment.
 Shipment inherits all of the traits of BoxWeight and Box, and adds a field called cost, which
holds the cost of shipping such a parcel.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 10


Object Oriented Programming with JAVA BCS306A

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 11


Object Oriented Programming with JAVA BCS306A

 Because of inheritance, Shipment can make use of the previously defined classes of Box and
BoxWeight, adding only the extra information it needs for its own, specific application.
 This is part of the value of inheritance; it allows the reuse of code.
 This example illustrates one other important point: super( ) always refers to the constructor in
the closest superclass.
 The super( ) in Shipment calls the constructor in BoxWeight.
 The super( ) in BoxWeight calls the constructor in Box.
 In a class hierarchy, if a superclass constructor requires parameters, then all subclasses must pass
those parameters “up the line.” This is true whether or not a subclass needs parameters of its
own.

2.5 When Constructors are called in Hierarchy


 When a class hierarchy is created, in what order are the constructors for the classes that make up
the hierarchy called? For example, given a subclass called B and a super class called A, is A’s
constructor called before B’s, or vice versa?

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 12


Object Oriented Programming with JAVA BCS306A
 The answer is that in a class hierarchy, constructors are called in order of derivation, from
super class to subclass. Further, since super() must be the first statement executed in a
subclass’ constructor, this order is the same whether or not super() is used.
 If super() is not used, then the default or parameter less constructor of each superclass will
be executed. The following program illustrates when constructors are executed:
// Demonstrate when constructors are called.
class A { // Create a super class.
A() {
System.out.println("Inside A's constructor.");
}
}
class B extends A { // Create a subclass by extending class A.
B() {
System.out.println("Inside B's constructor.");
}
}
class C extends B { // Create another subclass by extending B.
C() {
System.out.println("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[ ]) {
C c = new C();
}
}

The output from this program is shown here:


Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
 As you can see, the constructors are called in order of derivation.
 If you think about it, it makes sense that constructors are executed in order of derivation.
 Because a superclass has no knowledge of any subclass, any initialization it needs to perform is
separate from and possibly prerequisite to any initialization performed by the subclass.
 Therefore, it must be executed first.

2.6 Method Overriding


 In a class hierarchy, when a method in a subclass has the same name and type signature as
a method in its superclass, then the method in the subclass is said to override the method in
the superclass.
 When an overridden method is called from within a subclass, it will always refer to the
version of that method defined by the subclass. The version of the method defined by the
superclass will be hidden.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 13


Object Oriented Programming with JAVA BCS306A

 When show( ) is invoked on an object of type B, the version of show( ) defined within B is used.
That is, the version of show( ) inside B overrides the version declared in A. If you wish to access
the superclass version of an overridden method, you can do so by using super.
 Method overriding occurs only when the names and the type signatures of the two methods are
identical. If they are not, then the two methods are simply overloaded. Example for overloading.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 14


Object Oriented Programming with JAVA BCS306A

 The version of show( ) in B takes a string parameter. This makes its type signature different from
the one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.
Instead, the version of show( ) in B simply overloads the version of show( ) in A.

Differences between Method Overloading and Method Overriding:


No. Method Overloading Method Overriding
1. In method overloading, methods In method overriding, methods must
must have the same name and have the same name and same type
different type signatures. signature.
2. Method overloading is a compile- Method overriding is a run-time
time polymorphism. polymorphism.
3. In method overloading, the return In method overriding, the return type
type can or cannot be the same, but must be the same or co-variant.
we just have to change the
parameter.
4. Method overloading is used to Method overriding is used to provide the
increase the readability of the specific implementation of the method
program. that is already provided by its super class.

5. Method overloading is performed Method overriding occurs in two classes


within class. that have inheritance relationship.

6. In case of method overloading, In case of method overriding, parameter


parameter must be different. must be same.

7. Static binding is being used for Dynamic binding is being used for
overloaded methods. overriding methods.

8. Poor Performance due to compile It gives better performance. The reason


time polymorphism. behind this is that the binding of
overridden methods is being done at
runtime.
9. Private and final methods can be Private and final methods can’t be
overloaded. overridden.

10. The argument list should be The argument list should be the same in
different while doing method method overriding.
overloading.

2.7 Dynamic Method Dispatch


 Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method
dispatch.
 Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 15


Object Oriented Programming with JAVA BCS306A
 Dynamic method dispatch is important because this is how Java implements run-time
polymorphism.
 Let’s begin by restating an important principle: a superclass reference variable can refer to a
subclass object.
 Java uses this fact to resolve calls to overridden methods at run time.
 Here is how. When an overridden method is called through a superclass reference, Java
determines which version of that method to execute based upon the type of the object being
referred to at the time the call occurs. Thus, this determination is made at run time.
 When different types of objects are referred to, different versions of an overridden method
will be called.
 In other words, it is 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.
 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.

 This program creates one superclass called A and two subclasses of it, called B and C.
 Subclasses B and C override callme( ) declared in A.
 Inside the main( ) method, objects of type A, B, and C are declared.
 Also, a reference of type A, called r, is declared.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 16


Object Oriented Programming with JAVA BCS306A
 The program then in turn assigns a reference to each type of object to r and uses that reference to
invoke callme( ).
 As the output shows, the version of callme( ) executed is determined by the type of object being
referred to at the time of the call.

2.7.1 Why Overridden Methods?


 Overridden methods allow Java to support run-time polymorphism.
 Polymorphism is essential to object-oriented programming for one reason: it allows a general
class to specify methods that will be common to all of its derivatives, while allowing
subclasses to define the specific implementation of some or all of those methods.
 Overridden methods are another way that Java implements the “one interface, multiple
methods” aspect of polymorphism.
 Part of the key to successfully applying polymorphism is understanding that the superclasses
and subclasses form a hierarchy which moves from lesser to greater specialization.
 Used correctly, the superclass provides all elements that a subclass can use directly.
 It also defines those methods that the derived class must implement on its own.
 This allows the subclass the flexibility to define its own methods, yet still enforces a consistent
interface.
 Thus, by combining inheritance with overridden methods, a superclass can define the general
form of the methods that will be used by all of its subclasses.
 Dynamic, run-time polymorphism is one of the most powerful mechanisms that object-oriented
design brings to bear on code reuse and robustness.
 The ability of existing code libraries to call methods on instances of new classes without
recompiling while maintaining a clean abstract interface is a profoundly powerful tool.

2.7.2 Applying Method Overriding


 The following program creates a superclass called Figure that stores the dimensions of a two-
dimensional object.
 It also defines a method called area( ) that computes the area of an object.
 The program derives two subclasses from Figure.
 The first is Rectangle and the second is Triangle.
 Each of these subclasses overrides area( ) so that it returns the area of a rectangle and a triangle,
respectively.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 17


Object Oriented Programming with JAVA BCS306A

2.8 Using Abstract Classes


 There are situations in which you will want to define a superclass that declares the structure
of a given abstraction without providing a complete implementation of every method.
 That is, sometimes you will want to create a superclass that only defines a generalized form
that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
 Such a class determines the nature of the methods that the subclasses must implement.
 One way this situation can occur is when a superclass is unable to create a meaningful
implementation for a method.
 Java’s solution to this problem is the abstract method.
 You can require that certain methods be overridden by subclasses by specifying the
abstract type modifier.
 These methods are sometimes referred to as subclasser responsibility because they have no
implementation specified in the superclass.
 Thus, a subclass must override them—it cannot simply use the version defined in the
superclass.
 To declare an abstract method, use this general form:

 As you can see, no method body is present.


 Any class that contains one or more abstract methods must also be declared abstract.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 18


Object Oriented Programming with JAVA BCS306A
 To declare a class abstract, you simply use the abstract keyword in front of the class keyword at
the beginning of the class declaration.
 There can be no objects of an abstract class.
 That is, an abstract class cannot be directly instantiated with the new operator.
 Such objects would be useless, because an abstract class is not fully defined.
 Also, you cannot declare abstract constructors, or abstract static methods.
 Any subclass of an abstract class must either implement all of the abstract methods in the
superclass, or be itself declared abstract.
 Here is a simple example of a class with an abstract method, followed by a class which
implements that method:

 Notice that no objects of class A are declared in the program.


 As mentioned, it is not possible to instantiate an abstract class.
 One other point: class A implements a concrete method called callmetoo( ). This is perfectly
acceptable.
 Abstract classes can include as much implementation as they see fit.
 Although abstract classes cannot be used to instantiate objects, they can be used to create object
references, because Java’s approach to run-time polymorphism is implemented through the use
of superclass references.
 Thus, it must be possible to create a reference to an abstract class so that it can be used to point
to a subclass object.
 You will see this feature put to use in the next example.
 Using an abstract class, you can improve the Figure class shown earlier.
 Since there is no meaningful concept of area for an undefined two-dimensional figure, the
following version of the program declares area( ) as abstract inside Figure.
 This, of course, means that all classes derived from Figure must override area( ).

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 19


Object Oriented Programming with JAVA BCS306A

 As the comment inside main( ) indicates, it is no longer possible to declare objects of type
Figure, since it is now abstract.
 And, all subclasses of Figure must override area( ).
 To prove this to yourself, try creating a subclass that does not override area( ).
 You will receive a compile-time error.
 Although it is not possible to create an object of type Figure, you can create a reference variable
of type Figure.
 The variable figref is declared as a reference to Figure, which means that it can be used to refer
to an object of any class derived from Figure.
 As explained, it is through superclass reference variables that overridden methods are resolved at
run time.

2.9 Local Variable Type Inference and Inheritance


 JDK 10 added local variable type inference to the Java language, which is supported by the
context-sensitive keyword var.
 A superclass reference can refer to a derived class object, and this feature is part of Java’s
support for polymorphism.
 When using local variable type inference, the inferred type of a variable is based on the declared
type of its initializer.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 20


Object Oriented Programming with JAVA BCS306A
 Therefore, if the initializer is of the superclass type, that will be the inferred type of the
variable.
 It does not matter if the actual object being referred to by the initializer is an instance of a
derived class.
 For example, consider this program:

 In the program, a hierarchy is created that consists of three classes, at the top of which is
MyClass.
 FirstDerivedClass is a subclass of MyClass, and SecondDerivedClass is a subclass of
FirstDerivedClass.
 The program then uses type inference to create three variables, called mc, mc2, and mc3 by
calling getObj( ).
 The getObj( ) method has a return type of MyClass (the superclass), but returns objects of type
MyClass, FirstDerivedClass, or SecondDerivedClass, depending on the argument that it is
passed.
 As the output shows, the inferred type is determined by the return type of getObj( ), not by the
actual type of the object obtained.
 Thus, all three variables will be of type MyClass.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 21


Object Oriented Programming with JAVA BCS306A

2.10 Using final with Inheritance


 The keyword final has three uses.
 First, it can be used to create the equivalent of a named constant. This use was described in
the preceding chapter.
 The other two uses of final apply to inheritance.

1. Using final to Prevent Overriding


 While method overriding is one of Java’s most powerful features, there will be times when you
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:

 Because meth( ) is declared as final, it cannot be overridden in B.


 If you attempt to do so, a compile-time error will result.
 Methods declared as final can sometimes provide a performance enhancement: The compiler is
free to inline calls to them because it “knows” they will not be overridden by a subclass. When a
small final method is called, often the Java compiler can copy the bytecode for the subroutine
directly inline with the compiled code of the calling method, thus eliminating the costly
overhead associated with a method call.
 Normally, Java resolves calls to methods dynamically, at run time. This is called late
binding.
 However, since final methods cannot be overridden, a call to one can be resolved at compile
time. This is called early binding.

2. 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:

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG 22


Object Oriented Programming with JAVA BCS306A

2.11 The Object Class


 There is one special class, Object, defined by Java.
 All other classes are subclasses of Object.
 That is, Object is a superclass of all other classes.
 This means that a reference variable of type Object can refer to an object of any other class.
 Also, since arrays are implemented as classes, a variable of type Object can also refer to any
array.
 Object defines the following methods, which means that they are available in every object.

 The methods getClass( ), notify( ), notifyAll( ), and wait( ) are declared as final.
 However, notice two methods now: equals( ) and toString( ).
 The equals( ) method compares the contents of two objects. It returns true if the objects are
equivalent, and false otherwise.
 The toString( ) method returns a string that contains a description of the object on which it is
called.

Dr. Azizkhan F Pathan, Prof. Manjula P, JIT DVG


n-gl.com
23

You might also like