Topics Covered
• OverloadingMethods
• Object as parameter
• Retuning Objects
• Static ,Nested and inner classes
• Inheritance:Basics –Types of Inheritance
• Super Keyword
• Method Overriding
3.
Overloading Method
• Ifa 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
4.
1) Method Overloading:changing no. of arguments
Output
sum of a and b is 5
sum of a and b is7
Returning Objects:
A methodcan 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
8.
Static,Nested and InnerClass:
• 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.
10.
Inner Class
• Theclasses 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.
13.
Nested Class
• Javaallow 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.
14.
Inheritance:
• Definition1:
• Inheritancein 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.
15.
• The newclass 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).
16.
Why use inheritancein java
• For Code Reusability
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
20.
Member Access andInheritance
• 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.
Interface:
• An interfacein 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.
Super Keyword inJava
• 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.
40.
Usage of Javasuper 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.
41.
super is usedto 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.
43.
2) super canbe 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.
45.
3) super isused to invoke parent class constructor.
• The super keyword can also be used to invoke
the parent class constructor. Let's see a simple
example:
Method Overriding inJava
• 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.
51.
Usage of JavaMethod 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
52.
Rules for JavaMethod 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).
• Can weoverride 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.