OBJECT CLASS, abstract class,final notes
OBJECT CLASS, abstract class,final notes
The Object class is the parent class of all the classes in java by default (directly or indi-
rectly). The java.lang.Object class is the root of the class hierarchy. Some of the Object
class are Boolean, Math, Number, String etc.
Object
System.out.println(“end”);
}
protected void finalize() // finalize() is called just once on an object
{
System.out.println(“finalize method called”);
}
}
Sample Output:
Test@2a139a55
Test@2a139a55
705927765
end
finalize method called
In the above program, the default toString() method for class Object returns a string con-
sisting of the name of the class Test of which the object is an instance, the at-sign character
`@’, and the unsigned hexadecimal representation of the hash code of the object.
ABSTRACT CLASSES AND METHODS
Abstract class
A class that is declared as abstract is known as abstract class. It can have abstract and
non-abstract methods (method with body). It needs to be extended and its method imple-
mented. It cannot be instantiated.
Syntax:
abstract class classname
{
}
Abstract method
A method that is declared as abstract and does not have implementation is known as ab-
stract method. The method body will be defined by its subclass.
Abstract method can never be final and static. Any class that extends an abstract class
must implement all the abstract methods declared by the super class.
Note:
A normal class (non-abstract class) cannot have abstract methods.
Syntax:
abstract returntype functionname (); //No definition
Syntax for abstract class and method:
modifier abstract class className
{
//declare fields
//declare methods
abstract dataType methodName();
}
modifier class childClass extends className
{
dataType methodName()
{
}
}
Why do we need an abstract class?
Consider a class Animal that has a method sound() and the subclasses of it like Dog
Lion, Horse Cat etc. Since the animal sound differs from one animal to another, there is no
point to implement this method in parent class. This is because every child class must
override this method to give its own implementation details, like Lion class will say
“Roar” in this method and Horse class will say “Neigh”.
So when we know that all the animal child classes will and should override this method,
then there is no point to implement this method in parent class. Thus, making this method
abstract would be the good choice. This makes this method abstract and all the subclasses
to implement this method. We need not give any implementation to this method in parent
class.
Since the Animal class has an abstract method, it must be declared as abstract.
Now each animal must have a sound, by making this method abstract we made it
compul- sory to the child class to give implementation details to this method. This way we
ensure that every animal has a sound.
Rules
1. Abstract classes are not Interfaces.
2. An abstract class may have concrete (complete) methods.
3. An abstract class may or may not have an abstract method. But if any class has one or
more abstract methods, it must be compulsorily labeled abstract.
4. Abstract classes can have Constructors, Member variables and Normal methods.
5. Abstract classes are never instantiated.
6. For design purpose, a class can be declared abstract even if it does not contain any
abstract methods.
7. Reference of an abstract class can point to objects of its sub-classes thereby achieving
run-time polymorphism Ex: Shape obj = new Rectangle();
8. A class derived from the abstract class must implement all those methods that are
declared as abstract in the parent class.
9. If a child does not implement all the abstract methods of abstract parent class, then
the child class must need to be declared abstract as well.
Example 1
//abstract parent class
abstract class Animal
{
//abstract method
public abstract void sound();
}
//Lion class extends Animal
class public class Lion extends
Animal
{
public void sound()
{
System.out.println(“Roars”);
}
public static void main(String args[])
{
Animal obj = new Lion();
obj.sound();
}
}
Output:
Roars
In the above code, Animal is an abstract class and Lion is a concrete class.
Example 2
abstract class Bank
{
abstract int getRateOfInterest();
}
class SBI extends Bank
{
int getRateOfInterest()
{
return 7;
}
}
class PNB extends Bank
{
int getRateOfInterest()
{
return 8;
}
}
public class TestBank
{
public static void main(String args[])
{
Bank b=new SBI();//if object is PNB, method of PNB will be invoked
int interest=b.getRateOfInterest();
System.out.println(“Rate of Interest is: “+interest+” %”);
b=new PNB();
System.out.println(“Rate of Interest is: “+b.getRateOfInterest()+” %”);
}
}
Inheritance and Interfaces 2.19
Output:
Rate of Interest is: 7 %
Rate of Interest is: 8 %
Abstract class with concrete (normal) method
Abstract classes can also have normal methods with definitions, along with abstract
methods.
Sample Code:
abstract class A
{
abstract void callme();
public void normal()
{
System.out.println(“this is a normal (concrete) method.”);
}
}
public class B extends A
{
void callme()
{
System.out.println(“this is an callme (abstract) method.”);
}
public static void main(String[] args)
{
B b = new B();
b.callme();
b.normal();
}
}
Output:
this is an callme (abstract) method.
this is a normal (concrete) method.
Observations about abstract classes in Java
1. An instance of an abstract class cannot be created; But, we can have
references of abstract class type though.
Sample Code:
abstract class Base
{
abstract void fun();
}
class Derived extends Base
{
void fun()
{
System.out.println(“Derived fun() called”);
}
}
public class Main
{
public static void main(String args[])
{
// Base b = new Base(); Will lead to error
// We can have references of Base type.
Base b = new Derived();
b.fun();
}
}
Output:
Derived fun() called
2. An abstract class can contain constructors in Java. And a constructor of
ab- stract class is called when an instance of a inherited class is created.
Sample Code:
abstract class Base
{
Base()
{
System.out.println(“Within Base Constructor”);
}
abstract void fun();
}
class Derived extends Base
{
Derived()
{
System.out.println(“Within Derived Constructor”);
}
void fun()
{
System.out.println(“ Within Derived fun()”);
}
}
public class Main
{
public static void main(String args[])
{
Derived d = new Derived();
}
}
Output:
Within Base Constructor
Within Derived
Constructor
3. We can have an abstract class without any abstract method. This allows us to
create classes that cannot be instantiated, but can only be inherited.
Sample Code:
abstract class Base
{
void fun()
{
System.out.println(“Within Base fun()”);
}
}
class Derived extends Base
{
}
public class Main
{
public static void main(String args[])
{
Derived d = new Derived();
d.fun();
}
}
Output:
Within Base fun()
4. Abstract classes can also have final methods (methods that cannot be
overridden).
Sample Code:
abstract class Base
{
final void fun()
{
System.out.println(“Within Derived fun()”);
}
}
class Derived extends Base
{
}
public class Main
{
public static void main(String args[])
{
Base b = new Derived();
b.fun();
}
}
Output:
Within Derived fun()
FINAL METHODS AND CLASSES
The final keyword in java is used to restrict the user. The java final keyword can be ap-
plied to:
variable
method
class
{ {
OR
//code //code
} }
Sample Code:
final class XYZ
{
}