You are on page 1of 25

INFORMATION AND COMMUNICATIONS UNIVERSITY

DEPARTMENT OF ICT
Course Code: ICE0034 Course Name: Mobile Programming
UNIT II: Inner Classes String Class Inheritance Overriding methods Using super
Abstract class.
INNER CLASSES IN JAVA
Few more rules about Inner Classes:
1. Inner classes cannot have static members. only static final variables.
2. Interfaces are never inner.
3. Static classes are not inner classes.
4. Inner classes may inherit static members that are not compile-time constants even
though they may not declare them.
5. Nested classes that are not inner classes may declare static members freely, in
accordance with the usual rules of the Java programming language.
6. Member interfaces are always implicitly static so they are never considered to be inner
classes.
7. A statement or expression occurs in a static context if and only if the innermost
method, constructor, instance initializer, static initializer, field initializer, or explicit
constructor invocation statement enclosing the statement or expression is a static
method, a static initializer, the variable initializer of a static variable, or an explicit
constructor invocation statement.

A blank final field of a lexically enclosing class may not be assigned within an inner class.
For Example:
class HasStatic
{
static int j = 100;
}

class Outer
{
final int z=10;

class Inner extends HasStatic


{
static final int x = 3;
static int y = 4;
}

static class Inner2


{
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
public static int size=130;
}

interface InnerInteface
{
public static int size=100;
}
}

public class InnerClassDemo


{
public static void main(String[] args)
{
Outer outer=new Outer();

System.out.println(outer.new Inner().y);
System.out.println(outer.new Inner().x);
System.out.println(outer.new Inner().j);

System.out.println(Outer.Inner2.size);

System.out.println(Outer.InnerInteface.size);
}
}
Hence it gives compilation problems as y cannot be used in inner class "Inner".
Also note Method parameter names may not be redeclared as local variables of the method,
or as exception parameters of catch clauses in a try statement of the method or constructor.
However, a parameter of a method or constructor may be shadowed anywhere inside a class
declaration nested within that method or constructor. Such a nested class declaration could
declare either a local class or an anonymous class.

For Example:
public class MethodParameterExamples
{
public String s="bt";

public void m1(String s)


{
s=this.s;
s="uk";
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT

//abstract
class InnerClass extends MethodParameterExamples
{
String s="ros";

public void m1()


{
System.out.println(super.s=this.s);
}
}

InnerClass innerClass=new InnerClass();


innerClass.s=s;
innerClass.m1();
}

public static void main(String[] args)


{
MethodParameterExamples methodParameterExamples=new
MethodParameterExamples();
methodParameterExamples.m1("vij");
System.out.println(methodParameterExamples.s);
}
}

Hence Prints the output:


uk
bt

Now coming to Section Nested Inner Classes:


Consider the below program.
class WithDeepNesting
{
boolean toBe;

WithDeepNesting(boolean b) { toBe = b;}

class Nested
{
boolean theQuestion;
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT

class DeeplyNested
{
DeeplyNested(){
theQuestion = toBe || !toBe;
}
}
}

public static void main(String[] args)


{
WithDeepNesting withDeepNesting=new WithDeepNesting(true);
WithDeepNesting.Nested nested=withDeepNesting.new Nested();
nested.new DeeplyNested();
System.out.println(nested.theQuestion);
}
}
Please note that Inner classes whose declarations do not occur in a static context may freely
refer to the instance variables of their enclosing class. An instance variable is always defined
with respect to an instance. In the case of instance variables of an enclosing class, the instance
variable must be defined with respect to an enclosing instance of that class. So, for example,
the class Local above has an enclosing instance of class Outer

STRING FUNCTIONS
Java String is a set of characters such as the string "Hello". It is a combination of H, e, l, l, o.
All String literals are instances of this class and two string literals with the same contents
refer to the same String object. A string can store a single word or more.

The String class includes number of methods to edit the content of string. String is special-
cased when doing data serialization - rather than listing the fields of this class, a String object
is converted to a string literal in the object stream.

