Search Logic Blocks

Monday, December 14, 2020

Java: Partial Implementation of the Interface

Any class, which does not implement all the methods of an interface, must be declared as an abstract class. And any class which inherits the above abstract class, should implement the rest of the methods of the interface or should be abstract itself.

By using the example from the earlier post, I have created an abstract class called Quadrilateral. And the classes Rectangle, Square and Trapezoid are the subclasses of class Quadrilateral. The class Quadrilateral only defines the number of sides, which is the same for all the quadrilateral shapes.

As mentioned above, the class Quadrilateral doesn't implement all the methods from the interface IPolygon, so the class is declared as an abstract class. The classes inherited from the abstract class Quadrilateral, have implemented all the methods from the interface IPolygon.
// Interface Polygon gives structure of the methods needed to work with any polygon
// Every class implementing this interface should have all the methods
public interface IPolygon {
// Returns the text telling what shape it is
public String whatShape();

// Returns the numbers of sides
public int noOfSides();

// Returns the array of lengths of all sides
public double[] getSides();

// Returns the area of the shape
public double area();

// Returns the perimeter of the shape
public double perimeter();
}
// Abstract Class Quadrilateral implementing the class IPolygon
public abstract class Quadrilateral implements IPolygon {
final int noOfSides = 4;

public int noOfSides() {
return noOfSides;
}
}
// Class Rectangle subclass of class Quadrilateral
public class Rectangle extends Quadrilateral {
double side1;
double side2;
double perimeter;
double area;

// Rectangle Constructor with no arguments
Rectangle() {
this.side1 = 6;
this.side2 = 4;
}

// Rectangle Constructor with two arguments
Rectangle(double side1, double side2) {
this.side1 = side1;
this.side2 = side2;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Rectangle";
}

// Returns the array of lengths of all sides
public double[] getSides() {
double arrSides[] = new double[4];
arrSides[0] = side1;
arrSides[1] = side2;
arrSides[2] = side1;
arrSides[3] = side2;
return arrSides;
}

// Returns the area of the rectangle
public double area() {
double pr;
pr = side1 * side2;
area = Math.round(pr * 100) / 100.0;
return area;
}

// Returns the perimeter of the rectangle
public double perimeter() {
perimeter = Math.round((2 * (side1 + side2)) * 100) / 100.0;
return perimeter;
}
}
// Class Square subclass of class Quadrilateral
public class Square extends Quadrilateral {
double side;
double perimeter;
double area;

// Square Constructor with no arguments
Square() {
this.side = 6;
}

// Square Constructor with three arguments
Square(double side) {
this.side = side;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Square";
}

// Returns the array of lengths of all sides
public double[] getSides() {
double arrSides[] = new double[4];
arrSides[0] = side;
arrSides[1] = side;
arrSides[2] = side;
arrSides[3] = side;
return arrSides;
}

// Returns the area of the Square
public double area() {
double pr;
pr = side * side;
area = Math.round(pr * 100) / 100.0;
return area;
}

// Returns the perimeter of the Square
public double perimeter() {
perimeter = Math.round(4 * side * 100) / 100.0;
return perimeter;
}
}
// Class Trapezoid subclass of class Quadrilateral
public class Trapezoid extends Quadrilateral {
double side1;
double side2;
double lbase; // Long Base
double sbase; // Short Base
double height; // Height of trapezoid
double perimeter;
double area;

// Trapezoid Constructor with no arguments
Trapezoid() {
this.side1 = 15;
this.side2 = 20;
this.lbase = 55;
this.sbase = 30;
this.height = 12;
}

// Trapezoid Constructor with four arguments
Trapezoid(double side1, double side2, double lbase, double sbase, double height) {
this.side1 = side1;
this.side2 = side2;
this.lbase = lbase;
this.sbase = sbase;
this.height = height;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Trapezoid";
}

// Returns the array of lengths of all sides
public double[] getSides() {
double arrSides[] = new double[4];
arrSides[0] = side1;
arrSides[1] = side2;
arrSides[2] = lbase;
arrSides[3] = sbase;
return arrSides;
}

// Returns the area of the Trapezoid
public double area() {
double pr;
pr = (lbase + sbase) / 2 * height;
area = Math.round(pr * 100) / 100.0;
return area;
}

// Returns the perimeter of the Trapezoid
public double perimeter() {
perimeter = side1 + side2 + lbase + sbase;
perimeter = Math.round(perimeter * 100) / 100.0;
return perimeter;
}
}
// Class for testing the interface IPolygon and its implementing classes
public class PolygonTest {
public static void main(String args[]) {
IPolygon P[] = new IPolygon[4];
P[0] = new Square(9);
P[1] = new Rectangle(18, 11);
P[2] = new Trapezoid(7, 5, 8, 6, 4);
P[3] = new Trapezoid();

for(int i = 0; i < P.length; i++) {
System.out.println("Polygon " + (i+1) + " is " + P[i].whatShape());
System.out.println("No of sides: " + P[i].noOfSides());
double allSides[] = P[i].getSides();
System.out.print("All sides are : ");
for(int j = 0; j < allSides.length; j++) {
System.out.print("Side" + (j+1) + ": " + allSides[j] + " ");
}
System.out.println();
System.out.println("Perimeter is : " + P[i].perimeter());
System.out.println("Area is : " + P[i].area());
System.out.println();
}
}
}
Here is the output for above code:

