2.4 Advanced Class Features
2.4 Advanced Class Features
Session Objectives
Inheritance
Reflection API
Class Variable/Static Variables
They are allocated when the class is loaded. Remember, each time you call
the instance the new value of the variable is provided to you.
Class Variable/Static Variables
public class Employee {
static String companyName="GTT";
int empNo;
String empName;
public static void main(String[] args) {
System.out.println(sam.companyName);
System.out.println(tom.companyName);
System.out.println(Employee.companyName);
}
}
Class Variable/Static Variables
Employee.dispAddress();
}
}
Inheritance
The child class inherits the methods and data defined for the parent class
To tailor a derived class, the programmer can add new variables or methods, or
can modify the inherited ones
Software reuse is at the heart of inheritance
By using existing software components to create new ones, we capitalize on all
the effort that went into the design, implementation, and testing of the existing
software
Deriving Subclasses using extends
// class contents
}
public class Book {
protected int pages = 1500;
public String message() {
System.out.println(“Number of pages: ” + pages);
}
}
Constructors are not inherited, even though they have public visibility
Yet we often want to use the parent's constructor to set up the "parent's part"
of the object
The super reference can be used to refer to the parent class, and often is used
to invoke the parent's constructor
A child’s constructor is responsible for calling the parent’s constructor
The first line of a child’s constructor should use the super reference to call the
parent’s constructor
The super reference can also be used to reference other variables and
methods defined in the parent’s class
The super Reference
public class Book {
Book(int numPages) {
pages = numPages;
}
}
definitions = numDefinitions;
}
}
Multiple Inheritance
Java supports single inheritance, meaning that a derived class can have only
one parent class
Multiple inheritance allows a class to be derived from two or more classes,
inheriting the members of all parents
Collisions, such as the same variable name in two parents, have to be
resolved
Java does not support multiple inheritance
In most cases, the use of interfaces gives us aspects of multiple inheritance
without the overhead (will discuss later)
Overriding Methods
When a child class defines a method with the same name and signature as a
method in the parent class, we say that the child’s version overrides the
parent’s version in favor of its own.
Signature: method’s name along with number, type, and order of its
parameters
The new method must have the same signature as the parent's method, but
can have a different body
The type of the object executing the method determines which version of
the method is invoked
Overriding
The Object class contains a few useful methods, which are inherited by all
classes
For example, the toString method is defined in the Object class
Every time we have defined toString, we have actually been overriding an
existing definition
The toString method in the Object class is defined to return a string that
contains the name of the object’s class together along with some other
information
The Object Class
The equals method of the Object class returns true if two references are
aliases
The String class (as we've seen) defines the equals method to return true if
two String objects contain the same characters
Therefore the String class has overridden the equals method inherited from
Object in favor of its own version
Final Classes
Security:
One mechanism that hackers use to subvert systems is to create
subclasses of a class and then substitute their class for the original.
The subclass looks and feels like the original class but does vastly
different things possibly causing damage or getting into private
information.
To prevent this kind of subversion, you can declare your class to be
final and prevent any subclasses from being created
Design:
Another reason you may wish to declare a class as final are for object-
oriented design reasons.
You may think that your class is "perfect" or that, conceptually, your
class should have no subclasses.
Final Classes
To specify that your class is a final class, use the keyword final before the
class keyword in your class declaration.
You can use the final keyword in a method declaration to indicate to the
compiler that the method cannot be overridden by subclasses.
For example, instead of making your ChessAlgorithm class final, you might
just want to make the nextMove method final:
class ChessAlgorithm {
}
Final instance variables
Inner classes nest within other classes. A normal class is a direct member of
a package, a top-level class. Inner classes, which became available with Java
1.1, come in following flavors:
Like any other static method, a static member class has access to all static
methods of the parent, or top-level, class.
Unlike the static variety, the member class is instance specific and has
access to any and all methods and members, even the parent's this reference.
Use inner classes
Local classes are declared within a block of code and are visible only
within that block, just as any other method variable.
import java.lang.reflect.*;
public class UseReflect1 {
public static void main(String str[]){
Class cls = java.util.List.class;
Class[] intfs = cls.getInterfaces();
int len = intfs.length;
for (int i =0; i < len; i++)
{
System.out.println(intfs[i]);
}
}
}
Example-2
import java.lang.reflect.*;
public class UseReflect2{
public static void main(String[] args){
Class cls = java.lang.Integer.class;
String info;
info = cls.getName(); // It will show java.lang.Integer
System.out.println(info);
}
}
Example-3
import java.lang.reflect.*;
class Animal{
}
class Horse extends Animal{
}
public class checkSuper {
static void printName(Object objct){
Class temp=objct.getClass();
Class sup = temp.getSuperclass();
System.out.println(sup);
}