Object Oriented Programming Object Oriented Programming: Lecture-17 Instructor Name
Object Oriented Programming Object Oriented Programming: Lecture-17 Instructor Name
Instructor Name:
Lecture-17
Today’s Lecture
Polymorphism
2
Polymorphism
What is Polymorphism?
Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when a parent class
reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be
polymorphic.
In Java, all Java objects are polymorphic since any object will pass the IS-A
test for their own type and for the class Object.
It is important to know that the only possible way to access an object is
through a reference variable. A reference variable can be of only one type.
Once declared, the type of a reference variable cannot be changed.
3
Polymorphism
What is Polymorphism?
The reference variable can be reassigned to other objects provided that it is
not declared final.
The type of the reference variable would determine the methods that it can
invoke on the object.
A reference variable can refer to any object of its declared type or any
subtype of its declared type.
A reference variable can be declared as a class or interface type.
4
Polymorphism
We get an error in the NewsFeed class, because it cannot find the display
method.
5
Polymorphism
6
Polymorphism
post.display();
System.out.println();
}
The compiler informs display does not exist in post.
This should mean that post.display() ought to work, because, whatever it is—
MessagePost or PhotoPost—we know that it does have a display method. 7
Polymorphism
We do not know which one of these it is, assuming that we have entered both
MessagePost and PhotoPost objects into the feed.
9
Polymorphism
The dynamic type is often only known at runtime, so the compiler has no
other choice but to use the static type
if it wants to do any checks at compile time. The static type of post is Post,
and Post does not have a display method.
10
Polymorphism
11
Polymorphism
12
Polymorphism
13
Polymorphism
14
Polymorphism
15
Access Modifiers
Protected Access
In OOP a level of access lies between the complete restriction of private
access and the full availability of public access
Declaring a field or a method protected allows direct access to it from (direct
or indirect) subclasses.
protected long getTimeStamp()
{
return timestamp;
}
Protected access allows access to the fields or methods within a class itself
and from all its subclasses, but not from other classes
16
Access Modifiers
Access Levels
17
Access Modifiers
Access Levels
While protected access can be applied to any member of a class, it is usually
encapsulation.
There are, however, occasional valid cases where direct access by subclasses
is desirable.
18
Access Modifiers
subclasses.
Methods declared private are not inherited at all, so there is no rule for
them.
19
Final Keyword
21
Final Keyword
22
Static Keyword
Static Keyword
The static keyword in java is used for memory management mainly.
We can apply java static keyword with variables, methods, blocks and nested
class.
The static keyword belongs to the class than instance of the class.
3. block
4. nested class
23
Static Keyword
Static Variable
If declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all objects
(that is not unique for each object) e.g. company name of employees,college
name of students etc.
The static variable gets memory only once in class area at the time of class
loading.
Advantage of Static Variable is that It makes your program memory
efficient (i.e it saves memory).
Suppose there are 500 students in my college, now all instance data members
will get memory each time when object is created.All student have its unique
rollno and name so instance data member is good.Here, college refers to the
common property of all objects.If we make it static,this field will get memory
only once.
24
Static Keyword
Static Variable
25
Static Keyword
Static Methods
If you apply static keyword with any method, it is known as static
method.
– A static method belongs to the class rather than object of a class.
– A static method can be invoked without the need for creating an
instance of a class.
– static method can access static data member and can change the
value of it.
Restrictions
There are two main restrictions for the static method. They are:
1. The static method can not use non static data member or call non-static
method directly.
2. this and super cannot be used in static context.
26
Static Keyword
Static Block
Is used to initialize the static data member.
27
28