Polygon 1 is Square
No of sides: 4
All sides are : Side1: 9.0   Side2: 9.0   Side3: 9.0   Side4: 9.0   
Perimeter is : 36.0
Area is : 81.0

Polygon 2 is Rectangle
No of sides: 4
All sides are : Side1: 18.0   Side2: 11.0   Side3: 18.0   Side4: 11.0   
Perimeter is : 58.0
Area is : 198.0

Polygon 3 is Trapezoid
No of sides: 4
All sides are : Side1: 7.0   Side2: 5.0   Side3: 8.0   Side4: 6.0   
Perimeter is : 26.0
Area is : 28.0

Polygon 4 is Trapezoid
No of sides: 4
All sides are : Side1: 15.0   Side2: 20.0   Side3: 55.0   Side4: 30.0   
Perimeter is : 120.0
Area is : 510.0

You can find code at Github link.

Saturday, December 12, 2020

Java: Interfaces

What is an Interface?
We have already discussed about the abstract methods and abstract classes. Abstract methods (empty methods) are declared in the superclass, but the body of the method is written in the subclass. The subclass must define all the abstract methods given in the super class. An abstract method acts like a prototype.

Interface is also like an abstract class, where all the method bodies (except default and static methods) are empty (or abstract) and an interface can not be instantiated like a regular class. Interfaces can only be implemented by other classes or can be extended by other interfaces. One class can implement more than one interface. The class, which implements the interface, should have implementation of all the methods available in the interface.

How to declare an Interface -
Interface is declared by using a keyword interface followed by the interface name. The interfaces can have only the method signatures or declarations. Following code is an example of interface IPolygon and its implementation in two classes - Triangle nad Rectangle and one demo class - PolygonTest.
// Interface Polygon gives structure of the methods needed to work with any polygon
// Every class implementing this interface should have all the methods
public interface IPolygon {
// Returns the text telling what shape it is
public String whatShape();

// Returns the numbers of sides
public int noOfSides();

// Returns the array of lengths of all sides
public double[] getSides();

// Returns the area of the shape
public double area();

// Returns the perimeter of the shape
public double perimeter();
}
// Class Triangle implementing the interface IPolygon
public class Triangle implements IPolygon {
final int noOfSides = 3;
double side1;
double side2;
double side3;
double perimeter;
double area;

// Triangle Constructor with no arguments
Triangle() {
this.side1 = 4;
this.side2 = 4;
this.side3 = 4;
}

// Triangle Constructor with three arguments
Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the numbers of sides
public int noOfSides() {
return noOfSides;
}

// Returns the array of lengths of all sides
public double[] getSides() {
double arrSides[] = new double[3];
arrSides[0] = side1;
arrSides[1] = side2;
arrSides[2] = side3;
return arrSides;
}

// Returns the area of the triangle
public double area() {
double p, diff1, diff2, diff3, pr;
p = perimeter / 2;
diff1 = p - side1;
diff2 = p - side2;
diff3 = p - side3;
pr = p * diff1 * diff2 * diff3;
area = Math.round(Math.sqrt(pr) * 100) / 100.0;
return area;
}

// Returns the perimeter of the triangle
public double perimeter() {
perimeter = Math.round((side1 + side2 + side3) * 100) / 100.0;
return perimeter;
}
}
// Class Rectangle implementing the interface IPolygon
public class Rectangle implements IPolygon {
final int noOfSides = 4;
double side1;
double side2;
double perimeter;
double area;

// Rectangle Constructor with no arguments
Rectangle() {
this.side1 = 4;
this.side2 = 4;
}

// Rectangle Constructor with three arguments
Rectangle(double side1, double side2) {
this.side1 = side1;
this.side2 = side2;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Rectangle";
}

// Returns the numbers of sides
public int noOfSides() {
return noOfSides;
}

// Returns the array of lengths of all sides
public double[] getSides() {
double arrSides[] = new double[4];
arrSides[0] = side1;
arrSides[1] = side2;
arrSides[2] = side1;
arrSides[3] = side2;
return arrSides;
}

// Returns the area of the rectangle
public double area() {
double pr;
pr = side1 * side2;
area = Math.round(pr * 100) / 100.0;
return area;
}

// Returns the perimeter of the rectangle
public double perimeter() {
perimeter = Math.round((2 * (side1 + side2)) * 100) / 100.0;
return perimeter;
}
}
// Class for testing the interface IPolygon and its implementing classes
public class PolygonTest {
public static void main(String args[]) {
IPolygon P[] = new IPolygon[2];
P[0] = new Triangle(16.6, 11.2, 17.9);
P[1] = new Rectangle(12.5, 15.5);

for(int i = 0; i < P.length; i++) {
System.out.println("Polygon " + (i+1) + " is " + P[i].whatShape());
System.out.println("No of sides: " + P[i].noOfSides());
double allSides[] = P[i].getSides();
System.out.print("All sides are : ");
for(int j = 0; j < allSides.length; j++) {
System.out.print("Side" + (j+1) + ": " + allSides[j] + " ");
}
System.out.println();
System.out.println("Perimeter is : " + P[i].perimeter());
System.out.println("Area is : " + P[i].area());
System.out.println();
}
}
}
In this case, the interface is  declared as public, so any class, which is inside and outside of the package, can implement the interface. When there is no access modifier, the interface can be only accessed by the classes inside the package in which the interface is defined. The interface methods are declared like the variables and declaration statement is ended by a semicolon (;).

