Abstract Class in Java

Can you give an example of why exactly a Java abstract method cannot exist in a non-abstract class?

One of the key differences between an abstract class and a non-abstract class is the fact that you cannot create an instance of an abstract class. This prevents a situation where a method with no definition gets called.

So if we had the following abstract class:

abstract class Elephant {    // abstract class

public String getName() {
return "Ollie";
}

public abstract void walk(); // abstract method

}

Then the following would be illegal:

Elephant e = new Elephant();   // error

Because Elephant is abstract and it cannot be instantiated.

But say for argument sake, that we make Elephant non-abstract but it is allowed to still have the abstract method walk(). i.e. we change the definition of Elephant to:

class Elephant {    // non-abstract class

public String getName() {
return "Ollie";
}

public abstract void walk(); // abstract method

}

Now what should happen if I do the following:

Elephant e = new Elephant();   // legal since Elephant is non-abstract
e.walk(); // oops, we didn't define this anywhere

Java compiler won't allow this. You could argue that there are other ways to handle a situation like this, but this simply how the Java language has decided to implement the feature.

Abstract class in Java 5 or previous

Oracle publishes the JLS going back to Java 6: https://docs.oracle.com/javase/specs/ I'm not sure that JLS for Java 5 is available online.

That said, you're question has the same answer regardless of which Java version is considered:

Normal classes may have abstract methods (§8.4.3.1, §9.4), that is,
methods that are declared but not yet implemented, only if they are
abstract classes.

This means: a class that has an abstract method must be declared abstract otherwise you'll get a compilation error.

Abstract Classes & Methods In Java

Your approach is correct.
For clarification purposes, whenever you are overriding isSimilar method, you can you this keyword to shows that the comparison will take place between the brand of the device passed and the object which is calling
the method. your implementation of isSimilar is also correct but you can optimize this by just keeping it as one liner.

  @Override
public boolean isSimilar(Device d) {
return d.getBrand().equalsIgnoreCase(this.getBrand()) ;
}

Now to compare them, I have created a main class where I am creating the objects of class Server and Network device and calling the isSimilar method.

public class Main 

{
public static void main(String args[]) {
Device deviceA = new Server(1,"samsung","core-4","ten");

Device deviceB = new Server(1,"samsung","core-4","twenty");

System.out.println(deviceA.isSimilar(deviceB));


Device deviceC = new NetworkDevice(1,"samsung",4,false);

Device deviceD = new NetworkDevice(1,"galaxy",6,true);

System.out.println(deviceC.isSimilar(deviceD));

}
}

and the output is as follows

true
false

Can an abstract class have a constructor?

Yes, an abstract class can have a constructor. Consider this:

abstract class Product { 
int multiplyBy;
public Product( int multiplyBy ) {
this.multiplyBy = multiplyBy;
}

public int mutiply(int val) {
return multiplyBy * val;
}
}

class TimesTwo extends Product {
public TimesTwo() {
super(2);
}
}

class TimesWhat extends Product {
public TimesWhat(int what) {
super(what);
}
}

The superclass Product is abstract and has a constructor. The concrete class TimesTwo has a constructor that just hardcodes the value 2. The concrete class TimesWhat has a constructor that allows the caller to specify the value.

Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum fields required to setup the class.

NOTE: As there is no default (or no-arg) constructor in the parent
abstract class, the constructor used in subclass must explicitly call
the parent constructor.

Java / Abstract Class

You can make one step in between. Make an abstract Class implementing the method and derive from it for C and D

abstract class A {
public abstract void method();
}

abstract class CD extends A {
void method(){print "Bye";}
}

class B extends A {
void method(){print "Hello";}
}

class C extends CD { }

class D extends CD { }


Related Topics



Leave a reply



Submit