You are on page 1of 19

SCHOOL OF ARTS AND SCIENCE

DEPARTMENT OF COMPUTATIONAL
STUDIES
UNIT-II
PROBLEM SOLVING USING JAVA
(A20CPT305)

Dr. ANTONY PAUL RAJ.A M.Sc., M.Phil., B.Ed., Ph.D


Assistant Professor
Department of Computational Studies
School of Arts and Science, SMVEC
UNIT II
INHERITANCE, PACKAGES AND
INTERFACES

Inheritance: Basic concepts – Forms of inheritance – Super key


word – method overriding –
Abstract classes – Dynamic method dispatch – The Object class.
Packages: Defining –
Creating and Accessing –importing packages. Interfaces: Defining –
Implementing –
Applying – Variables and extending interfaces.
INHERITANCE:

o Inheritance can be defined as the procedure or mechanism of acquiring


all the properties and behaviors of one class to another, i.e., acquiring
the properties and behavior of child class from the parent class.
o When one object acquires all the properties and behaviours of another
object, it is known as inheritance.
o Inheritance represents the IS-A relationship, also known as parent-child
relationship.
Syntax:
class subClass extends superClass
{
//methods and fields
}
Terms used in Inheritance:

 Class: A class is a group of objects which have common properties. It is a template


or blueprint from which objects are created.

 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.

 Reusability: As the name specifies, reusability is a mechanism which facilitates you


to reuse the fields and methods of the existing class when you create a new class. It can
use the same fields and methods already defined in previous class.
SINGLE INHERITANCE
In single inheritance, a sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes it is also known as simple
inheritance.
MULTILEVEL INHERITANCE
In multi-level inheritance, a class is derived from a class which is also derived from
another class is called multi-level inheritance. In simple words, we can say that a class
that has more than one parent class is called multi-level inheritance.
HIERARCHICAL INHERITANCE
In Hierarchical Inheritance, one class is inherited by many sub classes
HYBRID INHERITANCE
Hybrid means consist of more than one. Hybrid inheritance is the combination of two
or more types of inheritance.
SUPER KEYWORD

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.

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.
METHOD OVERRIDING

1.Understanding the problem without method overriding


2.Can we override the static method
3.Method overloading vs. method overriding
If subclass (child class) has the same method as declared in the parent class,
it is known as method overriding in Java.

Rules for Java Method Overriding

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

Object class is present in java.lang package. Every class in Java is


directly or indirectly derived from the Object class. If a class does not
extend any other class then it is a direct child class of Object and if
extends another class then it is indirectly derived.

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

An interface is a reference type in Java. It is similar to class. It is a


collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface.

Along with abstract methods, an interface may also contain


constants, default methods, static methods, and nested types. Method
bodies exist only for default methods and static methods.

Writing an interface is similar to writing a class. But a class


describes the attributes and behaviors of an object. And an interface
contains behaviors that a class implements.
An interface is similar to a class in the following ways −

 An interface can contain any number of methods.


 An interface is written in a file with a .java extension, with the name of the interface matching the
name of the file.
 The byte code of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding bytecode file must be in a directory
structure that matches the package name.
However, an interface is different from a class in several ways, including −
28
 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can appear in an interface must be
declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.
Implementing Interfaces

When a class implements an interface, you can think of the


class as signing a contract, agreeing to perform the specific
behaviors of the interface. If a class does not perform all the
behaviors of the interface, the class must declare itself as
abstract.

A class uses the implements keyword to implement an interface.


The implements keyword appears in the class declaration
following the extends portion of the declaration.
Thank
you

You might also like