Variables can be declared inside the interface, but they must be initialized and they are implicitly final and static. The variables defined in the interface can not be changed (final) and can be accessed by interface (static) itself. All the members of the interface are implicitly public.

How to implement an Interface -
To implement an interface, implements clause is used in the class definition and all the methods are implemented in the class. One class can implement any number of interfaces by having all the interface names separated by commas.

The classes Triangle and Rectangle, both have implemented all the methods declared in the interface and they have their own implementations. Interface can not have its own constructor, as the interface can not be instantiated. But the classes implementing the interface can have their own constructors.

How to access Interface Methods -
Interface methods can be accessed through the implementing class in the same way as we access the regular class methods. But interface methods can also be accessed by reference as we access superclass methods by reference. In the above example, in the class PolygonTest, the interface methods are accessed only by reference. There is an array of interface class objects and the object references of classes Triangle and Rectangle are assigned to the array element.

When the loop is run at run-time, the program decides what type of object is referred and the method of that class is run dynamically. But the interface reference object can only call methods declared in the interface, but not the other methods defined in the class referred to.

Here is the output of above code-

Polygon 1 is Triangle
No of sides: 3
All sides are : Side1: 16.6   Side2: 11.2   Side3: 17.9   
Perimeter is : 45.7
Area is : 90.75

Polygon 2 is Rectangle
No of sides: 4
All sides are : Side1: 12.5   Side2: 15.5   Side3: 12.5   Side4: 15.5   
Perimeter is : 56.0
Area is : 193.75

Code can be get at Github link.

Friday, October 23, 2020

Java: Packages and Its Uses

We already have discussed about what is the package? And how to import the packages into the classes out from the package. We have also learnt about the access modifiers and their scopes of visibility in the package itself and outside from the package. Let's revise few points and go deeper into to learn more about the packages.

Why packages are so important?
Packages are actually directories in which the classes are stored. So, the packages provide structure and organize all the related classes, interfaces and other components. Packages give the programmers to have flexibility to have the same names as other programmers and provide the way to reuse other programmers' and built-in Java APIs. Packages are one important part of Java's feature encapsulation. So packages are, essentially the containers of related classes and interfaces.

How the package can be used by other packages?
Let's see the same example in the earlier post about package. Here is the package named as cards and there are four classes in the package cards - spade, club, diamond, and heart

Here in the following example, I use directly the package name and classes's names to access the class constructors.
// Class spade in package logicblocks.cards
package logicblocks.cards;

