Skip to main content

Can't stop procrastinating. (Second Partial Questions Review)

4 min read

After about 13 hours of trying to start studying i finally managed to do so. Here's a little study guide i made from the help of the Internet and other people's blogs: (INHERITANCE AND POLYMORPHISM ARE ON MY 1ST PARTIAL STUDY GUIDE POST)

Delegation: When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the quest on, you say that the object has forwarded responsibility for handling the request to the second object.

CRC Cards: Class, Responsibilities, collaborators. Although CRC cards were originally introduced as a technique for teaching object-oriented concepts, they have also been successfully used as a full-fledged modeling technique.

Class: represents a collection of similar objects.

Responsibilities: something that a class knows or does.

Collaborators: another class that a class interacts with to fulfill its responsibilities

Overloading (compile time) and overriding (runtime): The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required.

OVERLOADING: Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters.

Also a valid case of overloading, since the   //parameters are of different types:   

int changeDate(float Year) ; 

int changeDate(int Year); 

OVERLOADING EXAMPLE

//A class for adding upto 5 numbers

class Sum {

    int add(int n1, int n2) {

        return n1+n2;

    }

    int add(int n1, int n2, int n3) {

        return n1+n2+n3;

    }

    int add(int n1, int n2, int n3, int n4) {

        return n1+n2+n3+n4;

    }

    int add(int n1, int n2, int n3, int n4, int n5){

        return n1+n2+n3+n4+n5;

    }

    public static void main(String args[]){

         Sum obj = new Sum();

         System.out.println("Sum of two numbers: "+obj.add(1, 2)); //3

         System.out.println("Sum of three numbers: "+obj.add(1, 2, 3)); //6

         System.out.println("Sum of four numbers: "+obj.add(1, 2, 3, 4)); //10

         System.out.println("Sum of five numbers: "+obj.add(1, 2, 3, 4, 5)); //15

    }

}

Here we have 4 versions of same method “add” which means you’re overloading the method add().

OVERRIDING: An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

OVERRIDING EXAMPLE

class Car{
    public int speedLimit(){
        return 100;
    }
}
class lilCar extends Car{
    public int speedLimit(){
        return 150;
    }
    public static void main(String args[]){
         Car obj = new lilCar();
         int num= obj.speedLimit();
         System.out.println("Speed Limit is: "+num); //150
    }
}

Here speedLimit() method of class lilcar is overriding the speedLimit() method of class CarClass.

  • What do you feel are the benefits to object-oriented programming?
  • Why do you think your change to object-oriented programming is difficult or what makes it difficult for the standard student?  

These two questions are more subjective so i'll let them be. 

Usefull Links: Delegation    CRC Cards    Overloading and overriding      More overloading and overriding

 

Gilberto Rogel García a01630171