Public vs. Protected abstract class method [duplicate] - java

This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 7 years ago.
Is there any security/access difference when making a package access level abstract class's non-static methods public vs making them protected? Only classes from within the same package that extend the abstract class can access the non-static methods anyway right? So, does it matter whether those non-static methods are public or protected since the abstract class itself places restrictions on who can extend it?
abstract class MyClass {
protected void myFunction(){
System.out.println("Only child classes can print this");
}
}
abstract class MyClass {
public void myFunction(){
System.out.println("Still, only child classes can print this");
}
}

The public abstract method will be accessible in the other package where as the protected abstract method can not be accessed. Check the example below.
An abstract class with both public and protected abstract methods
package package1;
public abstract class MyClass {
abstract protected String method1();
abstract public String method2();
}
Another package which extends the class and implements the abstract class.
package package2;
import package1.MyClass;
public class MyClassImpl extends MyClass {
#Override
protected String method1() {
return "protected method";
}
#Override
public String method2() {
return "public method";
}
}
Main class for accessing the abstract method.
package package2;
import package1.MyClass;
public class MainClass {
static MyClass myClass = new MyClassImpl();
public static void main(String[] args) {
System.out.println(myClass.method1()); // This is compilation error.
System.out.println(myClass.method2());
}
}

Related

Static member method to Instance member method redeclaration allowed for Interface-Class but not for Class-Class or Interface-Interface [duplicate]

This question already has an answer here:
Why can I override a static interface method?
(1 answer)
Closed 1 year ago.
Consider the following snippet:
interface Super{
public static void doIt(){}
}
class Sub implements Super{
public void doIt(){}
}
Here I have declared a static member method and it is being redeclared as the instance method in the child class.
This is allowed by the compiler - but when I do the same with the superclass and subclass types - this gives compilation error:
class Super{
public static void doIt(){}
}
class Sub extends Super{
public void doIt(){}
}
What is the rationale behind the same? - ideally the way to access the subclass is essentially the same - then why this restriction?
The reason for this is that Java allows invoking static methods in a non-static manner. Consider this example:
public class MyClass {
public static void sayHello() {
}
public void test() {
this.sayHello();
}
}
This will produce a compiler warning (The static method sayHello() from the type MyClass should be accessed in a static way), but it will compile and invoke the static method correctly at runtime.
That's why the following code will not compile because of an ambiguity (Duplicate method test() in type MyClass):
public class MyClass {
public static void test() {
}
public void test() {
}
}
The reason for the compile error is, that if you write the following, the compiler cannot know which method to invoke, because it allows to call static methods in a non-static manner:
public class MyClass {
public static void test() {
}
public void test() {
}
public void execute() {
this.test();
}
}
For the same reason it is not possible to have the static test() method in the parent class - the same rules apply. It is possible in Java to call static methods of super classes with or without qualification:
public class Super {
public static void test() {
}
}
public class Sub extends Super {
public void execute() {
this.test();
}
}
OR
public class Sub {
public void execute() {
test();
}
}
where the invocation of this.test() will produce a warning, but will work at runtime.
For static methods in interfaces, the above example will not work, because the compiler forces you to call the static method of the interface in a qualified manner (edit: because they are not being inherited). The following will not work (The method interfaceStatic() is undefined for the type Sub):
public interface Interface {
public static void interfaceStatic() {
}
}
public class Sub implements Interface {
public void test() {
interfaceStatic();
}
}
In order to call interfaceStatic() the invocation has to be qualified like this:
public class Sub implements Interface {
public void test() {
Interface.interfaceStatic();
}
}
That's the difference between defining the static method in an interface and defining it in the super class: The way of invocation. If you implement multiple interfaces which all have a static method with the same signature, the compiler cannot know which one to call.
This is the reason why it is allowed to define a static method with the same signature in an implemented interface, but not in the parent classes.

How to use a non abstract method from a Abstract class in another class without extending

How to use a non-abstract method from an abstract class in another class without extending?
Abstract Class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
The anonymous class:
package com.test;
public class Example {
public static void main(String[] args) {
new Example().something();
}
void something() {
MyAbstract a = new MyAbstract() {
#Override
public void abstractMethod() {
//TODO implement
}
};
a.callNonAbstractMethod();
}
}
Internal class generated by the compiler would be for above example.
static class Example extends MyAbstract
{
Example(){}
void abstractMethod()
{
System.out.println("hiee");
}
}
Abstract Class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
You can't.
To call any non-static method of some class A, you need an instance of A or of a subclass of A, as such a method typically operates on data within such an instance. That's at the very core of what "object-oriented" is all about.
In your case, A is abstract and can't have direct instances. So the only way to call your method is to have some instance of some class B that extends A. You can either find an existing subclass that you can use, or create your own subclass.
I think you can use an anonymous class. Although it is a kind of extension, you are not explicitly using the keyword extends. In fact, you cannot use any class in java without implicitly extending because every class extends Object.
package com.test;
public class Example {
public static void main(String[] args) {
new Example().something();
}
void something() {
MyAbstract a = new MyAbstract() {
#Override
public void abstractMethod() {
//TODO implement
}
};
a.callNonAbstractMethod();
}
}
and here's your abstract class:
package com.test;
public abstract class MyAbstract {
public abstract void abstractMethod();
public void callNonAbstractMethod() {
System.out.println("Hello");
}
}
results in:
Hello