public class spade {
public spade() {
System.out.println("Spade card created");
}
}
// Class heart in package logicblocks.cards
package logicblocks.cards;

public class heart {
public heart() {
System.out.println("Heart card created");
}
}
// Class club in package logicblocks.cards
package logicblocks.cards;

public class club {
public club() {
System.out.println("Club card created");
}
}
// Class diamond in package logicblocks.cards
package logicblocks.cards;

public class diamond {
public diamond() {
System.out.println("Diamond card created");
}
}
// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;

public class cardsdemo {
public static void main(String args[]) {
// Classes in the package cards are accessed directly using pacakge path, name and class names
logicblocks.cards.spade sp = new logicblocks.cards.spade();
logicblocks.cards.heart hr = new logicblocks.cards.heart();
logicblocks.cards.club cl = new logicblocks.cards.club();
logicblocks.cards.diamond dm = new logicblocks.cards.diamond();
}
}
To avoid the typing repeatedly or redundantly, the package can be imported in the start of the program itself like in below example. It saves repeated code, avoiding writing wrong names and also makes more convenient for the programmers. The individual classes can be imported by using the individual class names after the package name.
// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;

// Individual classes in the package cards are imported using pacakge path and name
import logicblocks.cards.spade;
import logicblocks.cards.heart;
import logicblocks.cards.club;
import logicblocks.cards.diamond;

public class cardsdemo {
public static void main(String args[]) {
spade sp = new spade();
heart hr = new heart();
club cl = new club();
diamond dm = new diamond();
}
}
Or all the classes can be imported in one import statement by using * after the package name.
// Class cardsdemo in package logicblocks outside from package cards
package logicblocks;

// All the classes in the package cards are imported using pacakge path and name
// * means all the classes and interfaces in the package cards can be accessed through the program
import logicblocks.cards.*;

public class cardsdemo {
public static void main(String args[]) {
spade sp = new spade();
heart hr = new heart();
club cl = new club();
diamond dm = new diamond();
}
}
Java's Standard Packages
Java's built-in API (Application Program Interface) or class library is contained in the packages. There are a large number of standard classes that are available to all the programs. The package java is at the top of the packages hierarchy. There are few packages which are used commonly, as I have mentioned in my earlier post about the package -

Package NameDescription
java.langProvides classes that are fundamental to the design of the Java programming language.
java.ioProvides for system input and output through data streams, serialization and the file system.
java.netProvides the classes for implementing networking applications.
java.appletProvides the classes necessary to create an applet and the classes an applet uses to communicate with its applet context.
java.awtContains all of the classes for creating user interfaces and for painting graphics and images.
java.utilContains the collections framework, some internationalization support classes, a service loader, properties, random number generation, string parsing and scanning classes, base64 encoding and decoding, a bit array, and several miscellaneous utility classes.
java.timeThe main API for dates, times, instants, and durations.
java.textProvides classes and interfaces for handling text, dates, numbers, and messages in a manner independent of natural languages.
java.mathProvides classes for performing arbitrary-precision integer arithmetic (BigInteger) and arbitrary-precision decimal arithmetic (BigDecimal).

Tuesday, October 20, 2020

Java: Access Modifiers

We already discuss about the classes and objects, and packages. We already have discussed also about all three access modifiers (public, private and protected) and their scopes in the classes and packages. Let's revise all the access modifiers and default modifier (means when there is no modifier specified) in the table below:


Visibility InDefaultPublicProtectedPrivate
Same ClassYesYesYesYes
Same Package SubclassYesYesYesNo
Same package non-subclassYesYesYesNo
Different Package SubclassNoYesYesNo
Different Package non-subclassNoYesNoNo

Java: final keyword

We have already seen usage of the final keyword in the post related to constants. When the final keyword is used with the variables, the variable value can not be changed.
final double pi = 3.14;
In same way, when the final keyword is used with any method, the method becomes final means the method can not be overridden in subclasses.
final int sum(int a, int b) {
return a+b;
}
And if the final keyword is used with a class, that class can not be inherited or subclassed. Abstract class can not be a final class as abstract class as the object of the abstract class can not be instantiated. Here is the final class Square, which can not be subclassed further -
public final class Square extends Shape{
String
fillColor;
// Square Constructor with no arguments
Square() {
super();
this.fillColor = "Blue";
}

// Square Constructor with one argument
Square(String fillColor) {
super();
this.fillColor = fillColor;
}

// Square Constructor with two arguments
Square(double width, String fillColor) {
super(width, width);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Square";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((width * width) * 100) / 100.0;
}

public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}

