0% found this document useful (0 votes)
9 views57 pages

Unit 2

This document covers key concepts in Java programming, including method overloading, inheritance, and interfaces. It explains various types of inheritance, the use of the super keyword, and the differences between method overloading and overriding. Additionally, it discusses static, nested, and inner classes, as well as the purpose and structure of interfaces in Java.

Uploaded by

anuja.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views57 pages

Unit 2

This document covers key concepts in Java programming, including method overloading, inheritance, and interfaces. It explains various types of inheritance, the use of the super keyword, and the differences between method overloading and overriding. Additionally, it discusses static, nested, and inner classes, as well as the purpose and structure of interfaces in Java.

Uploaded by

anuja.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Unit 2

Inheritance ,Packages and Interfaces


Topics Covered
• Overloading Methods
• Object as parameter
• Retuning Objects
• Static ,Nested and inner classes
• Inheritance:Basics –Types of Inheritance
• Super Keyword
• Method Overriding
Overloading Method
• If a class has multiple methods having same
name but different in parameters, it is known
as Method Overloading
Different ways to overload the method
• By changing number of arguments
• By changing the data type
1) Method Overloading: changing no. of arguments

Output
sum of a and b is 5
sum of a and b is7
2) Method Overloading: changing data type
of arguments
Object as Parameters
Output
Sum of a and b :13
Returning Objects:
A method can return any type of data, including class types that you create.

The output generated by this


program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 2
Static,Nested and Inner Class:

• We can declare a class static using the static


keyword .
• A class be declared static only if it is a nested
class.
• The property of static class is that it does not
allow us to access non-static member of the
outer class.
Inner Class
• The classes that are non-static and nested are
called Inner Class.
• Note:
• We cannot create an instance of inner class
without creating an instance of outer class.
Nested Class
• Java allow us to define a class within a class
known as Nested Class.
• It may be static or non-static.
• The major difference between static and non-
static class
• 1.The static and non-static members of an
outer class can be accessed by an inner class.
• 2.The static members of the outer class can be
accessed only by the static class.
Inheritance:

• Definition1:
• Inheritance in Java is a mechanism in which one
object acquires all the properties and behaviors
of a parent object. It is an important part of
OOPs (Object Oriented programming system).
• Definition2:
• The process of deriving new from existing class
by sharing the properties of existing class is
known as inheritance.
• The new class that is created is known
as subclass (child or derived class) and the
existing class from where the child class is
derived is known as superclass (parent or base
class).
Why use inheritance in java
• For Code Reusability
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
Member Access and Inheritance

• Although a subclass includes all of the


members of its superclass, it cannot access
those members of the superclass that have
been declared as private.
Types of Inheritance’

• 1.Single
• 2.Multiple
• 3.Multilevel
• 4.hierarchial
• 5.hybrid
Single Inheritance
Multiple Inheritance

• Deriving a new class from more than one base


class
Interface:

• An interface in Java is a blueprint of a class. It has


static constants and abstract methods.
• The interface in Java is a mechanism to achieve
abstraction. There can be only abstract methods in
the Java interface, not method body. It is used to
achieve abstraction and multiple
inheritance in Java.
• In other words, you can say that interfaces can
have abstract methods and variables. It cannot
have a method body.
Syntax:
interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
}
Output:
Total Distance Travelled : 750
Average Speed maintained : 1
Multi-level Inheritance
Output
Inside display
Inside area
Inside volume
Example 2
Hierarchial Inheritance
Output
Class A
Class B
Class A
Class C
Class A
Class D
Super Keyword in Java

• 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

• super can be used to refer immediate parent


class instance variable.
• super can be used to invoke immediate
parent class method.
• super() can be used to invoke immediate
parent class constructor.
super is used to refer immediate parent class instance variable.

• We can use super keyword to access the data


member or field of parent class. It is used if
parent class and child class have same fields.
2) super can be used to invoke parent class method

• The super keyword can also be used to invoke


parent class method. It should be used if
subclass contains the same method as parent
class. In other words, it is used if method is
overridden.
3) super is used to invoke parent class constructor.

• The super keyword can also be used to invoke


the parent class constructor. Let's see a simple
example:
Output:
animal is created
dog is created
super example: real use
Output:
1 ankit 45000
Method Overriding in Java

• If subclass (child class) has the same method


as declared in the parent class, it is known
as method overriding in Java.
• In other words, If a subclass provides the
specific implementation of the method that
has been declared by one of its parent class, it
is known as method overriding.
Usage of Java Method Overriding

• Method overriding is used to provide the


specific implementation of a method which is
already provided by its superclass.
• Method overriding is used for runtime
polymorphism
Rules for Java Method Overriding

• The method must have the same name as in


the parent class
• The method must have the same parameter
as in the parent class.
• There must be an IS-A relationship
(inheritance).
A real example of Java Method Overriding
Difference between Method Overloading and
Method Overriding
• Can we override static method?
No, a static method cannot be overridden. It
can be proved by runtime polymorphism, so
we will learn it later.
Why can we not override static method?
It is because the static method is bound with
class whereas instance method is bound with
an object. Static belongs to the class area, and
an instance belongs to the heap area.
Can we override java main method?
No, because the main is a static method.

You might also like