Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 5

Object Oriented Programming

Prof. Rahul B. Diwate

1
Unit-3
• Inheritance:
• Inheritance in Java,
• Types,
• Constructor in Inheritance,
• Using final with Inheritance,
• Accessing superclass member,
• Override private methods,
• Parent and Child classes having same data member,
• Base vs derived class reference.
• Polymorphism:
• Method Overloading,
• Overloading main(),
• Static vs Dynamic Binding,
• Method Hiding.
• Private and final methods,
• Passing and Returning Objects in Java
Base vs derived class reference
• There are two approaches to refer a subclass object. Both have some advantages/disadvantages over
the other. The declaration affect is seen on methods that are visible at compile-time.
1. First approach (Referencing using Superclass reference): A reference variable of a superclass can be
used to a refer any subclass object derived from that superclass. If the methods are present in
SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.
2. Second approach (Referencing using subclass reference) : A subclass reference can be used to refer
its object.
PART-2
PART-1
// derived class
// Java program to illustrate referring to a subclass base class class MountainBike extends Bicycle
class Bicycle {
{
// the Bicycle class has two fields // the MountainBike subclass adds one more field
public int gear; public int seatHeight;
public int speed;
// the MountainBike subclass has one constructor
public MountainBike(int gear,int speed,
// the Bicycle class has one constructor
int startHeight)
public Bicycle(int gear, int speed)
{
{ // invoking base-class(Bicycle) constructor
this.gear = gear; super(gear, speed);
this.speed = speed; seatHeight = startHeight;
} }

// the Bicycle class has three methods // the MountainBike subclass adds one more method
public void applyBrake(int decrement) public void setHeight(int newValue)
{
{
seatHeight = newValue;
speed -= decrement;
}
}
// overriding toString() method
public void speedUp(int increment) // of Bicycle to print more info
{ @Override
speed += increment; public String toString()
} {

return (super.toString()+
// toString() method to print info of Bicycle
"\nseat height is "+seatHeight);
public String toString() }
{
return("No of gears are "+gear +"\n" + "speed of bicycle is "+speed); }
}
PART-3
// driver class
public class Test
{
public static void main(String args[])
{
// using superclass reference
// first approach
Bicycle mb2 = new MountainBike(4, 200, 20);

// using subclass reference( )


// second approach
MountainBike mb1 = new MountainBike(3, 100, 25);

System.out.println("Seat height of first bicycle is "


+ mb1.seatHeight);

// In case of overridden methods


// always subclass
// method will be executed
System.out.println(mb1.toString());
System.out.println(mb2.toString());

/* The following statement is invalid because Bicycle


does not define a seatHeight.
// System.out.println("seat height of second bicycle is "
+ mb2.seatHeight); */

/* The following statement is invalid because Bicycle


does not define setHeight() method.
mb2.setHeight(21);*/

}
}

You might also like