Você está na página 1de 46

Prof. S. A.

Koti 1
Inheritance in Java
1. Inheritance in Java is a mechanism in which one object
acquires all the properties and behaviours of a parent
object. It is an important part of OOPs (Object Oriented
programming system).
2. The idea behind inheritance in Java is that you can
create new classes that are built upon existing classes.
3. When you inherit from an existing class, you can reuse
methods and fields of the parent class.
4. Moreover, you can add new methods and fields in
your current class also.
5. Inheritance represents the IS-A relationship which is
also known as a parent-child relationship.
Why use inheritance in java ?
• For Method Overriding (so runtime polymorphism can be
achieved).
• For Code Reusability.

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: Superclass 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. You can use the
same fields and methods already defined in the previous class.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}

Prof. S. A. Koti 4
• As displayed in the figure, Programmer
is the Subclass and Employee is the
Superclass.
• The relationship between the two
classes is Programmer IS-A Employee. It
means that Programmer is a type of
Employee.

Prof. S. A. Koti 5
class Employee Example
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
OUTPUT............................
Programmer salary is:40000.0
Bonus of programmer is:10000

Prof. S. A. Koti 6
Types of inheritance in java

• On the basis of class, there can be three types


of inheritance in java: single, multilevel and
hierarchical.
• In java programming, multiple and hybrid
inheritance is supported through interface
only.

Prof. S. A. Koti 7
Prof. S. A. Koti 8
When one class inherits multiple classes, it is known as
multiple inheritance. For Example:

Prof. S. A. Koti 9
Q) Why multiple inheritance is not supported
in java?
• To reduce the complexity and simplify the language,
multiple inheritance is not supported in java.
• Consider a scenario where A, B, and C are three classes.
The C class inherits A and B classes. If A and B classes
have the same method and you call it from child class
object, there will be ambiguity to call the method of A or
B class.
• Since compile-time errors are better than runtime errors,
Java renders compile-time error if you inherit 2 classes.
So whether you have same method or different, there
will be compile time error.

Prof. S. A. Koti 10
class A class C extends A,B
{ {
void msg() public static void main(String args[])
{ {
System.out.println("Hello"); C obj=new C();
} obj.msg();
}
class B //Now which msg() method would
{ be invoked? }
void msg() }
{
System.out.println("Welcom
e");
}
Prof. S. A. Koti 11
}
super keyword in Java

• super variable refers immediate parent class


instance.
• super variable can invoke immediate parent class
method.
• super() acts as immediate parent class
constructor and should be first line in child class
constructor.
• When invoking a superclass version of an
overridden method the super keyword is
used.
Prof. S. A. Koti 12
Prof. S. A. Koti 13
Constructors in Java
• Constructors are used to initialize the object’s
state. Like methods, a constructor also contains
collection of statements(i.e. instructions) that
are executed at time of Object creation.
• When is a Constructor called ?
Each time an object is created using new()
keyword at least one constructor (it could be
default constructor) is invoked to assign initial
values to the data members of the same class.

Prof. S. A. Koti 14
Rules for writing Constructor:
• Constructor(s) of a class must has same name as
the class name in which it resides.
• A constructor in Java can not be abstract, final,
static and Synchronized.
• Access modifiers can be used in constructor
declaration to control its access i.e which other
class can call the constructor.
Constructor Overloading
• Like methods, we can overload constructors for
creating objects in different ways. Compiler
differentiates constructors on the basis of
numbers of parameters, types of the parameters
and order of the parameters.
Prof. S. A. Koti 15
• A derived Java class can call a constructor in
its base class using the super keyword. In fact,
a constructor in the derived class must call
the super's constructor unless default
constructors are in place for both classes. ...
Also note that the constructor requires two
parameters, the first name and the last name.

Prof. S. A. Koti 16
Constructors in Java

Prof. S. A. Koti 17
Method Overloading in Java

• Method Overloading is a feature that allows a


class to have more than one method having
the same name, if their argument lists are
different. It is similar to constructor
overloading in Java, that allows a class to have
more than one constructor having different
argument lists.

Prof. S. A. Koti 18
In order to overload a method, the argument lists of
the methods must differ in either of these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)

Prof. S. A. Koti 19
• Invalid case of method overloading:
When I say argument list, I am not talking
about return type of the method, for example
if two methods have same name, same
parameters and have different return type,
then this is not a valid method overloading
example. This will throw compilation error.
• int add(int, int)
• float add(int, int)

