Subclass constructor, super Keyword

  • Extending a class and then constructing the new extends class will call the constructor of the superclass. This is useful when you want to have a constructor that will call the constructor of the superclass.
public class Superclass {

    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

public class Subclass extends Superclass {

    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}
Subclass.main(null);
Printed in Superclass.
Printed in Subclass

Overloading a method, same name different parameters

  • In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading.
// Overloading by changing the number of parameters
class MethodOverloading {
    private static void display(int a){
        System.out.println("Arguments: " + a);
    }

    private static void display(int a, int b){
        System.out.println("Arguments: " + a + " and " + b);
    }

    public static void main(String[] args) {
        display(1);
        display(1, 4);
    }
}
MethodOverloading.main(null);
Arguments: 1
Arguments: 1 and 4

Overriding a method, same signature of a method

  • Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
// method overriding in java
// Base Class
class Parent {
	void show()
	{
		System.out.println("Parent's show()");
	}
}

// Inherited class
class Child extends Parent {
	// This method overrides show() of Parent
	@Override
	void show()
	{
		System.out.println("Child's show()");
	}
}

// Driver class
class Main {
	public static void main(String[] args)
	{
		// If a Parent type reference refers
		// to a Parent object, then Parent's
		// show is called
		Parent obj1 = new Parent();
		obj1.show();

		// If a Parent type reference refers
		// to a Child object Child's show()
		// is called. This is called RUN TIME
		// POLYMORPHISM.
		Parent obj2 = new Child();
		obj2.show();
	}
}
Main.main(null);
Parent's show()
Child's show()

Abstract Class, Abstract Method

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
//An abstract class can have both abstract and regular methods:
abstract class Animal {
    public abstract void animalSound();
    public void sleep() {
      System.out.println("Zzz");
    }
  }

Standard methods:

  • toString(): Used to convert an object into a string.
  • equals(): Useful for comparing two objects to see if they are equal
  • hashCode(): Useful for getting a unique hash code for an object.
class toString {
    public static void main( String args[] ) {

        //Creating an integer of value 10
        Integer number=10;
        // Calling the toString() method as a function of the Integer variable
        System.out.println( number.toString() );
    }
}
toString.main(null)
10
String myStr1 = "Karthik";
String myStr2 = "Evan";
String myStr3 = "Pranav";
String myStr4 = "Sanjay";
String myStr5 = "Jay";
String myStr6 = "Karthik";
System.out.println(myStr1.equals(myStr6)); // Returns true because they are equal
System.out.println(myStr1.equals(myStr4)); // false
true
false
/*
* Program Demonstrate hashcode() method of Method Class.
*/
import java.lang.reflect.Method;
 
public class hashCode {
    // create a Method name getSampleMethod
    public void getSampleMethod() {}
 
    // create main method
    public static void main(String args[])
    {
        try {
            // create class object for class name hashCode
            Class c = hashCode.class;
            // get Method object of method name getSampleMethod
            Method method = c.getDeclaredMethod("getSampleMethod", null);
            // get hashcode of method object using hashCode() method
            int hashCode = method.hashCode();
            // Print hashCode with method name
            System.out.println("hashCode of method " + method.getName()
                               + " is " + hashCode);
        }
        catch (Exception e) {
            // print if any exception occurs
            e.printStackTrace();
        }
    }
}
hashCode.main(null)
hashCode of method getSampleMethod is 49965842

Late binding of object, referencing superclass object

  • Late binding is when you have allow the compiler to determine which method to use at runtime instead of compile time.
public class lateBinding {
    public static class superclass {
        void print()
        {
            System.out.println("print in superclass.");
        }
    }
  
    public static class subclass extends superclass {
        @Override
        void print()
        {
            System.out.println("print in subclass.");
        }
    }
  
    public static void main(String[] args)
    {
        superclass A = new superclass();
        superclass B = new subclass();
        A.print();
        B.print();
    }
}
lateBinding.main(null)
print in superclass.
print in subclass.

Polymorphism: any of overloading, overriding, late binding

  • Polymorphism is the ability to have multiple methods with the same name but different parameters. This is useful when you want to have multiple methods that do the same thing but with different parameters. This is also useful when you want to have a method that can take multiple types of parameters.
abstract class Animal {
  public void distanceDriven() {
      System.out.println("Anima has walked 120 feet");
  }
}

class Dog extends Animal {
  public void distanceDriven() {
      System.out.println("The SUV has driven 70 feet");
  }
}

class Cat extends Animal {
  public void distanceDriven() {
      System.out.println("The truck has driven 60 feet");
  }
}

// Instantiating the Car class would throw an error as it is abstract
Dog dog = new Dog();
Cat cat = new Cat();
dog.distanceDriven();
cat.distanceDriven();
The SUV has driven 70 feet
The truck has driven 60 feet

Big O notation for Hash map, Binary Search, Single loop, Nested Loop

  • Implementing Big O notation is useful for determining the efficiency of an algorithm. Making algorithms more efficient is useful for making programs compile and run faster and likely reduce the load of it, which could be especially good for websites where users may be runnning the same function through an api many times.
class Main
{
public static void main(String[] args)
{
	int a = 0, b = 0;
	int N = 4, M = 4;
	// This loop runs for N time
	for (int i = 0; i < N; i++)
	{
	a = a + 10;
	}
	// This loop runs for M time
	for (int i = 0; i < M; i++)
	{
	b = b + 40;
	}
	System.out.print(a + " " + b);
}
}
Main.main(null)
40 160