Object Oriented Programming Object Oriented Programming: Lecture-13-16 Instructor Name
Object Oriented Programming Object Oriented Programming: Lecture-13-16 Instructor Name
Instructor Name:
Lecture-13-16
Today’s Lecture
Inheritance
2
Inheritance
Introduction
While programming we may face problem of Code Duplication.
Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another.
Introduction
Inheritance allows us to define one class as an extension of another class.
The class which inherits the properties of other is known as subclass (derived
class, child class) and the class whose properties are inherited is known as
superclass (base class, parent class).
A subclass is a class that extends (inherits from) another class. It inherits all
fields and methods from its superclass.
It's main uses are to enable polymorphism and to be able to reuse code for
different classes by putting it in a common super class
5
Inheritance
Advantage of Inheritance
If we develop any application using concept of Inheritance, that application
have following advantages,
Important Points
In java programming one derived class can extends only one base class
because java programming does not support multiple inheritance through the
concept of classes, but it can be supported through the concept of Interface.
Whenever we develop any inheritance application first create an object of
bottom most derived class but not for top most base class.
When we create an object of bottom most derived class, first we get the
memory space for the data members of top most base class, and then we get
the memory space for data member of other bottom most derived class.
Bottom most derived class contains logical appearance for the data members
of all top most base classes.
If we do not want to give the features of base class to the derived class then
the definition of the base class must be preceded by final hence final base
classes are not reusable or not inheritable.
7
Inheritance
Important Points
If we are do not want to give some of the features of base class to derived
class than such features of base class must be as private hence private
features of base class are not inheritable or accessible in derived class.
Data members and methods of a base class can be inherited into the derived
class but constructors of base class can not be inherited because every
constructor of a class is made for initializing its own data members but not
made for initializing the data members of other classes.
An object of base class can contain details about features of same class but an
object of base class never contains the details about special features of its
derived class (this concept is known as scope of base class object).
For each and every class in java there exists an implicit predefined super class
called java.lang.Object. because it providers garbage collection facilities to its
sub classes for collecting un-used memory space and improved the
performance of java application.
8
Inheritance
super.method();
13
Inheritance
super(values)
14
Inheritance - Example
Message Post
The details we want to store for each message post are:
the username of the author
the text of the message
a time stamp (time of posting)
show many people like this post
a list of comments on this post by other users
16
Facebook Example
Photo Post
The details we want to store for each photo post are:
the username of the author
the filename of the image to display
the caption for the photo (one line of text)
a time stamp (time of posting)
how many people like this post
a list of comments on this post by other users
17
Facebook Example
MessagePost PhotoPost
username username
message filename
timestamp caption
likes timestamp
comments like
comments 18
Facebook Example
NewsFeed Class
Once we have defined the MessagePost and PhotoPost classes, we
can create as many post objects as we need—one object per message
post or photo post that we want to store.
We also need another object: an object representing the complete
news feed that can hold a collection of message posts and a collection
of photo posts.
For this, we shall create a class called NewsFeed.
The NewsFeed object could itself hold two collection objects (for
example, of types ArrayList <MessagePost> and
ArrayList<PhotoPost>).
Facebook Example – NewsFeed Class
import java.util.ArrayList;
public class NewsFeed
{
private ArrayList<MessagePost> messages;
private ArrayList<PhotoPost> photos;
public NewsFeed()
{
messages = new ArrayList<MessagePost>();
photos = new ArrayList<PhotoPost>();
}
public void addMessagePost(MessagePost message)
{
messages.add(message);
}
public void addPhotoPost(PhotoPost photo)
{
photos.add(photo);
}
Facebook Example – NewsFeed Class
public void show()
{
// display all text posts
for(MessagePost message : messages) {
message.display();
System.out.println(); // empty line between posts
}
We need to do the same for the fourth type of post and so on.
Solution to Problem?
Inheritance
Using Inheritance
By defining new class Post we need to define the common feature only once.
Using Inheritance
Class Post
import java.util.ArrayList;
Class Post
public void like()
{
likes++;
}
Class Post
public long getTimeStamp()
{
return timestamp;
}
Class Post
public void display(){
System.out.println(username);
System.out.print(timeString(timestamp));
if(likes > 0) {
System.out.println(" - " + likes + " people like
this.");
}
else {
System.out.println();
}
if(comments.isEmpty()) {
System.out.println(" No comments.");
}
else {
System.out.println(" " + comments.size() + "
comment(s). Click here to view.");
}
Using Inheritance – Facebook Example
Class MessagePost
import java.util.ArrayList;
public class MessagePost extends Post
{
private String message;
public MessagePost(String author, String text)
{
super(author);
message = text;
}
public String getText()
{
return message;
}
The keyword super is a call from the subclass constructor to the constructor
of the superclass.
Its effect is that the Post constructor is executed as part of the MessagePost
constructor’s execution.
The Post constructor initializes the post’s fields, and then returns to the
MessagePost constructor, which initializes the remaining field defined in the
MessagePost class.
Using Inheritance – Facebook Example
super();
Class PhotoPost
import java.util.ArrayList;
Class PhotoPost
public String getImageFile()
{
return filename;
}
We can reuse the code that we have written for photo posts and message
posts (in the Post class) so that it also works for the EventPost class.
Using Inheritance – Facebook Example
Class NewsFeed
Import java.util.ArrayList;
public NewsFeed()
{
posts = new ArrayList<Post>();
}
Class NewsFeed
public void show()
{
// display all posts
for(Post post : posts) {
post.display();
System.out.println();
}
}
}
In this class we have one add method to add both message and photo posts
We have one show method to show both message and photo posts
Inheritance
Code Reuse
Easier maintenance
Extendibility
Subtyping
What is Subtyping?
Subtyping refers to compatibility of interfaces.
A type B is a subtype of A if every function that can be invoked on an object
of type A can also be invoked on an object of type B.
To implement a subtype, it is often useful to use the implementation of its
supertype This is also called “subclassing”
In Java:
class B extends A
B is a subtype of A
B inherits from A both subtyping and inheritance
class C implements F
C is a subtype of F
just subtyping
Subtyping
Method Dispatch
B is a subtype of A
A a = new A ();
B b = new B ();
MessagePost.
Out of last two First will give compile time error and second
statement will give runtime error
Casting should be avoided wherever possible, because it can lead
to runtime errors
Autoboxing & Wrapper Classes