announcements‎ > ‎

2015-10-21

posted Oct 21, 2015, 10:51 AM by Samuel Konstantinovich   [ updated Oct 21, 2015, 10:52 AM ]
Happy Back To The Future Day!!!!!

/*cool booleans*/
public boolean endOther(String a, String b) {
  return a.substring(
  Math.min(a.length(),Math.abs(a.length()-b.length()))).equalsIgnoreCase(b) || 
  b.substring(
  Math.min(b.length(),Math.abs(b.length()-a.length()))).equalsIgnoreCase(a);
}

//OR!
public boolean endOther(String a, String b) {
return a.toLowerCase().endsWith(b.toLowerCase()) ||
       b.toLowerCase().endsWith(a.toLowerCase());
}

Super / Parent / Base Class 
Sub / Child / Derived class
*note base and derived, base sounds like a "lower" level, but it is more of a foundation*

Private fields and Inheritance:
-private members are not inherited to sub classes.

constructors:
-Constructors are not inherited to sub classes. But, they are executed while instantiating a sub class with a implicit call to super() if there is no other call to super in your constructor.

What happens if both, super class and sub class, have a field with same name.?
-Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.

Are static members inherited to sub classes?
-Yes, Static members are also inherited to sub classes.

/*************************************************************************************/
//DEMO1: What is the output of running the main?
class A {
    public int i;
    public void display() {
        System.out.println(i);
    }
}    
class B extends A {
    public int j;
    public void display() {
        System.out.println(j);
    }
}    
class inheritance_demo {
    public static void main(String args[])
    {
        B obj = new B();
        obj.i=1;
        obj.j=2;   
        obj.display();     //2
        ((A)obj).display(); // still 2
    }
}









/*************************************************************************************/
//SAMPLE CODE FOR QUESTION 1
public class Superclass {
    private void doSomething(){System.out.println("from Superclass");}
    public static void main (String[] args){
        Superclass obj = new Subclass();
        obj.doSomething(); //prints "from Superclass"
    }
}

class Subclass extends Superclass {
    private void doSomething(){System.out.println("from Subclass");}
}

QUESTION1:
//Which of these is not allowed? Why?
Superclass s1 = new SuperClass();
Superclass s2 = new SubClass(); 
Subclass sub1 = new SuperClass(); //Super does not have all of the properties of the Sub this is NOT valid.
Subclass sub2 = new SubClass();

/*************************************************************************************/


Comments