Java experts.Please answer this question

View Answers

May 16, 2008 at 6:30 PM

Hi

class A {
int i, j;
}
class B {
int i, j;
}
class C extends A {
int k;
}
class D extends A {
int k;
}
public class ins {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
D d = new D();
if(a instanceof A)
System.out.println("a is instance of A");
if(b instanceof B)
System.out.println("b is instance of B");
if(c instanceof C)
System.out.println("c is instance of C");
if(c instanceof A)
System.out.println("c can be cast to A");
if(a instanceof C)
System.out.println("a can be cast to C");
System.out.println();
// compare types of derived types
A object;
object = d; // A reference to d
System.out.println("object now refers to d");
if(object instanceof D)
System.out.println("object is instance of D");
System.out.println();
object = c; // A reference to c
System.out.println("object now refers to c");
if(object instanceof D)
System.out.println("object can be cast to D");
else
System.out.println("object cannot be cast to D");
if(object instanceof A)
System.out.println("object can be cast to A");
System.out.println();

}
}

------------------------------------------

Read for more information.

http://www.roseindia.net/java/beginners/nonstaticvariable.shtml




July 24, 2013 at 2:04 AM

package UI;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ColorModel;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;

public class MainUI extends JDialog implements MouseListener {


    /**
     * @param args
     */

    static String[][] templocations=new String[5][];
    static int check1,check2,check3,check4,check5;
    ImageIcon imgicon;
    Image img;
    static JButton butt;
    JLabel addlabel,configure,exit;
    static Container con;
    EnlargeUI largeUI;
    static int count=0;
    static File savelocations = new File("e:\\Dock\\ObjectDock.doc");
    static File savealwaysonTop = new File("e:\\Dock\\alwaysonTop.doc");
    AddFrame temp=new AddFrame(this);
    static String checkTop="";

    public MainUI(){
        con = getContentPane();

        try{
            CreateButton button=new CreateButton("","",this);
            setUndecorated(true);
            setBounds(0, 0, 500, 65);
            setLayout(null);

            try {
                if(!savelocations.exists()){
                    savelocations.createNewFile();
                    // TODO Auto-generated catch block
                }
                if(!savealwaysonTop.exists()){
                    savealwaysonTop.createNewFile();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                Scanner scanFile=new Scanner(savelocations);
                while(scanFile.hasNext()){
                    String[] splitLocations=scanFile.nextLine().split(";");
                    AddFrame.GetID.setText(splitLocations[0]);
                    templocations[Integer.parseInt(splitLocations[0])-1]=new String[2];
                    templocations[Integer.parseInt(splitLocations[0])-1][0]=splitLocations[1];
                    templocations[Integer.parseInt(splitLocations[0])-1][1]=splitLocations[2];
                    AddFrame.GetPath.setText(splitLocations[1]);
                    AddFrame.GetTarget.setText(splitLocations[2]);
                    System.out.println(splitLocations[0]);

                    AddFrame.addShortCuts();
                }
                scanFile.close();
                Scanner scanFile1=new Scanner(savealwaysonTop);
                while(scanFile1.hasNext()){
                    checkTop=scanFile1.nextLine();
                }
                if(checkTop.equalsIgnoreCase("True")){
                    ConfigureUI.alwaysonTop.setSelected(true);
                }else{
                    ConfigureUI.alwaysonTop.setSelected(false);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            addlabel = new JLabel("ADD");
            addlabel.setBounds(400, 5, 50, 25);
            addlabel.addMouseListener(this);

            configure = new JLabel("CONFIGURE");
            configure.setBounds(400, 20, 70, 25);
            configure.addMouseListener(this);

            exit = new JLabel("EXIT");
            exit.setBounds(400, 35, 50, 25);
            exit.addMouseListener(this);

            con.add(addlabel);
            con.add(configure);
            con.add(exit);

            setVisible(true);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new MainUI();

    }


    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getSource()==addlabel){
            AddFrame add = new AddFrame(this);
            add.setVisible(true);
        }else if(arg0.getSource()==exit){
            try {
                FileWriter writetoFile = new FileWriter(savelocations);
                for (int i = 0; i < templocations.length; i++) {
                    if(templocations[i]!=null && !templocations[i][0].equals("")){
                        writetoFile.write((i+1)+";"+templocations[i][0]+";"+templocations[i][1]+"\n");
                    }
                }
                writetoFile.flush();
                writetoFile.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.exit(0);
        }else if(arg0.getSource()==configure){
            ConfigureUI confi=new ConfigureUI(this);
            if(ConfigureUI.Y==1){
                confi.setBounds(arg0.getXOnScreen(), arg0.getYOnScreen()-130, 130, 200);
            }else{
                confi.setBounds(arg0.getXOnScreen(), arg0.getYOnScreen(), 130, 200);
            }
            confi.setVisible(true);
        }
    }


    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }


    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }


    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        //      largeUI.setVisible(false);
    }
}









