Object-Oriented Programming (CS F213) : BITS Pilani
Object-Oriented Programming (CS F213) : BITS Pilani
Object-Oriented Programming (CS F213) : BITS Pilani
BITS Pilani
Hyderabad Campus
Agenda
• Inheritance
• Polymorphism
Hierarchical
Single
Multilevel Note: Please note that Java does not support multiple
inheritance with classes. In java, we can achieve
multiple inheritance only through Interfaces.
Output:
weeping…
barking…
eating…
1. ______ is a form of software reusability in which new classes acquire the members of existing
classes and embellish those classes with new capabilities.
Ans: Inheritance
2. When an object of a subclass is instantiated, a superclass ______ is called implicitly or explicitly.
Ans: constructor
3. State True or False: Superclass constructors are not inherited by subclasses.
Ans: True
Superclass Subclasses
Output
class A{
int i, j;
}
class B extends A{ Output:
int j; 2 3
void display(){
super.i=j+1;
System.out.println(j+" "+i);
}
}
class MainExample{
public static void main(String args[]){
B obj=new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
BITS Pilani, Hyderabad Campus
Method Overriding
• When a method in a subclass has the same name and type signature as a
method in its superclass, then the method in the subclass is said to override
the method in the superclass. This is known method overriding.
• When an overridden method is called from within its subclass, it will always
refer to the version of that method defined by the subclass.
• The version of the method defined by the superclass will be hidden.
Method overloading is used to allow the Method overriding is used to provide the
object to call the same method by passing specific implementation of the method that is
varied number of values with different types. already provided by its superclass.
Method overloading is performed within a Method Overriding occurs in two classes that
class. have IS-A (inheritance) relationship.
In case method overloading, parameters must In case of Method overriding, parameters
be different (number of parameters, or their must be same.
types, order of parameters).
Method overloading is the example of compile Method overriding is the example of run time
time polymorphism polymorphism.
In simple words: If there are some methods present in the superclass, but overridden
(same methods) by a subclass and assume that an object of subclass is created, and we
assign that object to the superclass reference variable, then we can use the superclass
reference variable to execute the overridden method defined in the subclass.
Explanation :
A child class reference variable cannot be used to store an instance of parent class.
class A{
int i=10;
}
class B extends A{
int i=20;
}
public class Solution{
public static void main(String args[]){
A a=new B();
System.out.println(a.i);
}
}
Output:
10
Note: Superclass reference can access only the overridden methods of subclass and cannot
access the variables of subclass but can access its own variables.
BITS Pilani, Hyderabad Campus
What is the output of the below
program?
class A{
static void disp(){
System.out.println("Static method of class A ");
}
}
class B extends A{ Output:
static void disp(){ Static method of class A
System.out.println("Static method of class B");
} Note: Methods of both classes
} are static due to which the
class Main{ superclass reference variable
public static void main(String args[]) accesses its own static method
{ even though it is assigned the
A a=new B(); object of subclass B.
a.disp();
}
}
BITS Pilani, Hyderabad Campus
Abstract Class
• Abstract classes are classes for which we never intend to create objects.
• The purpose of abstract classes is to specify an abstract method in a superclass, that if it is extended
by a subclass, then the subclass must implement the abstract method (i.e., provide a method
definition).
• It is useful in situations where a superclass is unable to create a meaningful implementation for a
method. These methods would be specified by the abstract type modifier that need to be overridden
by subclasses who provide implementation for these methods..
• A class which is declared with the abstract keyword is known as an abstract class in Java.
To declare a class as abstract:
Use the abstract keyword in front of the class keyword.
To declare an abstract method:
abstract type method_name(parameter-list)
Note: Any class that contains one ore more abstract methods must be declared
as abstract.
BITS Pilani, Hyderabad Campus
Rules of abstract class
• Abstract classes cannot be instantiated, i.e., objects cannot be created with the new operator.
• Although abstract classes cannot be used to instantiate objects, but they can be used to create
object references, because Java’s approach to run-time polymorphism is implemented through the
use of superclass references.
• Abstract classes can have abstract methods (without body) and non-abstract methods (method with
the body).
• It can have constructors and static methods but no abstract static methods.
• Abstract methods of an abstract superclass must be implemented by subclasses. If the subclass
does not implement the abstract methods, then that class also must be declared as abstract itself.
• Abstract class cannot be final.
Notes: -When abstract specifier is applied to a class, then that class cannot be instantiated.
-When abstract specifier is applied to member function declaration, then that function should be
implemented in subclass.
Output:
Woof
BITS Pilani, Hyderabad Campus
Another example program on abstract class
Why aggregation?
• To maintain code re-usability.
Example program:
class A{
final int CAPACITY=10;
}
class BFinal{
public static void main(String args[])
{
A obj=new A();
System.out.println(obj.CAPACITY);
obj.CAPACITY=20; //this will generate a compiler error.
}
} BITS Pilani, Hyderabad Campus
2. final to prevent method overriding
• There will be times when we will want to prevent it from occurring.
• To disallow a method from being overridden, specify final as a modifier at the start of its declaration.
• Methods declared as final cannot be overridden.
The following code segment prevents inheritance and generates compile-time error if we try to inherit:
Note: It is illegal to declare a class as both abstract and final because an abstract class is incomplete
by itself and relies upon its subclasses to provide complete implementations.
BITS Pilani, Hyderabad Campus
Nested Classes
• Java allows us to define a class within another class. Such a class is called a nested class.
• A nested class is a member of its enclosing class (outer class).
Important Points:
• Nested classes are divided into two categories: non-static and static.
• Non-static nested classes are called inner classes.
• Nested classes that are declared static are called static nested classes.
• Non-static nested classes (inner classes) have access to other members of the enclosing class, even if
they are declared private.
• Static nested classes do not have access to other members of the enclosing class.
Example code with nested classes
• It is a way of logically grouping classes that are only used in one place: If a
class is useful to only one other class, then it is logical to embed it in that class
and keep the two together. Nesting such "helper classes" makes their package
more streamlined.
• It increases encapsulation: Consider two top-level classes, A and B, where B
needs access to members of A that would otherwise be declared private. By hiding
class B within class A, A’s members can be declared private and B can access
them. In addition, B itself can be hidden from the outside world.
• It can lead to more readable and maintainable code: Nesting small classes
within top-level classes places the code closer to where it is used.
• An instance of InnerClass can exist only within an instance of OuterClass and has direct access to
the methods and fields of its enclosing instance.
• Also, because an inner class is associated with an instance, it cannot define any static members
itself.
Example code segment of inner class within outer class:
• To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object
within the outer object with this syntax:
• Inheritance
• Polymorphism
• Abstract Classes
• Nested Classes