public void setDiffColor(String fillcolor, String bordercolor) {
this.fillColor = fillcolor;
super.setColor(bordercolor);
}
}
So, the final keyword prevents the variables values to be changed, prevents methods from being overridden and also prevents a class from being inherited.

Monday, October 19, 2020

Java: Let's Practice - How to create Fibonacci sequence through recursion

What is the Fibonacci sequence?
Fibonacci sequence is a sequence of numbers generating new number by adding two previous numbers. It starts with 0 and 1.

SequenceLast two nos.
0
1
10+1
21+1
31+2
52+3

0, 1, 1, 2, 3, 5, 8, 13.......

So, to create recursive method to get next number in the sequence, program should use the same method which calculates the previous numbers of the sequence -

fib(0) = 0
fib(1) = 1
fib(2) = fib(1) + fib(0)
fib(3) = fib(2) + fib(1)
........
fib(n) = fib(n-1) + fib(n-2)

Here is the code -
import java.util.Scanner;

public class FibonacciDemo {
static int[] fibnos;
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
fibnos = new int[n+1];
int fib = fibonacci(n);
System.out.println(n + "th number in the Fibonacci Sequence (0, 1, 2, ...) is : " + fib);

for(int i = 0; i < fibnos.length; i++) {
System.out.print(fibnos[i] + ", ");
}
}

public static int fibonacci(int n) {
int result = 0;

if(n == 0) {
fibnos[0] = 0;
return 0;
}
if(n == 1) {
fibnos[1] = 1;
return 1;
}
result = fibonacci(n-1) + fibonacci(n-2);
fibnos[n] = result;
return result;
}
}
And here is the output -

Enter the number: 6
6th number in the Fibonacci Sequence (0, 1, 2, ...) is : 8
0, 1, 1, 2, 3, 5, 8, 

And here is the explanation -

The program asks user to enter any number n. It passes the number to calculate the nth number of the sequence. The method calculates the next number recursively and returns the result back. The numbers are added into an array to display the sequence.

Code can be found on the Github.

Sunday, October 11, 2020

Java: Let's Practice - How to calculate factorial through recursion

I have already explained about recursion in earlier post and shown the example Reverse the string. In this post, we will use recursion method to calculate the factorial of a number.

What is factorial of a number?
Factorial of a number is the product of all numbers counting backwards from the given number till number 1. Suppose number is 5, so factorial of number 5 is equal to 5 * 4 * 3 * 2 * 1

f(5) = 5 * 4 * 3 * 2 * 1
f(4) = 4 * 3 * 2 * 1
f(3) = 3 * 2 * 1

We can notice the pattern to calculate the factorial.

f(n) = n * f(n-1)

We will use this pattern for recursion. The program will call the same method recursively till the number is 1 and then it will calculate the backwards and return the result.

Here is the program -
import java.util.Scanner;

public class FactorialDemo {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
int n = sc.nextInt();
int fact = factorial(n);
System.out.println("Factorial of the number " + n + " is: " + fact);
}

public static int factorial(int n) {
int result = n;
if(n != 1)
result = result * factorial(n-1);
return result;
}

}
Here is the output - 

Enter the number: 5
Factorial of the number 5 is: 120

The program asks user to enter any number. It passes the number to a method factorial() which calculates the factorial of the number. 
  • The method factorial() method calls itself recursively by decreasing the number by 1 till the number is 1. It returns 1 when the number is 1. 
  • Then it multiplies by 2 and then returns result back to the calling method. 
  • Multiplies by 3 and then returns result back to the calling method.
  • And so on till it reaches the original number
  • When it reaches the original number, multiplies and it returns the final result back to main method.
Code can be found here on Github.

Friday, September 11, 2020

Java: What are Abstract classes?

What are the Abstract Methods?
Abstract methods are methods, which are declared as abstract by using keyword abstract. Abstract methods don't have any implementation and need to be overridden by subclasses. All subclasses must implement the abstract method of super class. When a method is declared as an abstract method, the class must be declared as abstract too.

What is an Abstract Class?
Abstract class is a class which is declared as abstract by using the keyword abstract, and may or may not have abstract methods. An abstract class can not be instantiated, means abstract classes can't have instances, they are to be subclassed / extended necessarily.

