answersLogoWhite

0


Best Answer

False. A method with the same signature in both the superclass and its subclass is known as method overriding, and is a valid concept in Java.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: It is an error to have a method with the same signature in both the super class and its subclass. True or false?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is finalize method in java?

Finalize method is used when a variable becomes unreachable or of no use. it is finalize and garbage collection will free the memory used by it, which can be then reclaimed for some other variable


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.


What is overriding in java?

Providing a declaration which matches another declaration of the same name, thereby hiding the existing declaration.In terms of object-oriented programming, overriding is the ability of a subclass to "override" and replace the functionality of a method.Example:class A {f(){print "A"}}class B extends A {// Function f is overridden.// When B.f() is called, it will call this function instead of A.f()f() {print "B"}}


How does Java decide which method with the same signature to use when the method exists in several levels of a class hierarchy?

Java uses a process called method dispatch, where the most specific version of the method is chosen at runtime. It starts by searching for a matching method in the class where the object is declared. If it finds a match, it will execute that method. If not, it will search the superclasses in the hierarchy until it finds a match or reaches the top of the hierarchy. If multiple methods with the same signature are found at the same level, a compile-time error occurs.


What does the final keyword mean in front of a variable a method a class?

When used in a class declaration, the final keyword means the a class can't be subclassed. In other words, no other class can extend (inherit) a final class, and any attempts to do so will give you a compiler error. The final keyword prevents a method from being overridden in a subclass. Final methods are usually placed in classes where you don't want anyone who is extending your code to meddle with a certain piece of functionality. Declaring a variable with the final keyword makes it impossible to reassign a different value to that variable once it has been initialized with an explicit value -

Related questions

S it an error to have a method with the same signature in both the super class and its subclass?

No; you may decide to change the behaviour of a method in the subclass, or to add additional functionality.


What is finalize method in java?

Finalize method is used when a variable becomes unreachable or of no use. it is finalize and garbage collection will free the memory used by it, which can be then reclaimed for some other variable


Write a Java Program to illustarte the use of abstract methods?

Abstract MethodsAn abstract method is a method that's been declared as abstract but not implemented. In other words, the method contains no code. You mark a method abstract when you want to force subclasses to provide the implementation. (remember the reason why you would want to make a class abstract from the previous paragraph)Ex: public abstract void showSample();Notice that the abstract method ends with a semicolon instead of curly braces. It is illegal to have even a single abstract method in a class that is not explicitly declared abstract! Look at the following illegal class:public class IllegalAbstractClass{public abstract void test();}The preceding class will produce the following error if you try to compile it:IllegalClass.java:1: class IllegalAbstractClass must be declaredabstract.It does not define void test() from class IllegalAbstractClass.public class IllegalAbstractClass {1 errorYou can, however, have an abstract class with no abstract methods. The following example will compile fine:public abstract class LegalAbstractClass{void test() {// you can write lots of code here}}In the preceding example, test() is not abstract. Three different clues tell you it's not an abstract method:• The method is not marked abstract.• The method declaration includes curly braces, as opposed to ending in a semicolon. In other words, the method has a method body.• The method contains actual implementation code.Any class that extends an abstract class must implement all abstract methods of the superclass, unless the subclass is also abstract. The rule is this:The first concrete subclass of an abstract class must implement all abstract methods of the superclass.Concrete just means nonabstract, so if you have an abstract class extending another abstract class, the abstract subclass doesn't need to provide implementations for the inherited abstract methods. Sooner or later, though, somebody's going to make a nonabstract subclass (in other words, a class that can be instantiated), and that subclass will have to implement all the abstract methods from up the inheritance tree.


Can abstract class have constructors?

A constructor of a class in invoked when a object of that class is created. As an abstract class can't have an object, so we can't create a constructor of the abstract class. But we can create a constructor of a concrete subclass of that abstract class and we have to pass the object of that concrete subclass to the abstract class.


What is overriding in java?

Providing a declaration which matches another declaration of the same name, thereby hiding the existing declaration.In terms of object-oriented programming, overriding is the ability of a subclass to "override" and replace the functionality of a method.Example:class A {f(){print "A"}}class B extends A {// Function f is overridden.// When B.f() is called, it will call this function instead of A.f()f() {print "B"}}


How does Java decide which method with the same signature to use when the method exists in several levels of a class hierarchy?

Java uses a process called method dispatch, where the most specific version of the method is chosen at runtime. It starts by searching for a matching method in the class where the object is declared. If it finds a match, it will execute that method. If not, it will search the superclasses in the hierarchy until it finds a match or reaches the top of the hierarchy. If multiple methods with the same signature are found at the same level, a compile-time error occurs.


What does the final keyword mean in front of a variable a method a class?

When used in a class declaration, the final keyword means the a class can't be subclassed. In other words, no other class can extend (inherit) a final class, and any attempts to do so will give you a compiler error. The final keyword prevents a method from being overridden in a subclass. Final methods are usually placed in classes where you don't want anyone who is extending your code to meddle with a certain piece of functionality. Declaring a variable with the final keyword makes it impossible to reassign a different value to that variable once it has been initialized with an explicit value -


What does private mean in java?

Members marked private can't be accessed by code in any class other than the class in which the private member was declared. Let's make a small change to the Parent class from an earlier example. package pack1; public class Parent { private String getName() { return "Parent"; } } Now the getName() method is private and is not visible from within the Child class and the same piece of code would throw up the below compilation error when you try to compile the class. cannot find symbol symbol : method getName() This error method looks as if the method getName() does not exist at all. Of course the method exists but unfortunately it is declared private and hence no class (except the class that has the exact code written into it) can access it. This includes a child class that extends the parent class that has the method.


Can a base class access members of a derived class?

Base class can access members functions of derived class if they were declared as 'virtual' in base class and have same prototype. For this type of access, a pointer to base class is required.class Parent{//data members and constructor herevirtual void Func1(){cout


Difference between public nd default access specifier?

An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:1. Public2. Protected3. Default and4. PrivatePrivate is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.Public Members:If a variable or method is declared public, it means that it can be accessed by anyone or any other class (irrespective of which package they are in). Of course, it is mandatory that the class inside which the public method is placed is visible in the first place for the method or variable to be visible. You can check out the code example in the previous paragraph for an example. The method from Dad class is visible and available for usage inside the son class.For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.Ex:package pack1;public class Parent {public String getName() {return "Parent";}}Package pack2;Import pack1.Parent;Public class Child extends Parent{Public String getParentsName() {return getName();}}If you see the example above, the child class is able to access the parent class's method getName() even without instantiating an object of the parent because it inherits the Parent class and all its method as part of the inheritance (extends) feature.Protected and Default Members:The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.Take a look at the following two classes:package certification;public class ClassOne {void testIt() { // No modifier means method has default accessSystem.out.println("ClassOne");}}In another source code file you have the following:package otherCertification;import certification.ClassOne;class ClassTwo {static public void main(String[] args) {ClassOne o = new ClassOne();o.testIt();}}As you can see, the testIt() method in the first file has default (think: package-level) access. Notice also that class OtherClass is in a different package from the AccessClass. When you compile the ClassTwo.java file you will get an error like below:No method matching testIt() found in classcertification.ClassOne.o.testIt();From the preceding results, you can see that AccessClass can't use the OtherClass method testIt() because testIt() has default access, and AccessClass is not in the same package as OtherClass. So AccessClass can't see it, the compiler complains.Default and protected behavior differs only when we talk about subclasses. If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance. It doesn't matter if the superclass and subclass are in different packages, the protected superclass member is still visible to the subclass. This is in contrast to the default behavior, which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass. (See the example above)Whereas default access doesn't extend any special consideration to subclasses, the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think of package restrictions. No exceptions at all. But when you think protected, think package + kids. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package.


What is protected in Java?

Protected and Default Access Levels:The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.Take a look at the following two classes:package certification;public class ClassOne {void testIt() { // No modifier means method has default accessSystem.out.println("ClassOne");}}In another source code file you have the following:package otherCertification;import certification.ClassOne;class ClassTwo {static public void main(String[] args) {ClassOne o = new ClassOne();o.testIt();}}As you can see, the testIt() method in the first file has default (think: package-level) access. Notice also that class OtherClass is in a different package from the AccessClass. When you compile the ClassTwo.java file you will get an error like below:No method matching testIt() found in classcertification.ClassOne.o.testIt();From the preceding results, you can see that AccessClass can't use the OtherClass method testIt() because testIt() has default access, and AccessClass is not in the same package as OtherClass. So AccessClass can't see it, the compiler complains.Default and protected behavior differs only when we talk about subclasses. If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance. It doesn't matter if the superclass and subclass are in different packages, the protected superclass member is still visible to the subclass. This is in contrast to the default behavior, which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass. (See the example above)Whereas default access doesn't extend any special consideration to subclasses, the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think of package restrictions. No exceptions at all. But when you think protected, think package + kids. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package.


Can you help with arrays in DrJava I have to edit the code for my class at UofP Getting... Error No 'fillArray' method in 'Five' with arguments?

tough luck