The Java String object contains a sequence of char values. A char represents a single
character, such as 'a', 'A'. A char value is written in Java code using single quotes (') as
shown here. Java supports the Unicode character set, so we are no limited to just roman A-Z
characters. The char data type is a simple primitive data type, this is very similar to int, so
use simple = to copy char values, and == and != to compare char values.

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Return Method Parameter Description
DataType List

int length() str Returns the length (character count) of this


string buffer
char charAt() int Extract a single character. char charAt(int
location_index index);
String substring() int start Returns a new string that contains a sub-
sequence of characters currently contained in
this StringBuffer The sub-string begins at the
specified index and extends to the end of the
StringBuffer

String substring() int start, Returns a new string that contains a sub-
int end sequence of characters currently contained in
this StringBuffer

Fixed String Java Methods


Return Method Parameter List Description
DataType

boolean endsWith() String Determines if the string ends


string_to_match with the string_to_match.

int indexOf() String sub_string Determines location of first


occurence of substring. You
get a-1 if it does not exist.

int lastIndexOf() String sub_string Determines location of last


occurrence of substring. You
get a-1 if it does not exist.

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
String replace() char originalChar, Replaces an existing
char character with another.
replacementChar

boolean startsWith() String, Determines if the string


string_to_match starts with the
string_to_match.

String concat() String str Concatenate the specified


string to the end of this
string

String toLowerCase() None Converts the string to


lowercase.
String toUpperCase() None Converts the string to
uppercase.
String trim() None Removes leading and trailing
spaces.
String compareTo() String, compares two strings
string_to_match lexicographically

String compareToIgnoreCase() String, compares two strings


string_to_match lexicographically, ignoring
case considerations

Variable String Buffer Methods


Return Method Parameter Description
DataType List
int capacity() . Returns the current capacity of the
string buffer
void setLength() int, Sets the length of this string buffer
newLength
StringBuffer append() String str Appends the string to this string buffer
StringBuffer delete() int start, Removes the characters in a sub-string
int end of this StringBuffer

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
StringBuffer deleteCharAt() int index Removes the character at the specified
position in this StringBuffer (shortening
the StringBuffer by one character)
StringBuffer insert() int offset, Inserts the string into this string buffer
String str
StringBuffer reverse() None The character sequence contained in
this string buffer is replaced by the
reverse of the sequence

For creating a String object you use the "new" operator:


String str = new String("Hello");

public static void main(String[] args)


{
/*1) Find the 5th caracter in the string " RUSTIN BOND"
2) Find the index of character "K"in string "KANSAS".
3) Convert the string "hello world"to uppercase.
4) Replace character 'h'with 'b'in the string"humble */

String no1 = "RUSTIN BOND";


String no2 = "KANSAS";
String no3 = "hello world";
String no4 = "humble";

//print out the results

//remember java indicies start at 0


System.out.println("\nCurrent String: " + no1);
System.out.println("The 5th character: " + no1.charAt(4));

//remember java indicies start at 0


System.out.println("\nCurrent String: " + no2);
System.out.println("The index of character k: " + (no2.indexOf("K") + 1) );
System.out.println("\nCurrent String: " + no3);
System.out.println("Convert string to uppercase: " + no3.toUpperCase());

System.out.println("\nCurrent String: " + no4);

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
System.out.println("Replace character h with b: " + no4.replace('h', 'b'));
}

INHERITANCE
Inheritance can be defined as the process where one object acquires the properties of another.
With the use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance the most commonly used key words would be extends and
implements. These words would determine whether one object IS-A type of another. By
using these keywords we can make one object acquire the properties of another object.

IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how the extends
keyword is used to achieve inheritance.
public class Animal
{
}

public class Mammal extends Animal


{
}

public class Reptile extends Animal


{
}

public class Dog extends Mammal


{
}
Now based on the above example, In Object Oriented terms following are true:
Animal is the superclass of Mammal class.
Animal is the superclass of Reptile class.
Mammal and Reptile are sub classes of Animal class.
Dog is the subclass of both Mammal and Animal classes.
Now if we consider the IS-A relationship we can say:
Mammal IS-A Animal
Reptile IS-A Animal
Dog IS-A Mammal
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Hence : Dog IS-A Animal as well
With use of the extends keyword the subclasses will be able to inherit all the properties of the
superclass except for the private properties of the superclass.
We can assure that Mammal is actually an Animal with the use of the instance operator.
Example:
public class Dog extends Mammal
{
public static void main(String args[])
{
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();

System.out.println(m instanceof Animal);


System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
This would produce following result:
true

true

true

Since we have a good understanding of the extends keyword let us look into how the
implements keyword is used to get the IS-A relationship.
The implements keyword is used by classes by inherit from interfaces. Interfaces can never
be extended.
Example:
public interface Animal {}
public class Mammal implements Animal
{
}
public class Dog extends Mammal
{
}

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT

The instanceof Keyword:


Let us use the instanceof operator to check determine whether Mammal is actually an
Animal, and dog is actually an Animal
interface Animal{}

class Mammal implements Animal{}

class Dog extends Mammal{


public static void main(String args[]){

Mammal m = new Mammal();


Dog d = new Dog();

System.out.println(m instanceof Animal);


System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
This would produce following result:
true

true

true

HAS-A relationship:
These relationships are mainly based on the usage. This determines whether a certain class
HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.
Lets us look into an example:
public class Vehicle{}

public class Speed{}

public class Van extends Vehicle

private Speed sp;

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
}

This shows that class Van HAS-A Speed. By having a separate class for Speed we do not have
to put the entire code that belongs to speed inside the Van class., which makes it possible to
reuse the Speed class in multiple applications.
In Object Oriented feature the users do not need to bother about which object is doing the
real work. To achieve this, the Van class hides the implementation details from the users of
the Van class. SO basically what happens is the users would ask the Van class to do a certain
action and the Vann class will either do the work by itself or ask another class to perform the
action.
A very important fact to remember is that Java only supports only single inheritance. This
means that a class cannot extend more than one class. Therefore following is illegal:
public class extends Animal, Mammal{}
Inheritance can be defined as the process where one object acquires the properties of another.
With the use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance the most commonly used key words would be extends and
implements. These words would determine whether one object IS-A type of another. By
using these keywords we can make one object acquire the properties of another object.

IS-A Relationship:
IS-A is a way of saying : This object is a type of that object. Let us see how the extends
keyword is used to achieve inheritance.
To know the concept of inheritance clearly you must have the idea of class and its features
like methods, data members, access controls, constructors, keywords this, super etc.
As the name suggests, inheritance means to take something that is already made. It is one of
the most important feature of Object Oriented Programming. It is the concept that is used for
reusability purpose. Inheritance is the mechanism through which we can derived classes
from other classes. The derived class is called as child class or the subclass or we can say the
extended class and the class from which we are deriving the subclass is called the base class
or the parent class. To derive a class in java the keyword extends is used. To clearly
understand the concept of inheritance you must go through the following example.
The concept of inheritance is used to make the things from general to more specific e.g. When
we hear the word vehicle then we got an image in our mind that it moves from one place to
another place it is used for traveling or carrying goods but the word vehicle does not specify
whether it is two or three or four wheeler because it is a general word. But the word car
makes a more specific image in mind than vehicle, that the car has four wheels . It concludes
from the example that car is a specific word and vehicle is the general word. If we think
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
technically to this example then vehicle is the super class (or base class or parent class) and
car is the subclass or child class because every car has the features of it's parent (in this case
vehicle) class.
The following kinds of inheritance are there in java.
Simple Inheritance
Multilevel Inheritance
Pictorial Representation of Simple and Multilevel Inheritance

Multiple Inheritance
Simple Inheritance
When a subclass is derived simply from it's parent class then this mechanism is known as
simple inheritance. In case of simple inheritance there is only a sub class and it's parent class.
It is also called single inheritance or one level inheritance.
eg.
class A
{
int x;
int y;
int get(int p, int q)
{
x=p; y=q; return(0);
}
void Show()
{
System.out.println(x);
}
}

class B extends A

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
{
public static void main(String args[])
{
A a = new A();
a.get(5,6);
a.Show();
}
void display()
{
System.out.println("B");
}
}

Multilevel Inheritance
It is the enhancement of the concept of inheritance. When a subclass is derived from a
derived class then this mechanism is known as the multilevel inheritance. The derived class
is called the subclass or child class for it's parent class and this parent class works as the child
class for it's just above ( parent ) class. Multilevel inheritance can go up to any number of
level.
e.g.
class A
{
int x;
int y;
int get(int p, int q)
{
x=p; y=q; return(0);
}
void Show()
{
System.out.println(x);
}
}
class B extends A
{
void Showb()
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
{
System.out.println("B");
}
}

class C extends B
{
void display()
{
System.out.println("C");
}
public static void main(String args[])
{
A a = new A();
a.get(5,6);
a.Show();
}
}

Multiple Inheritance: (Java does not support multiple Inheritance)


The mechanism of inheriting the features of more than one base class into a single class is
known as multiple inheritance. Java does not support multiple inheritance but the multiple
inheritance can be achieved by using the interface.
In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more
than one interfaces in a class.

super keyword
The super is java keyword. As the name suggest super is used to access the members of the
super class.It is used for two purposes in java.
The first use of keyword super is to access the hidden data variables of the super class
hidden by the sub class.
e.g. Suppose class A is the super class that has two instance variables as int a and float b.
class B is the subclass that also contains its own data members named a and b. then we can
access the super class (class A) variables a and b inside the subclass class B just by calling the
following command.
super.member;
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Here member can either be an instance variable or a method. This form of super most useful
to handle situations where the local members of a subclass hides the members of a super
class having the same name.

The following example clarify all the confusions.


class A
{
int a;
float b;
void Show()
{
System.out.println("b in super class: " + b);
}
}

class B extends A
{
int a;
float b;
B( int p, float q)
{
a = p;
super.b = q;
}
void Show()
{
super.Show();
System.out.println("b in super class: " + super.b);
System.out.println("a in sub class: " + a);
}

public static void main(String[] args)


{
B subobj = new B(1, 5);
subobj.Show();

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
}
}
Output:
C:\>java B
b in super class: 5.0
b in super class: 5.0
a in sub class: 1

Use of super to call super class constructor: The second use of the keyword super in java is
to call super class constructor in the subclass. This functionality can be achieved just by using
the following command.
super(param-list);
Here parameter list is the list of the parameter requires by the constructor in the super class.
super must be the first statement executed inside a super class constructor. If we want to call
the default constructor then we pass the empty parameter list. The following program
illustrates the use of the super keyword to call a super class constructor.
class A{
int a;
int b;
int c;
A(int p, int q, int r){
a=p;
b=q;
c=r;
}
}

class B extends A{
int d;
B(int l, int m, int n, int o){
super(l,m,n);
d=o;
}
void Show(){
System.out.println("a = " + a);
System.out.println("b = " + b);
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
System.out.println("c = " + c);
System.out.println("d = " + d);
}

public static void main(String args[]){


B b = new B(4,3,8,7);
b.Show();
}
}
Output:
C:\>java B
a=4
b=3
c=8
d=7

OVERRIDING
In the previous chapter we talked about super classes and sub classes. If a class inherits a
method from its super class, then there is a chance to override the method provided that it is
not marked final.
The benefit of overriding is: ability to define a behavior that's specific to the sub class type.
Which means a subclass can implement a parent calss method based on its requirement.
In object oriented terms, overriding means to override the functionality of any existing
method.

Example:
Let us look at an example.
class Animal
{
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal


{
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
public void move()
{
System.out.println("Dogs can walk and run");
}
}

public class TestDog


{
public static void main(String args[])
{
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class

b.move();//Runs the method in Dog class


}
}
This would produce following result
Animals can move
Dogs can walk and run

Rules for method overriding:


1. The argument list should be exactly the same as that of the overridden method.
2. The return type should be the same or a subtype of the return type declared in the
original overridden method in the super class.
3. The access level cannot be more restrictive than the overridden method's access level.
For example: if the super class method is declared public then the overridding method in
the sub class cannot be either private or public. However the access level can be less
restrictive than the overridden method's access level.
4. Instance methods can be overridden only if they are inherited by the subclass.
5. A method declared final cannot be overridden.
6. A method declared static cannot be overridden but can be re-declared.
7. If a method cannot be inherited then it cannot be overridden.
8. A subclass within the same package as the instance's superclass can override any
superclass method that is not declared private or final.

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
9. A subclass in a different package can only override the non-final methods declared
public or protected.
10. An overriding method can throw any uncheck exceptions, regardless of whether the
overridden method throws exceptions or not. However the overridden method should
not throw checked exceptions that are new or broader than the ones declared by the
overridden method. The overriding method can throw narrower or fewer exceptions
than the overridden method.
11. Constructors cannot be overridden.

Using the super keyword:


When invoking a superclass version of an overridden method the super keyword is used.
class Animal{
public void move(){
System.out.println("Animals can move");
}
}

class Dog extends Animal{


public void move(){
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}

public class TestDog{

public static void main(String args[]){

Animal b = new Dog(); // Animal reference but Dog object


b.move();//Runs the method in Dog class

}
}
This would produce following result:
Animals can move
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Dogs can walk and run

ABSTRACTION
Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that
cannot be instantiated. All other functionality of the class still exists, and its fields, methods,
and constructors are all accessed in the same manner. You just cannot create an instance of
the abstract class.
If a class is abstract and cannot be instantiated, the class does not have much use unless it is
subclassed. This is typically how abstract classes come about during the design phase. A
parent class contains the common functionality of a collection of child classes, but the parent
class itself is too abstract to be used on its own.

Abstract Class:
Use the abstract keyword to declare a class abstract. The keyword appears in the class
declaration somewhere before the class keyword.
/* File name : Employee.java */
public abstract class Employee
{
private String name;
private String address;
private int number;
public Employee(String name, String address, int number)
{
System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}
public double computePay()
{
System.out.println("Inside Employee computePay");
return 0.0;
}
public void mailCheck()
{
System.out.println("Mailing a check to " + this.name
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
+ " " + this.address);
}
public String toString()
{
return name + " " + address + " " + number;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public void setAddress(String newAddress)
{
address = newAddress;
}
public int getNumber()
{
return number;
}
}
Notice that nothing is different in this Employee class. The class is now abstract, but it still
has three fields, seven methods, and one constructor.
Now if you would try as follows:
/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{

/* Following is not allowed and would raise error */


Employee e = new Employee("George W.", "Houston, TX", 43);

System.out.println("\n Call mailCheck using


Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Employee reference--");
e.mailCheck();
}
}
When you would compile above class then you would get following error:
Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error1

Extending Abstract Class:


We can extend Employee class in normal way as follows:
/* File name : Salary.java */
public class Salary extends Employee
{
private double salary; //Annual salary
public Salary(String name, String address, int number, double
salary)
{
super(name, address, number);
setSalary(salary);
}
public void mailCheck()
{
System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}
public double getSalary()
{
return salary;
}
public void setSalary(double newSalary)
{
if(newSalary >= 0.0)
{
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
salary = newSalary;
}
}
public double computePay()
{
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Here we cannot instantiate a new Employee, but if we instantiate a new Salary object, the
Salary object will inherit the three fields and seven methods from Employee.
/* File name : AbstractDemo.java */
public class AbstractDemo
{
public static void main(String [] args)
{
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP",
3, 3600.00);
Salary e = new Salary("John Adams", "Boston, MA",
2, 2400.00);

System.out.println("Call mailCheck using


Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using
Employee reference--");
e.mailCheck();
}
}
This would produce following result:
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Prepared by: Mr. Thinakaran Anansingh


INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.

Abstract Methods:
If you want a class to contain a particular method but you want the actual implementation of
that method to be determined by child classes, you can declare the method in the parent class
as abstract.
The abstract keyword is also used to declare a method as abstract.An abstract methods
consist of a method signature, but no method body.
Abstract method would have no definition, and its signature is followed by a semicolon, not
curly braces as follows:
public abstract class Employee
{
private String name;
private String address;
private int number;

public abstract double computePay();

//Remainder of class definition


}
Declaring a method as abstract has two results:
The class must also be declared abstract. If a class contains an abstract method, the class must
be abstract as well.

Any child class must either override the abstract method or declare itself abstract.
A child class that inherits an abstract method must override it. If they do not, they must be
abstract,and any of their children must override it.
Eventually, a descendant class has to implement the abstract method; otherwise, you would
have a hierarchy of abstract classes that cannot be instantiated.
If Salary is extending Employee class then it is required to implement computePay() method
as follows:
/* File name : Salary.java */
public class Salary extends Employee
{
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
private double salary; //Annual salary

public double computePay()


{
System.out.println("Computing salary pay for " + getName());
return salary/52;
}
//Remainder of class definition
}

Prepared by: Mr. Thinakaran Anansingh

You might also like