Implementing interface to a abstract class Java

I was testing a program in which i was trying to implement a interface on abstract class . as given below
interface Inf{
void display();
}
abstract class InfTst implements Inf{
}
class InterfaceTest extends InfTst{
void display(){
System.out.println("Hello");
}
}
but it is shows error
error: display() in InterfaceTest cannot implement display() in Inf
void display(){
what is this error means and how resolve it, please help me.
When you omit access modifier on an interface it defaults to public, but on concrete classes it defaults to package-private.
Change your method signature to below in concrete class InterfaceTest
public void display(){..}
In concrete classes by default package is private. A concrete method means, the method have complete definition so must be your method modifier are always public.
package domain;
interface Inf {
void display();
}
abstract class InfTst implements Inf {
}
class InterfaceTest extends InfTst {
public void display() {
System.out.println("Hello");
}
}
public class StackOverFlow extends InterfaceTest {
public static void main(String[] args) {
StackOverFlow sof = new StackOverFlow();
sof.display();
}
}
output : - Hello

Could we call super.someMethod() in derived class from an abstract class?

Could we call super.someMethod() in derived class from an abstract class?
for in stance:
abstarct class TestBase{
void nonabstractMethod(){
....
}
}
Then derived class:
class Child extend TestBase{
void callFunction(){
}
void nonabstractMethos(){
super.nonabstractMethos();
}
}
I assume this can be done. But if we have an abstract method then it cannot be called because of no implementation, am i correct?
The short answer: yes.
You can always call a public or protected super method. Like any (instance) method in java, it will be handled polimorphicly, and a concrete implementation will be called, either of the super's class or from the derived class if it overrides it.
Yes you are correct. If you are extending an abstract class having abstract method, you can't call super.thatMethod();
Consider the following example
public class RSAService {
protected void doRSA(){}
}
class MyService extends RSAService{
public void myService(){
super.doRSA(); //Works fine
}
}
This will work as the doRSA() is accessible from the MyService. Same for public but not for private
But
public abstract class RSAService {
protected abstract void doRSA();
}
class MyServe extends RSAService{
public void myService(){
super.doRSA(); //This won't work
}
#Override
protected void doRSA() {
}
}
Now consider this case, where you can call the super.superClassMethod() from your subclass
public abstract class RSAService {
protected void doRSA(){}
}
class MyService extends RSAService{
public void myService(){
}
#Override
protected void doRSA() {
super.doRSA();
}
}
So if you are overriding a super class method you can call the method using super. Consider this Java Specification link for more clarification
Your example will work if both classes are in the same package.
If that is not the case, then you should declare the method protected or public, something like:
abstract class TestBase{
protected void nonabstractMethod(){
....
}
}
If your method is abstract, then you can't call it, for example:
abstract protected void abstractMethod();

Overriding of Abstract Method in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Abstract method's property is that it doesn't have body, and so when concrete class extends abstract class, it must implement that abstract method.
So my question is how can an abstract method can be overridden when it doesn't have a body? Isn't it implemented, not overridden?
Abstract class with an abstract method.
The method is also marked with abstract and has no body.
public abstract class A {
public abstract void method();
}
Concretization in the class B that extends abstract class A. Also, the method loses the abstract keyword and gets the body.
public class B extends A {
#Override
public void method() {
System.out.println("Hello");
}
}
public abstract class BaseClass {
public abstract void doSomething();
}
public class ConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hi!");
}
}
public class AnotherConcreteClass extends BaseClass {
#Override
public void doSomething() {
System.out.println("Hello!");
}
}
The implementation of the doSomething method that gets executed will depend on the runtime type of the object you are calling doSomething upon.
Just by providing an implementation for the method , provide it with a body.
public abstract class Base {
public abstract void someMethod(); // no implementation here
}
public class Sub extends Base {
#Override
public void someMethod() {
//It is Overridden
}
}
public abstract class A {
public abstract String getValue();
}
public class B extends A {
#Override
public String getValue(){
return "I'm Overriden";
}
}
It's the same like in any other class.
When you are implementing a superclass method in subclass, it will override. So Even if the superclass method is abstract, if you implement it in subclass, then it will override it.
So,
public class SuperClass{
public abstract void someMethod();
}
public class SubClass{
public void someMethod(){
System.out.println("subclass method");
}
}
Foo.java
public abstract class Foo {
public abstract void doStuff();
}
Bar.java
public class Bar extends Foo {
#Override
public void doStuff() {
// doStuff
}
}
Abstract method can be overridden just like overriding other normal methods, by providing an implementation.
This is the same as any other method override:
public abstract class AbstractClass {
public abstract void abstractMethod();
}
...
public class ConcreteClass {
#Override
public void abstractMethod() {
....
}
}

Categories