Professional Documents
Culture Documents
DEPARTMENT OF COMPUTATIONAL
STUDIES
UNIT-II
PROBLEM SOLVING USING JAVA
(A20CPT305)
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
Super Class/Parent Class: Super class is the class from where a subclass inherits
the features. It is also called a base class or a parent class.
The super keyword in Java is a reference variable which is used to refer immediate
parent class object.
Whenever you create the instance of subclass, an instance of parent class is
created implicitly which is referred by super reference variable.
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).
ABSTRACT CLASS
A class which is declared as abstract is known as an abstract class. It can have abstract
and non-abstract
methods. It needs to be extended and its method implemented. It cannot be
instantiated.
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
14
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the body of
the method.
DYNAMIC METHOD DISPATCH
Dynamic method dispatch or Runtime polymorphism in Java
Runtime Polymorphism in Java is achieved by Method overriding in which a child
class overrides a method in its parent. An overridden method is essentially hidden in
the parent class, and is not invoked unless the child class uses the super keyword
within the overriding method. This method call resolution happens at runtime and is
termed as Dynamic method dispatch mechanism.
OBJECT CLASS IN JAVA
1. toString()
2. hashCode()
3. equals(Object obj)
4. getClass()
5. initialize()
6. clone()
1. toString():
The toString() provides a String representation of an object and is used to convert an object to
String. The default toString() method for class Object returns a string consisting of the name of
the class of which the object is an instance, the at-sign character `@’, and the unsigned
hexadecimal representation of the hash code of the object.
2. hashCode():
For every object, JVM generates a unique number which is hashcode. It returns distinct integers
for distinct objects. A common misconception about this method is that the hashCode() method
returns the address of the object, which is not correct. It converts the internal address of the object
to an integer by using an algorithm.
3. equals(Object obj):
It compares the given object to “this” object (the object on which the method is called). It gives a
generic way to compare objects for equality. It is recommended to override the equals(Object obj)
method to get our own equality condition on Objects.
4. getClass():
It returns the class object of “this” object and is used to get the actual runtime class of the
object. It can also be used to get metadata of this class. The returned Class object is the object that
is locked by static synchronized methods of the represented class.
5. finalize():
This method is called just before an object is garbage collected. It is called
the Garbage Collector on an object when the garbage collector determines that there are no more
references to the object. We should override finalize() method to dispose of system resources,
perform clean-up activities and minimize memory leaks .
6. clone():
It returns a new object that is exactly the same as this object. For clone() method refer Clone().The
remaining three methods wait(), notify() notifyAll() are related to Concurrency. Refer to Inter-
thread Communication in Java for details
PACKAGES:
A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can
be categorized in two form, built-in package and user-defined package. There are many built-in
packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Defining a Package:
To create a package includes a package command as the first statement in a Java source file. Any classes
declared within that file will belong to the specified package. The package statement defines a name
space in which classes are stored. If package statement is omitted, the class names are put into the
default package,
which has no name.
Syntax:
package<fully qualified package name>;
package pkg;
Creating a Package
While creating a package, you should choose a name for the package and include a package
statement along with that name at the top of every source file that contains the classes, interfaces,
enumerations, and annotation types that you want to include in the package.
The package statement should be the first line in the source file. There can be only one package
statement in each source file, and it applies to all types in the file.
If a package statement is not used then the class, interfaces, enumerations, and annotation types will
be placed in the current default package.
To compile the Java programs with package statements, you have to use -d option as shown below.
javac -d Destination_folder file_name.java
Then a folder with the given package name is created in the specified destination, and the compiled
class files will be placed in that folder.
The import Keyword
If a class wants to use another class in the same package, the package name need not be used. Classes in the
same package find each other without any special syntax.
Example
Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can
then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss
class.
package payroll;
public class Boss {
public void payEmployee(Employee e) {
e.mailCheck();
}
}
What happens if the Employee class is not in the payroll package? The Boss class must then use one of the
following techniques for referring to a class in a different package.
The fully qualified name of the class can be used. For example −
payroll.Employee
The package can be imported using the import keyword and the wild card (*). For example −
import payroll.*;
The class itself can be imported using the import keyword. For example −
JAVA – INTERFACES