Let's review our shape class in earlier posts. In the class definition, we have one method called area() which is required by all the subclasses definitely, but there is no such implementation to calculate area in shape class itself. So, we can convert method area() to an abstract method and also the shape class into an abstract class. Here is the code how we can write an abstract class shape with abstract method area() -
public abstract class Shape {
final static double PI = Math.PI;
double width = 0;
double height = 0;
String
borderColor = "Blue";

// Superclass constructors
Shape() {
this.width = 5;
this.height = 8;
}

Shape(
double width, double height) {
this.width = width;
this.height = height;
}

// Returns the text telling what shape it is
public String whatShape() {
return "";
}

// Returns the area of the shape
public abstract double area();


public void setColor(String color) {
this.borderColor = color;
}
}
And here is the subclass Triangle of shape class and ShapesTest class to show demo of an abstract class -
public class Triangle extends Shape {
String
fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}

// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}


public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}

public void setDiffColor(String fillcolor, String bordercolor) {
this.fillColor = fillcolor;
super.setColor(bordercolor);
}
}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.out.println(t.width);
System.out.println(t.height);
System.out.println(t.fillColor);
t.setDiffColor("Green","Pink");
System.out.println(t.fillColor);
System.out.println(t.borderColor);
}
}
So, to use an abstract class, it must be extended, as the abstract class can not be instantiated. And all the abstract methods must be implemented in the subclass.

We have already learnt about Polymorphism. Inheritance, method overloading, method overriding and abstract classes, all are used to support polymorphism. Code can be found here on Github.

Saturday, August 22, 2020

Java: How to access superclass members

We have already seen how to call superclass constructors in the last post - How to access superclass constructors. Subclass instances / objects can access all the non-private superclass members (variables and methods) directly. But if the methods are overridden, the subclass instances call only the overridden subclass methods. But if the program needs to call superclass method, subclass methods can call them by using a keyword super -
public class Shape {
final static double PI = Math.PI;
double width = 0;
double height = 0;
String borderColor = "Blue";

// Superclass constructors
Shape() {
this.width = 5;
this.height = 8;
}

Shape(
double width, double height) {
this.width = width;
this.height = height;
}

// Returns the text telling what shape it is
public String whatShape() {
return "";
}

// Returns the area of the shape
public double area() {
return 0.0;
}

public void setColor(String color) {
this.borderColor
= color;
}

}
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}

// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}
}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.
out.println(t.width);
System.
out.println(t.height);
System.
out.println(t.fillColor);
System.out.println(t.borderColor);
}
}
In the above example, a new method setColor() is added to set the Shape class's bordercolor variable. And subclass Triangle hasn't overridden the method setColor(). The Triangle class instance calls the method setColor(), Triangle class doesn't have any method named as setColor(). So, the setColor() method from the superclass is called automatically. Here is the output -

4.0
6.0
Yellow
Blue

Overriding Methods
Let's override the method setColor() and then call the method from the subclass instance. In this case, the subclass version of the method setColor() is called. Here is the code and output.
public class Triangle extends Shape {
String
fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}

// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}

public void setColor(String color) {
this.fillColor = color;
}

}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.out.println(t.width);
System.out.println(t.height);
System.out.println(t.fillColor);
t.setColor("Green");
System.out.println(t.fillColor);
System.out.println(t.borderColor);

}
}
4.0
6.0
Yellow
Green
Blue

In above example, setColor() method in the subclass method is changing the fillcolor, not bordercolor. So, fillColor is Green and borderColor is same as blue.

Call superclass method from the overridden method
To call the superclass method from the subclass method, the keyword super is used. Here in the example, the following statement calls the superclass method setColor(). And the output is changed accordingly -
public class Triangle extends Shape {
String
fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}

// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}

public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}

}
4.0
6.0
Yellow
Green
Green

Superclass method can be called from any subclass method
To call the superclass methods, the method does not need to be overridden. Here in the following example,  a new method setDiffColor() is added, it passes two parameters - fillcolor and bordercolor. The superclass method is called to set bordercolor.
public class Triangle extends Shape {
String
fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}

// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}

// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}

// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}

public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}

public void setDiffColor(String fillcolor, String bordercolor) {
this.fillColor = fillcolor;
super.setColor(bordercolor);
}

}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.
out.println(t.width);
System.
out.println(t.height);
System.
out.println(t.fillColor);
t.setDiffColor("Green","Pink");
System.
out.println(t.fillColor);
System.
out.println(t.borderColor);
}
}
Here is the output -

4.0
6.0
Yellow
Green
Pink

All the code can be found at Github Link.