Hi From the below code,the example is calling the dog object through the animal reference.But i do not understand why don't it call directly through the dog class as the reference? Thanks class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); // Animal reference but Dog object b.move(); //Runs the method in Dog class } }

Member Avatar
Member Avatar
+0 forum 9

Hi Below is a code sample i copied from the book that i am reading.But i need explanation on why creating a reference to the figure(Eg: Figure figref;) when each reference for the individual class is already been declared. Like if i need the area of the rectangle,i could have just used r.area(),since the reference to class rectangle is already being made. Thanks class Figure { double dim1; double dim2; Figure(double a, double b) { dim1 = a; dim2 = b; } double area() { System.out.println("Area for Figure is undefined."); return 0; } } class Rectangle extends Figure { Rectangle(double …

Member Avatar
Member Avatar
+0 forum 1

I wanted to convert this segment of code into something slightly different package expression; import java.util.*; public class Expression{ /* * Strings used for storing expression. */ String s, x; /* * Term evaluator for number literals. */ double term(){ double ans = 0; StringBuffer temp = new StringBuffer(); while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){ temp.append(Integer.parseInt( "" + s.charAt( 0 ) )); s = s.substring( 1 ); } if( s.length() > 0 && s.charAt( 0 ) == '.' ){ temp.append( '.' ); s = s.substring( 1 ); while( s.length() > 0 && Character.isDigit( s.charAt( 0 …

Member Avatar
Member Avatar
+0 forum 5

In my project, I have created around 12 custom controls(checkbox,radiobutton,editbox,progressbar,scrollbar,button etc...). Custom painting is done in all controls for the extended look and feel using Pen,Brushes,Graphics, GraphicPath,Images,Region and Bitmaps ect. After the use of these objects, I have disposed the objects using the Dispose() method. I am using these controls in a System.Windows Form. When I am contiously showing and closing the Form causes an increase in the GDI objecs each time. I have checked the GDI object count using Task Manager and confirmed the GDI leak in the form. My question is, how can I manage this GDI leak? …

Member Avatar
Member Avatar
+0 forum 3

class Dog { public void bark() { System.out.println("Dog bark"); } } class Hound extends Dog { public void bark() { System.out.println("Hound bark"); } public void sniff() { System.out.println("Hound sniff"); } } public class CastingMethodCall { public static void main(String r[]) { ((Dog)new Hound()).bark(); //((Dog)new Hound()).sniff(); } } On executing above, I get o/p as "Hound bark". So it means the cast of Dog has nothing to play any role that I can safely assume, since new Hound().bark() also gives same o/p of "Hound bark". However, if I remove the comment from next line, i.e. ((Dog)new Hound()).sniff() and then execute program, …

Member Avatar
Member Avatar
+0 forum 3

General rules is that: 1) A private method cannot be overridden. 2) Subclass method should be a lower access modifier than a overridden superclass method. But the output of the program is "I am Son". Why? Is it because of the rule 2 the output comes? If that's the case here, then two rules of overriding I mentioned above are clashing, aren't they? Help please. Thanks.

Member Avatar
Member Avatar
+0 forum 8

Hello. I have this question: "Amend the subclass of Holiday called Premier with an overridden equals method. Test this method in an orchestrating class with objects of Premier that contain the same data values and with objects that contain different data values." I have searched high and low for a good explanation of overriding and how to achieve it but it refuses to click for me. Could someone please (using my code) explain how I go about achieving the answer to the question. Or point me in the right direction. So far I have as follows: Holiday.java package Holiday; //<--------------------------------------------------------------------------------------------------------------------------------------------------------- …

Member Avatar
Member Avatar
+0 forum 11

[CODE]class Animal { private String type; Animal theAnimal = null; // Declare a variable of type Animal public Animal(String aType) { type = new String(aType); } public String toString() { return "This is a " + type; } } class Dog extends Animal{ private String name; // Name of a Dog private String breed; // Dog breed public Dog(String aName) { super("Dog"); // Call the base constructor name = aName; // Supplied name breed = "Unknown"; // Default breed value } public Dog(String aName, String aBreed) { super("Dog"); // Call the base constructor name = aName; // Supplied name breed …

Member Avatar
Member Avatar
+0 forum 5

Hi, I have seen in couple of places the code where ToString() method has been overriden. Like : class Employee : IComparable<Employee> { public int Salary { get; set; } public string Name { get; set; } public int CompareTo(Employee other) { if (this.Salary == other.Salary) { return this.Name.CompareTo(other.Name); } return other.Salary.CompareTo(this.Salary); } public override string ToString() { // String representation. return this.Salary.ToString() + "," + this.Name; } } What is the need of overriding. I have seen if we dont override we do not get the value instead we get the object only. Why so ? Plz reply if …

Member Avatar
Member Avatar
+0 forum 8

Hello, I have a question about the code below [CODE] // Checks the vertex a if (this.a != ((Triangle)o).a) { return false; } [/CODE] So In a method called Triangle i declared an instance variable like this: [CODE] private java.awt.Point a [/CODE] What I think this code is doing is that this.a is comparing its point value (a.x and a.y) and checking to see if they are equal to that of the o Triangle point (o.a.x and o.a.y) is my interpretation correct?

Member Avatar
Member Avatar
+0 forum 1

[code] public class Animal { public void eat() { System.out.println("I eat like a generic Animal."); } public static void main(String[] args) { } } class Fish extends Animal { @Override public void eat() { System.out.println("I eat like a fish!"); } } class Goldfish extends Fish { @Override public void eat() { System.out.println("I eat like a goldfish!"); } } [/code] In this example how to call the 'eat' method of the Animal class using an object of class GoldFish? [code] GoldFish gfobj = new GoldFish(); gfobj.eat();//i'm expecting this line to call the eat method in the Animal class [/code] however i'm …

Member Avatar
Member Avatar
+0 forum 1

Hi, Please help me tod this.. how can i change Overriding The Default Text color in IE browser? I can change in ff, and Safari. but Not in this.. help me. thanks in advance. jack.

Member Avatar
Member Avatar
+0 forum 4

The End.