0% found this document useful (0 votes)
2 views

OOPS Unit2 Firsthalf Updated

The document covers key concepts in Java 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, along with examples and outputs for better understanding.

Uploaded by

anuja.it
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

OOPS Unit2 Firsthalf Updated

The document covers key concepts in Java 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, along with examples and outputs for better understanding.

Uploaded by

anuja.it
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

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
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.
Types of Nested Class
• Static Nested Classes
• Non-static Nested Classes (or Inner Classes)
We can further divide the Non-static Classes
into -
– Member Inner Classes
– Local Inner Classes
– Anonymous Inner Classes
Static Nested 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.
Non-static Nested Classes
Member Inner class

• A non-static class that is created inside a class


but outside a method is called member inner
class. It is also known as a regular inner class
• Syntax:
class Outer{
//code
class Inner{
//code
}
}
Member Inner Class Example
Output:
data is 30
Anonymous inner class
• Java anonymous inner class is an inner class
without a name and for which only a single
object is created.
• In simple words, a class that has no name is
known as an anonymous inner class in Java.
anonymous inner class example using class

Output:
nice fruits
Local inner class

• A class i.e., created inside a method, is called


local inner class in java. Local Inner Classes are
the inner classes that are defined inside a
block. Generally, this block is a method body.
local inner class example

Output:
30
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