Java example featuring encapsulation, polymorphism, and inheritance?

12,047

Solution 1

Your polymorphism example is merely method overloading and that's not actually what the Object Oriented folks mean by polymorphism. They mean how you can have a interface that exposes a method, and the various classes that implement that interface can implement the method to have different behaviors.

See this. Last paragraph of the introduction in particular.

Also, I would suggest that the best way to demonstrate knowledge of polymorhpism in code would necessarily include some client code that uses the polymorphic objects to demonstrate that they can have different, i.e. poly, behaviors.

Solution 2

A couple of things:

  • Getter method should return values.
  • Overloading is different from overriding. You probably want to override setVal() (or maybe some other more appropriate method) in your child class in order to demonstrate polymorphism.

See the below example. Parent implements fooMethod(). Parent then overloads fooMethod() by adding fooMethod(String str). Think of these as two separate, completely unrelated methods -- they just happen to have very similar looking names. Overloading doesn't have anything to do with polymorphism.

Then Child extends Parent. Child initially inherits fooMethod from Parent, but it wants different functionality when fooMethod() is called. So Child overrides fooMethod() with its own implementation. Now when an object of Child calls fooMethod(), the Child version of fooMethod() will run, printing "bar", not "foo".

public class Parent {
  public void fooMethod() {
    System.out.println("foo");
  }

  public void fooMethod(String str) {  // overloading Parent.fooMethod()
    System.out.println("foo " + str);
  }
}


public class Child extends Parent {
  public void fooMethod() {
    System.out.println("bar");  // overriding Parent.fooMethod()
  }
}

Solution 3

your example of overriding is incorrect (as in, does not demonstrate polymorphism).

you show two functions with different signatures (the types of the arguments are part of the function signature). just because they have the same name does not make them an example of overriding.

overriding would be if class child had something like

public void setVal(int x){
    a = x+10;
}

which would override the setVal(int) method in its super (parent) class.

however a better way to demonstrate polymorphism would be something along the lines of

Parent guy = new Child();
guy.getVal();

Solution 4

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Parent_1
{
    private int i;//encap
    private int j;

    public  void display() {            
        System.out.println("these are the 2 answer");
    }
}

class child extends Parent_1   //inher
{
    public void display()  //method overiding
    {
        System.out.println("this is for method overriding");
    }

    public void mul(int i, int j)
    {
        int k=i*j;
        System.out.println("mul of 2 int val is:"+k);
    }

    public void mul(double i,double j)  //poly
    {
         double z=i*j;
         System.out.println("mul val of 2 double is:"+z);
    }
}

class Son
{
    public static void main(String args[])
    {
         Parent_1 p=new Parent_1();

         Parent_1 pt=new child();
         child cd=new child();
         p.display();
         cd.mul(2, 20);
         cd.mul(2.2, 1.1);
         pt.display();
    }
}
Share:
12,047
user2048643
Author by

user2048643

Updated on June 04, 2022

Comments

  • user2048643
    user2048643 almost 2 years

    I need to produce a project featuring the listed characteristics of object oriented programming using Java.

    Could someone look over my quick sample program to confirm that I understand how these characteristics are implemented and that they are all present and done correctly?

    package Example;
    
    public class Parent {
    
        private int a;
        public void setVal(int x){
            a = x;
        }
        public void getVal(){
            System.out.println("value is "+a);
        }
    }
    
    public class Child extends Parent{
    
        //private fields indicate encapsulation
        private int b;
        //Child inherits int a and getVal/setVal methods from Parent
        public void setVal2(int y){
            b = y;
        }
        public void getVal2(){
            System.out.println("value 2 is "+b);
        }
        //having a method with the same name doing different things
        //for different parameter types indicates overloading,
        //which is an example of polymorphism
        public void setVal2(String s){
            System.out.println("setVal should take an integer.");
        }
    }
    
  • user2048643
    user2048643 over 11 years
    This makes it much clearer; I understand the difference now. Thanks!