Related Tutorials/Questions & Answers:
Java experts.Please answer this question - Java Beginners
Java experts.Please answer this question  class A { void get() { System.out.println("A's method"); } } class C extends A{} class D extends...(); } } ------------------------------------------ Read for more information. http://www.roseindia.net/java
java interview question and answer - Java Interview Questions
java interview question and answer   sir, i want one mark java interview question and answers
Advertisements
Clarify my last question's answer - Java Beginners
Clarify my last question's answer  Dear expert, I've tried out... did not specify my question too clearly. Here's my 2 database... question. Do you want to search through id or through the qualification selected
share your answer to this question
share your answer to this question  i need to know what is the TELERIK CONTROL? what is the map suite? what is dunda chart? Also i have to know what are the websites available which give infor regarding this. Let me know how
pls answer these question
pls answer these question  Write a program that prompts the user to input the length and width of a rectangle. Then calculate and prints the rectangleâ??s area and perimeter.   import java.io.*; class rectangle
i cNT GET ANSWER FOR THIS QUESTION
i cNT GET ANSWER FOR THIS QUESTION  The roots of the quadratic equation ax2+bx+c=0, aïâ??¹0 are given by the following formula: x=(-bÃ?±âË?Å¡(b^2-4ac))/2a In this formula. The term b2-4ac is called
Please , can any one answer this Question?
Please , can any one answer this Question?  How to cummunicate one web application to another web application in different places
i need to answer this Question but i can't do it probably
i need to answer this Question but i can't do it probably   use the web to locate the lyrics to the traditional song"The Twelve Days of Christmas" the song contains a list of gifts received for the holiday.the list is cumulative
Plz answer - Java Beginners
Plz answer  Create a Java Bean Class â?? Customer according to the below given class diagram. Customer ------------------------------------ - customerId : int - customerName : String - customerAddress : String - pincode
Interview question link - Java Beginners
Interview question link  Hi, I want JAVA+J2EE interview question and answer please suggesion me link
hi,i'am new to Jsp.Can some one please let me know an answer for this question?
hi,i'am new to Jsp.Can some one please let me know an answer for this question?  how do we upload an xml file and validate them against WIPO DTD
Core Java Interview Question, Interview Question
Core Java Interview Question Page 27  ... be overridden by public method Question: Explain modifier final Answer... Question: Can you change the reference of the final object Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 14       Question: What is clipping? Answer: Clipping..._TO_REPLACE_1 Question: What is a native method? Answer: A native method is a method
Core Java Interview Question, Interview Question
Core Java Interview Question Page 12       Question: What is a transient variable? Answer... Question: Which containers use a border Layout as their default layout? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 18       Question: Is &&= a valid Java operator? Answer: No, it is not. ADS_TO_REPLACE_1 Question: Name the eight
Core Java Interview Question, Interview Question
_TO_REPLACE_3 Question: How are Java source code files named? Answer: A Java source code... Core Java Interview Question Page 16  ...? Answer: Frame ADS_TO_REPLACE_1 Question: How are commas used
Core Java Interview Question, Interview Question
Core Java Interview Question Page 15  ..., or TextComponent Question: What is the GregorianCalendar class? Answer... and redisplayed? Answer: validate() Question: What is the purpose of the Runtime class
Core Java Interview Question Page 1
Core Java Interview Question Page 1       Question: How could Java classes... and the method sleep() is defined in the class Thread. Question: Can you write a Java
Core Java Interview Question, Interview Question
Core Java Interview Question Page 17  ... calendar. Question: What is the Map interface? ADS_TO_REPLACE_8 Answer: The Map... with values. Question: Does a class inherit the constructors of its superclass? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 2   ... of this method? Answer: It's possible if these variables are final. Question: What can... the main difference between a Vector and an ArrayList Answer: Java Vector class
Core Java Interview Question, Interview Question
Core Java Interview Question Page 21  ... to the Throwable type. Question: What are E and PI? ADS_TO_REPLACE_4 Answer: E..._TO_REPLACE_5 Question: What is a void return type? Answer: A void return type
Core Java Interview Question, Interview Question
Core Java Interview Question Page 13       Question: What is an Iterator interface? Answer... of a component?ADS_TO_REPLACE_2 Answer: setBounds() Question: How many bits
Core Java Interview Question, Interview Question
Core Java Interview Question Page 30  ... Question: What happens when a constructor of the subclass is called Answer... Answer: To reference it you will have to use OuterClass$InnerClass Question: Can
Core Java Interview Question, Interview Question
Core Java Interview Question Page 19  ... file system. Question: Can an exception be rethrown? Answer: Yes, an exception... button? Answer: By associating Checkbox objects with a CheckboxGroup. Question
Core Java Interview Question, Interview Question
Core Java Interview Question Page 20       Question: Name four Container classes. Answer..._TO_REPLACE_1 Question: What is the difference between a Choice and a List? Answer
Core Java Interview Question, Interview Question
Core Java Interview Question Page 22       Question: What is a compilation unit? Answer: A compilation unit is a Java source code file. ADS_TO_REPLACE_1 Question: What
Core Java Interview Question, Interview Question
Question: What is the ultimate ancestor of all java classes Answer: Object class is the ancestor of all the java classes Question: What... Core Java Interview Question Page 32   
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Question in Java ??
Question in Java ??   Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number squrfoot
Question in Java ??
Question in Java ??  Welcome every One ,I have Q in Java : Write aprogram that print the falewing table using SQRT method in the Math Class? Number 0 ,2, . . . ,18,20 squrfoot 00000 1.4142 5.2426 5.4721
Core Java Interview Question, Interview Question
Question: What are different types of operators in java Answer: Uniary... Core Java Interview Question Page 26  ... and Boolean Question: Is char type signed or unsigned Answer: char type
java related question
java related question  How can we make a program in which we make mcqs question file and then make its corresponding answer sheet....like if we make 15 mcqs then java should generate it answer sheet of 15 mcqs with a,b,c d
Java Class Question?
Java Class Question?  1. Create an application names Numbers whose... but not display the answer instead it should return the answer to the calling method which displays the answer
Core Java Interview Question, Interview Question
. Question: What is reflection in java Answer: Reflection allows Java code... Core Java Interview Question Page 24  ...(String args[]) method in Java Answer: We need ADS_TO_REPLACE_1 public
Question on java
Question on java  why java is not purely oop?   java is not purely OOP because 1.) Multiple Inheritance is not supported.ADS_TO_REPLACE_1 2.) It uses primitive data type. 3.) static method can be called without
struts interview Question - Struts
struts interview question and answer java  struts interview question and answer java
Question Java>?
Question Java>?  **ïâ??· Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. ïâ??· Enter data for all accounts (account number,name, balance). ïâ??· Add
Question Answer
. If proper answer is not given you can ask question detailing about the previous... Question-Answer Guidelines... Here, you can post your desired question. For posting any question / query
Core Java Interview Question, Interview Question
Core Java Interview Question Page 25  ... interfaces in Java Answer: The null interfaces are marker interfaces...: Does java Support multiple inheritance ADS_TO_REPLACE_1 Answer: Java does
Core Java Interview Question, Interview Question
Core Java Interview Question Page 31       Threads Question: Where does java thread... The java language and virtual machine Question: What is the difference
java question
java question   Create a class called Complex to perform arithmetic operations with complex numbers. 1- Use double variables to represent the fields of the class. 2- Provide a no-argument constructor with default values in case
Core Java Interview Question, Interview Question
Core Java Interview Question Page 29  ... Question: When do we say an exception is not handled Answer... problems Question: What are runtime exceptions Answer: Runtime
Core Java Interview Question, Interview Question
Core Java Interview Question Page 23  ... group. ADS_TO_REPLACE_3 Question: What is a Java package and how is it used... that are loaded by a Java program. ADS_TO_REPLACE_5 Question: How does a try statement
java question
java question  Implement a class called ShortAddress that has the following attributes: firstName, secondName, and phoneNumber. Now implement... details or the long address details. This means your final answer should have
java question :)
java question :)  write java program to use vector in ArrayList with add, remove,sort   import java.util.Vector; public class... Element MUAYAD"); vc.add("Vector Element JAVA"); vc.add("Vector Element
Core Java Interview Question, Interview Question
Core Java Interview Question Page 28  ... Casting of object references Question: When can conversion happen Answer.... Question: What are the rules for Object reference casting Answer: Casting
Core Java Interview Question, Interview Question
Core Java Interview Question Page 4   ... and Unchecked Exceptions in Java ? Answer: All predefined exceptions in Java... | +------------------+ / | | \ \_________________/ unchecked Question: Explain garbage collection ?  Answer: Garbage
Core Java Interview Question, Interview Question
Core Java Interview Question Page 11       Question: What is serialization ?  Answer...; Question: How do I serialize an object to a file ?  ADS_TO_REPLACE_2 Answer
Core Java Interview Question Page 3
Core Java Interview Question Page 3       Question: When you declare a method...-defined exception in javaAnswer: User-defined expections
Core Java Interview Question, Interview Question
Core Java Interview Question Page 9   ... to make a java program as multithred java program? Answer: There are two ways... is in ? Answer: Prior to Java 5, isAlive() was commonly used to test a threads state