I am doing astrophysical research. I wrote a package containing the classes Star, Band, and Datfile. I also have the enumerated type of BandName. Each star contains several Bands, each Band ... |
|
A reference variable can be assigned a reference to any subclass derived from superclass but the reverse in not true . Why ?? Class first {.....} Class second extends first {...........} Class mainone { public static void main (string[] args) { second sec = new second(); first fir = new first(); sec = fir //Error fir=sec ; // Works } } ... |
Hi Hamsagayathri, inheritance and delegation are both ways to add some functionality to a class. The major difference is that your class is more tightly coupled to a base class if you use inheritance. Then your extending class belongs to the class hierarchy forever. This can be good or not so good depending on the situation. Delegation means that your class ... |
Originally posted by j srinivas: i have "A" class in which i define two methods add()and substract(). I have another class "B" in which i declared another two methods multiply()and divide(). Now i have a class "C",in which i want to use all the four methods. Whether it is possible or not. In java you can not inherit more ... |
I got this question while surfing. The answer to this question has been given below: Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance ... |
is it possible to reduce number of parameter passed to extended class than number of parameter in base class e.g if class tree has 3 parameters in constructor & its extended class Pine has 2 parameters 1 is from base class(through super) and 1 new parameter. class Tree{ String name; int no; int height; Tree(String a,int b,int c) { name=a; no=b; ... |
|
I am very much confused with logic behind typecasting of super class object to subclass and vice versa which is allowed and which is not allowed. after type casting if the overridden method is invoked which one will be invoked and WHY.can someone hel me by explaining or give some articles to go through |
|
class Cat { int x,y; Cat() { } public Cat(int x,int y) { this.x=x; this.y=y; } } class Rat extends Cat { /* Rat() { //super(4,2); }*/ public void sum() { System.out.println(x); int a=x+y; System.out.println("Sum="+a); }//sum void sub() { int b=x-y; System.out.println("Sub="+b); }//sub void mult() { int c=x*y; System.out.println("Mul="+c); } }//class Rat class CatRat { public static void main(String args[]) { ... |
I am a little confused regarding the exact meaning of the word "inheritence". If private members are not inherited, then how can they be accessed after doing a cast to the superclass? class A { private int a = 10; public void foo(A q) { System.out.println(q.a); } } class B extends A { public static void main(String[] args) { B b ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |
Hi, I just was reading SunJava tutorial for refreshing and a few questions came to my mind. Before I post my questions, let me give a you an excerpt from the Sun Java tutorial (from their website) on which my questions are based upon. In the Language basics chapter, Sun says that every class implicitly extends the Object class (i.e. Object ... |
|
|
Hidear I would like to add some more. it is called maultilevel inheritence which you are explnig exctly. when we say multiple that means extends morethen oneclass. as you may see in C plus plus. if we extend two class to one single class by extending 2-times then it is not not fair. [ April 21, 2008: Message edited by: Kuldeep ... |
Hi All, I have a question here I've been meaning to ask for sometime re' inheritence and when to extend another class. This arose when I have been working on a JSF (+ spring/jpa) based project and I got to thinking that I should be extending my classes away from the data model to incorporate properties that are only applicable to ... |
Ravi Kiran V wrote: A Dog is an Animal. Not all Animals are Dogs. still in some sort of confusion .Can anybody please help me on this . The core issue here is typesafety. If a reference assignment can be guaranteed to work properly already at compilertime it's said to be typesafe. If you have to wait until runtime before you ... |
thanks for your replies but I still dont get it. if Dog extends Animal. If you write: Dog d = new Dog(); and Animal d = new Dog(); and then call a method d.eat(); the result will be the same. I understand like polymorphic arguments. and i understand for example that if you had an animal array: Animal[] a = new ... |
|
I am facing a problem.I know that JAVA doesn't allow multiple inheritence.But I am need to do something like that.But how to???? Please check this and the question is written below... Java Code: class databaseAccess extends javax.swing.JFrame { static JLabel jTextField1; static JLabel jTextField2; static JLabel jTextField3; static JLabel jTextField4; static JTextField jTextField5; static JTextField jTextField6; static JTextField jTextField7; static JPasswordField ... |
The ability to call a method in an object has nothing to do with it's return value, just if the caller has visibility to the object's method or not. object B is a subclass of A, from B you can say A.method(argument list) if the method is overridden in B, then you can still do A.method(argument list) If you don't have ... |
ok here's my question, I am working inside a sub class and i need to initialize the variables defined by the superclass. ( i think thats what its called) I know i need to call the constructor of that class, but I don't know how. Can somebody point in the right direction or give an example of doing this? Edited by: ... |
hi all regarding multiple inheritnce in java using interface. Consider the following implementation (please consider all the classes to be in same folder) // code for interface inter1 public interface inter1 { public void disp(); } //code for class a, implementing inter1 public class a implements inter1 { public void disp() { System.out.println("I am inside a"); } } //code for class ... |
Usually explicitly stating the super() has always worked better than letting the Java do it automatically. Because more often then not you will be working with a constructor with input arguments that the compiler can't do it for you. Too true... in fact we've recently adopted the policy that "using an auto-super constructor" is a defect, and have removed all noarg ... |
I don't understand this. It seems to me that everything is inherited from the superclass. And as to the private members, well, they are too, but the subclass just cannot see them, run them, but nonetheless, their functionality is in the subclass, right? It seems there is a lot of double talk on this issue. The books say "private members not ... |
|
|
|
|
In C++ class A: public B{}; but in Java class A extends B{} so in C++, i m deciding which members to actually inherit through the access specifier along B, but in Java i aint providing any, i read in a book, which says, all members of the base class are inherited, irrespective of whether they are even private, but in ... |
The reason why Java does not support multiple inheritance is because of the case of shape of deadly diamond. This happens in this situation: Class B has a method doStuff() which it inherits from class A, and class C has the same method inherited from class A. If we make a class D which extends class B, and class A then ... |
And I might add that, while thowirng that Exception on that operation is indeed restricting the behaviour, I would consider it quite bad design. A subclass should be able to do everything the superclass says it can. So the technical implementation of inheritance allows you to create such a restriction, the concept of inheritance though doesn't. In other words: not everything ... |
1. Member variables should be private, not protected. 2. I don't think that the Person <-- Borrower <-- StudentBorrower/StaffBorrower is an appropriate hierarchy. 3. Assuming you stick with that hierarchy, then provide a c'tor in Borrower that takes the max number of books, and have Student and Staff pass the right value to super() in their respective c'tors. |
|
So I have these two classes, one's called MC, which represents a Multiple-Choice question, and one's called TF, which represents a True/False question. It would make sense to have TF inherit from MC, since TF is simple a MC question with two set options (true and false!). So it would also make sense for the constructor for MC to take a ... |
|
|
|
|
|
can a subclass extend a superclass when both class modifier is public? in doing so i am getting an compile time error. it is saying that class subclass is public so it should be declared in a file named subclass.java. the main method is in superclass so i gave the program name as superclass.java. |
Let me rephrase that a bit. You can explicitly reference the subclass from the superclass, assuming the subclass is around at compile time, in your classpath, etc. What I meant was that, at the time super's c'tor is executing, it's not the superclass or its c'tor that "knows" to call sub's method. There's only one object here--an instance of the subclass. ... |