Information and Communications University: Inner Classes in Java
Information and Communications University: Inner Classes in Java
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;
interface InnerInteface
{
public static int size=100;
}
}
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";
//abstract
class InnerClass extends MethodParameterExamples
{
String s="ros";
class Nested
{
boolean theQuestion;
Prepared by: Mr. Thinakaran Anansingh
INFORMATION AND COMMUNICATIONS UNIVERSITY
DEPARTMENT OF ICT
class DeeplyNested
{
DeeplyNested(){
theQuestion = toBe || !toBe;
}
}
}
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.
String substring() int start, Returns a new string that contains a sub-
int end sequence of characters currently contained in
this StringBuffer
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
{
}
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
{
}
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{}
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
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();
}
}
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.
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);
}
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);
}
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");
}
}
}
}
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)
{
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;
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