Thursday, June 4, 2015
Interface With Polymorphism
package areaAndPerimeter;
public class InheritanceWithPolymorphismDemo
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.breadth = 10;
rect.length = 10;
rect.getArea();
rect.getPerimeter();
System.out.println("The area and perimeter of rectangle is "+ rect.getArea()+" and "+ rect.getPerimeter()+"\n");
Circle circ = new Circle();
circ.radius = 10;
circ.getArea();
circ.getPerimeter();
System.out.println("The area and perimeter of circle is "+ circ.getArea()+" and "+ circ.getPerimeter());
}
}
interface Measurable
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}
class Rectangle implements Measurable
{
double length , breadth ;
@Override
public double getArea()
{
return length * breadth;
}
@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}
class Circle implements Measurable
{
double radius;
@Override
public double getArea() {
return PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * PI * radius ;
}
}
final Keyword
public class FinalClassDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
System.out.println(d.SymbolNo);
d.method1();
}
}
final class Demo
{
final int SymbolNo = 130;
public final void method1()
{
System.out.println(SymbolNo);
}
}
/*
class Demo1 extends Demo{ // we cannot extends final class
}
*/
Polymorphism Dynamic Method Dispatch 2
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DynamicMethodDispatchCOOL
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the blog from where you want to read: \n"+" 1: Site1 \n 2: Site2 \n 3: Site3 \n");
int no = Integer.parseInt(reader.readLine());
Reader blogReader = null;
switch (no) {
case 1:
blogReader = new Site1();
break;
case 2:
blogReader = new Site2();
break;
case 3:
blogReader = new Site3();
break;
default:
System.out.println("Please select valid site no...");
break;
}
if(blogReader != null)
{
blogReader.readFromBlog();
}
}
}
class Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader....");
}
}
class Site1 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site1....");
}
}
class Site2 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site2....");
}
}
class Site3 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site3....");
}
}
Polymorphism Dynamic Method Dispatch
// Polymorphism is the ability to treat object with differences in behavior in a uniform way
// Polumorphism enables us to write programs that process objects that share
// the same superclass(directly or indirectly) as if they're all object of the superclass
public class PolymorphismDynamicMethodDispatch
{
public static void main(String[] args)
{
Animal a =new Human();
a.move();
}
}
class Animal
{
public void move()
{
System.out.println("Animal can Move. ");
}
}
class Human extends Animal
{
public void move()
{
System.out.println("Human can Move. ");
}
}
-----------------------------------------------------------------------------------------------------------------------
public class PolynomialDynamicMethodDispatch2
{
public static void main(String[] args)
{
Animal1 a =new Human1();
a.move();
}
}
class Animal1
{
public void move()
{
System.out.println("Animal can Move. ");
}
}
class Human1 extends Animal1
{
}
// Polumorphism enables us to write programs that process objects that share
// the same superclass(directly or indirectly) as if they're all object of the superclass
public class PolymorphismDynamicMethodDispatch
{
public static void main(String[] args)
{
Animal a =new Human();
a.move();
}
}
class Animal
{
public void move()
{
System.out.println("Animal can Move. ");
}
}
class Human extends Animal
{
public void move()
{
System.out.println("Human can Move. ");
}
}
-----------------------------------------------------------------------------------------------------------------------
public class PolynomialDynamicMethodDispatch2
{
public static void main(String[] args)
{
Animal1 a =new Human1();
a.move();
}
}
class Animal1
{
public void move()
{
System.out.println("Animal can Move. ");
}
}
class Human1 extends Animal1
{
}
instanceof
// instanceof simply means objectof
// referenceVariable instanceOf className/interfaceName
// Test whether the reference variable on the left is an instance/object
// of the specified type(class or subClass or interface) on the right
// An object of subclass type is also a type of parent class
// instanceOf operator with a variable that has null value returns false
// Parent class can never be converted into child but child class can be converted into the parent
public class InstanceOfDemo
{
public static void main(String[] args)
{
Parent p = new Parent();
System.out.println(p instanceof Parent);
System.out.println(p instanceof Child); // Parent can never be the instance of child
System.out.println();
Child cc = new Child();
System.out.println(cc instanceof Child);
System.out.println(cc instanceof Parent);
System.out.println();
Parent p1 = new Child();
System.out.println(p1 instanceof Child);
System.out.println(p1 instanceof Parent);
System.out.println();
Child c = (Child) p1;
System.out.println(c instanceof Child);
System.out.println(c instanceof Parent);
System.out.println();
Parent pp = p1;
System.out.println(pp instanceof Parent);
System.out.println(pp instanceof Child);
System.out.println();
Child c1 = (Child) p; // class cast exception
System.out.println(c1 instanceof Child);
System.out.println(c1 instanceof Parent);
}
}
class Parent
{
}
class Child extends Parent
{
}
Subscribe to:
Posts (Atom)



















