Skip to main content

JAVA-OOPS: POLYMORPHISM

This post covers the topic Polymorphism

POLYMORPHISM

  • Poly->many
  • Morphism->forms
  • Taking more than one forms is called polymorphism or one task completed by many ways
It has 2 types,
  1. Method overloading (static binding/compile time polymorphism)
  2. Method overriding (dynamic binding/run time polymorphism)

1. Method overloading:

  • Class->same
  • Method->same
  • Argument->differ
  • In a same class method name is same and the argument is different is called method overloading
  • the argument is depends on
    • data types
    • data types count
    • data type order

Example Program:

public class StudentInfo {
    private void studentId(int num) {
    }
    private void studentId(String name) {     \\ depends on order
    }
    private void studentId(String email, int ph) {     \\depends on data type
    }
    private void studentId(int dob, String add) {     \\depends on datatype count
    }
    public static void main(String[] arg) {
        StudentInfo info = new StudentInfo();
    }
}

  • In the same method the argument can't use int and byte because int & byte both are numbers. so it doesn't work.
  • public void employeeID(int num, byte num2) is not correct

2. Method overriding:

  • Class name->differ(using extends)
  • Method->same
  • Argument->same
  • In a different class , the method name should be same and argument name should be same is called overriding

Example Program:

  • 1st class(sub class)
public class Boy extends Marriage {
    public void girlName() {
        System.out.println("ramya");
    }
    public static void main(String[] args) {
    Boy.b=new Boy();
    b.girlName();
}
  • 2nd class(super class)
public class Marriage {
    public void girlName() {
        System.out.println("priya");
}
output : ramya;
  • The same method name in both class it take sub class only
  • If we satisfied with super class we go for super class method but we won't satisfy with super class we go for sub class method
  • We can assign our sub class to our super class but can't reverse

Example Program:

  • Marriage b=new Boy() is possible
  • Boy b=new Marriage() impossible
  • Inside the class if we use static we don't want to crate object (i.e)
public class Employee{
    public static void addNum(){
        System.out.println("Hello");
    }
    public static void main(String[] args){
    addNum(); // don't want to create object
    }
}
Output: Hello
  • If its different class we have to use class name(i.e)
sub class:
public class Employee{
    public static void addNum(){
        system.out.println("Hello");
   }
}
super class:
public class sample{
    public static void main(string[] args){
        Employee.addNum();
    }
}
Output: Hello

Comments

Popular posts from this blog

JAVA-OOPS: ABSTRACTION

This post covers the topic Abstraction  ABSTRACTION: Hiding the implementation part is called abstraction It has 2 types, Partial abstraction (abstract class) Full abstraction (interface) 1. Partial Abstraction (Abstract class): It will support abstract method and non-abstract method. We can’t create object for abstract class because in the method signature we didn't mention any business logic. In abstract method, we only mention abstract signature, won't create business logic It have 2 class, abstract class (sub class) and super class. we create object and business logic only in super class, won't create in abstract class Example Program: abstract class public abstract class Bank {      abstract void saving();           //method signature      abstract void current();      abstract void salary();      abstract void joint();      public void branchDetails(){      ...

Java Interview Questions For Selenium Testers

This post covers Core Java Interview Questions for Selenium Automation Testers. Question 1: Why did you choose Java? Answer:  Java is open source and easy to learn. Writing, Compiling and debugging is easy. We can reuse the code. Question  2: What are all the features of Java? Answer:  Question 3: Why Java is platform independent? Answer:  Java compiler converts Source code to Byte Code. We can run it in any platform such as Windows, Mac, and Linux etc. Question 4: Data types and Its Default Values? Answer: Data type has two types namely, Primitive : (Stores directly) Predefined Can store only one value There are no additional methods Non-primitive : (Stores based on reference) Not-Predefined Can store more than one value It references a memory location which stores the Data. (Reference variables) Default Values: Data Type Memory Size (Byte) Default Value Wrapper Class byte 1 0 Byte Short 2 0 Sh...