Wednesday, June 3, 2015

Polymorphism II








// All reference can be converted to the type Object
// When a superclass variable contains a reference to a subclass object,
// and that reference is used to call method, the subclass version of the method is called.

// why anyone want it --- This can happen if you want to reuse code
// that knows about the superclass but not the subclass

public class PolymorphismDemo2
{
public static void main(String[] args)
{
//Student s = (Student) new Person();
// though above this statement do not shows the compile errot it will shows the runTime error later

Person1 p = new Student1();
Student1 s = (Student1) p;
System.out.println(s);
}
}


class Person1
{
String name, address ;
int age ;
}


class Student1 extends Person1
{
int roll ;
int grade;
}


class GraduateStudent extends Student1
{
int graduateYear;
}



Polymorphism I





// In the real world, every student is a person
// Every object of class Student is also an object of the class Peron
// An object of a class can be referenced by a variable of any ancestor type


public class PolymorphismDemo {

public static void main(String[] args)
{
Person p = new Person();
Student s = new Student();

Person pRef = new Student();          // this is possible
// Student sRep = new Person();       // this is not possible
}
}


class Person
{
String name, address ;
int age ;
}


class Student extends Person
{
int roll ;
int grade;
}



Super (Constructor Behaviour)2



// super() should be the first statement in this case with BB's constructor, otherwise we get compile error

// Any constructor inside a subClass will try to call the default constructor of the superClass by default,
// if there is no default constructor in the superClass, you must use the super() keyword
// in all subClass constructors to explicitly call some other constructor defined in the superClass


public class COnstructorBehaviourWithSuperDemo2
{
public static void main(String[] args)
{
BB b = new BB(22, "Bipen");
}
}


class AA
{
int phNo;

public AA(int phone)
{
phNo = phone;
System.out.println("SuperClass constructor called");
}
}


class BB extends AA
{
String addr;

public BB() {
super(0);  // upper statement
System.out.println("Default Constructor BB");
}
public BB(int phone , String address)
{
super(phone); // super class  variable initialization
addr = address;
System.out.println("Sub Class constructor is called");
System.out.println( "you got vars from super class and subclass constructor:   "+ addr+" "+ phNo);
}
}





Super (Constructor Behaviour)1




public class COnstructorBehaviourWithSuper
{
public static void main(String[] args)
{
A a = new A();
System.out.println();

B b = new B();
System.out.println();

C c = new C();
System.out.println();
}
}


class A
{
public A()
{
System.out.println("Constructor call from A");;
}
}


class B extends A
{
public B()
{
System.out.println("Constructor call from B");;
}
}


class C extends B
{
public C()
{
System.out.println("Constructor call from C");;
}
}



Super Keyord



// Super keyword is used to:
// Call super class methods
// Call super class constructors
// super.methodName() can be call anywhere from the subClass
// it do not have to be first statement


public class SuperDemo {

public static void main(String[] args)
{
Student s = new Student();
s.printInfo();
}
}


class Person
{
String name = "Shyam";
String age = "22";

public void printInfo()
{
System.out.println("Name is : "+ name + " and my age is : "+ age);
}
}


class Student extends Person
{

int mark = 70;

public void printInfo()
{
System.out.println("Exam mark is: "+ mark);
super.printInfo();
}
}


Overriding Methods





// A subclass methods Overrides a superclass method if it has the same name and parameter types as a superclass method
// When such a method is applied to a subclass object, the overriding method, and not the orignal method, is executed.

// If a superclass declare a method to be publicly accessible, you cannot override it to be more private.
// Compiler doesnot allow this, because the increased privacy would conflict with polymorphism
// The compiler reports an error if you override a public method and make it private or give it package access

public class OverridingDemo {

public static void main(String[] args) {

Bycycle b = new Bycycle();
b.printInfo();
}
}


class cycle
{
int weight = 20;

public void printInfo()
{
System.out.println("PrintInfo from cycle class: "+weight);
}
}



class Bycycle extends cycle
{
String brand= "hero";

public void printInfo()
{
System.out.println("PrintInfo from subClass Bicycle: "+ brand);
}
}



Static Methods


// When we define a STATIC METHOD, the method is still a member of a class,
// since you can define it in a class, but the method can be invoked without using any object.
// ClassName.methodName

// You can create a collection of static methods to perform computation that are
// somehow related and group them within a single class

// A NON-STATIC method van reference a static variable or static methods
// A STATIC method can reference a static variable but not an instance variable directly
// A STATIC method cannot call a NON_STATIC variable/method without having an
// instance of a class to use in the invocation.

// Cannot use "this" or "super" keyword in the STATIC methods

// Why main method is static?
// Main method is static because if we do not declare static different class objects will make
// different main method due to which bugs occur so, if we declare main as static it will be the
// common method or action for different classes object    

// When a method performs a task that is independent of the contents of the object, make it static.
// None of the methods in the Math class such as random(), pow(), sin() is dependent
// on specific instance. So these methods are static methods




public class StaticMethodsDemo
{
public static void main(String[] args)
{
Demo.staticString = "changedStaticBpn";
Demo.staticMethod();

Demo d = new Demo();
d.normalString = "changedNormalBpn";
d.normalMethod();

}
}


class Demo
{
String normalString = "NormalBipen" ;
static String staticString = "staticBipen" ;

public void normalMethod()
{
System.out.println("\n------NormalMethod------");
System.out.println(normalString);
System.out.println(staticString);
staticMethod();
}

public static void staticMethod()
{
System.out.println("\n---------StaticMethod--------");
System.out.println(staticString);
// System.out.println(normalString);      
// normalMethod();
// this above two case is not possible but can be posible by creating object

//----------Now by making an object---------
Demo de = new Demo();
// System.out.println(de.normalString);
// System.out.println();
}
}