Prof. S. A. Koti 20
Prof. S. A. Koti 21
Runtime Polymorphism or Dynamic
method dispatch
• Dynamic method dispatch is a mechanism by which a
call to an overridden method is resolved at runtime.
• This is how java implements runtime polymorphism.
• When an overridden method is called by a reference,
java determines which version of that method to
execute based on the type of object it refer to.
• In simple words the type of object which it referred
determines which version of overridden method will
be called.

Prof. S. A. Koti 22
Prof. S. A. Koti 23
Difference between Static binding
and Dynamic binding in Java?
• Static binding in Java occurs during compile time
while dynamic binding occurs during runtime.
• Static binding uses type(Class) information for
binding while dynamic binding uses instance of
class(Object) to resolve calling of method at run-
time.
• Overloaded methods are bonded using static
binding while overridden methods are bonded
using dynamic binding at runtime.
Prof. S. A. Koti 24
Abstract Class in Java with example
• A class that is declared using “abstract” keyword
is known as abstract class.
• It can have abstract methods(methods without
body) as well as concrete methods (regular
methods with body).
• A normal class(non-abstract class) cannot have
abstract methods.
• An abstract class can not be instantiated, which
means you are not allowed to create an object of
it.
Prof. S. A. Koti 25
Prof. S. A. Koti 26
• Why can’t we create the object of an abstract
class?
• Because these classes are incomplete, they have
abstract methods that have no body so if java
allows you to create object of this class then if
someone calls the abstract method using that
object then What would happen?
• There would be no actual implementation of the
method to invoke.
Also because an object is concrete. An abstract
class is like a template, so you have to extend it
and build on it before you can use it.

Prof. S. A. Koti 27
Using final with inheritance
• final is a keyword in java used for restricting
some functionalities. We can declare
variables, methods and classes with final
keyword.

Prof. S. A. Koti 28
Prof. S. A. Koti 29
Prof. S. A. Koti 30
Packages In Java

Prof. S. A. Koti 31
Prof. S. A. Koti 32
Prof. S. A. Koti 33
• import keyword is used to import built-in and user-
defined packages into your java source file so that your
class can refer to a class that is in another package by
directly using its name.
There are 3 different ways to refer to any class that is
present in a different packag
1. Using fully qualified name (But this is not a good
practice.)
2. To import only the class/classes you want to
use(packagename.classname)
3. To import all the classes from a particular
package(packagename.*)

Prof. S. A. Koti 34
Prof. S. A. Koti 35
Prof. S. A. Koti 36
Interfaces in Java
• Interface is a pure abstract class.They are
syntactically similar to classes, but you cannot
create instance of an Interface and their methods
are declared without any body. Interface is used
to achieve complete abstraction in Java. When
you create an interface it defines what a class can
do without saying anything about how the class
will do it.
Syntax:
interface interface_name { }
Prof. S. A. Koti 37
Rules for using Interface
• Methods inside Interface must not be static, final,
native or strictfp.
• All variables declared inside interface are
implicitly public static final variables(constants).
• All methods declared inside Java Interfaces are
implicitly public and abstract, even if you don't
use public or abstract keyword.
• Interface can extend one or more other interface.
• Interface cannot implement a class.
• Interface can be nested inside another interface.

Prof. S. A. Koti 38
Prof. S. A. Koti 39
Prof. S. A. Koti 40
Prof. S. A. Koti 41
Prof. S. A. Koti 42
Default Methods In Interface

• Before Java 8, interfaces could have only abstract


methods.
• The implementation of these methods has to be
provided in a separate class.
• So, if a new method is to be added in an interface,
then its implementation code has to be provided in the
class implementing the same interface.
• To overcome this issue, Java 8 has introduced the
concept of default methods which allow the interfaces
to have methods with implementation without
affecting the classes that implement the interface.

Prof. S. A. Koti 43
Prof. S. A. Koti 44
Static method in Interface in Java
• Static Methods in Interface are those methods,
which are defined in the interface with the
keyword static.
• Unlike other methods in Interface, these static
methods contain the complete definition of the
function and since the definition is complete and
the method is static.
• Therefore these methods cannot be overridden
or changed in the implementation class.
Prof. S. A. Koti 45
Prof. S. A. Koti 46

Você também pode gostar