Java Inheritancepolymorphism BW
Java Inheritancepolymorphism BW
Polymorphism
13 March 2019
Contents
• Inheritance
• Polymorphism
Inheritance
https://www.pinterest.com/pin/16888567332662930/
Inheritance (“is-a relationship”)
• A way of grouping a more
general variables and
methods into a super
class; a specialized subset
(sub-class) extends /
inherits from) this super
class.
• Inheritance refers to
objects inheriting
properties (data & https://docs.oracle.com/javase/tutorial/java/concepts/inheritance.html
C2 C1
public class Circle /** Return radius */
{ public double getRadius()
/** The radius of the circle */ { return radius; }
private double radius;
/** Set a new radius */
/** Construct a circle with radius 1 public void setRadius(double
*/ newRadius)
public Circle() { radius = (newRadius >= 0) ?
{ radius = 1.0; } newRadius : 0; }
/** Construct a circle with a /** Return the area of this circle */
specified radius */ public double findArea()
public Circle(double newRadius) { return radius* radius *3.14159; }
{ radius = newRadius; } Circle }
-radius
+getRadius
+setRadius
+findArea
// Cylinder.java: Class definition for describing Cylinder
public class Cylinder extends Circle
{ private double length = 1;
}
Superclass Subclass
supertype subtype
Circle Cylinder
-radius
-length
+getRadius
+setRadius +getLength
+findArea +setLength
+findVolume
Cylinder cylinder = new Cylinder();
System.out.println("The length is " + cylinder.getLength());
System.out.println("The radius is " + cylinder.getRadius());
System.out.println("The volume of the cylinder is " +
cylinder.findVolume());
System.out.println("The area of the circle is " +
cylinder.findArea());
The output is
The length is 1.0
The radius is 1.0
The volume of the cylinder is 3.14159
The area of the circle is 3.14159
Superclass Subclass
supertype subtype
Circle Cylinder
-radius
-length
+getRadius
+setRadius +getLength
+findArea +setLength
+findVolume
17
Constructing an instance of a class
Constructor Chaining invokes all the superclasses’
Example 1 constructors along the inheritance
chain.
class Person
{ public Person()
{ CVCCno-arg constructor is invoked");}
System.out.println("Person's
}
public Employee(String s)
{ System.out.println(s); }
}
public Faculty()
{ System.out.println("Faculty's no-arg constructor is invoked"); }
Example 2
class Fruit
{
ERROR
public Fruit(String name)
{
System.out.println("Fruit's constructor is invoked");
}
} It is better to provide a
no-arg constructor to
public class Apple extends Fruit avoid programming
{ } errors.
Calling Superclass Methods
The keyword super can also be used to reference a
method other than constructor in the superclass.
General syntax:
super.method(parameters)
For example, in class Cylinder:
public double findVolume()
{ return super.findArea() * length; }
The final Modifier
• The modifiers are used on classes and class members
(data and methods), except that the final modifier can also
be used on local variables in a method.
• A final local variable is a constant inside a method.
The final class cannot be extended:
final class Math
{ ... }
visibility increases
package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }
package p2;
https://javahungry.blogspot.com/2018/11/method-
overloading-in-java-with-examples.html
https://www.programcreek.com/2009/02/overriding-and-
overloading-in-java-with-examples/
https://programmingstack.com/java-
methods/9-java/49-java-method-
overloading.html
Overriding
Overriding Methods in the Superclass
// New cylinder class that overrides the findArea()
// Method defined in the circle class.
Output:
Resources
• BlueJ http://bluej.org/
• Think Java (Allen Downey & Chris Mayfield)
– http://greenteapress.com/thinkjava6/thinkjava.pdf
• Online Java Compiler
– https://www.tutorialspoint.com/compile_java_online.php
– https://www.jdoodle.com/online-java-compiler
– https://www.compilejava.net
• Online Java Tutorial
– https://docs.oracle.com/javase/tutorial/java/
– https://www.w3schools.in/java-tutorial/
– https://www.tutorialspoint.com/java/