restricting a class method overridden by few child classes - java

I have a 1 Question asked in 1 Interview
class Birds {
public void fly();
public void eat();
public void sleep();
}
Have several other classes extending it like -
Sparrow extends Birds{
overriding fly();// Sparrow can have its own style to fly
overriding eat();
overriding sleep();
}
Eagle extends Birds{
overriding fly();// Eagle can have its own style to fly
}
Ostrich extends Birds {
overriding fly();// Ostrich could not fly !!!
}
How can I restrict such situation ?? Since parent class doesn't aware of its Child class here.
Birds class is extended by several classes some could override Some could not be able to override.

You need to use composition:
Define an interface called, say, Flyable which has a fly() method. Sparrow and Eagle implement that interface. Ostrich does not.
Define an interface called Living, say which has eat() and sleep() methods. All your types implement that interface.
You could point out to your interviewer that you could then have an Aeroplane() object that implements Flyable but not Living.

You can simply add FlyingBird and GroundBird classes and make them abstract.
public class Birds {
public void eat() {
// default implementation
}
public void sleep() {
// default implementation
}
}
abstract class FlyingBird extends Birds {
public abstract void fly();
}
abstract class GroundBird extends Birds {
public abstract void walk();
}
class Sparrow extends FlyingBird {
#Override
public void eat() {
// different implementation
}
#Override
public void sleep() {
// different implementation
}
#Override
public void fly() {
// some impl
}
}
class Eagle extends FlyingBird {
#Override
public void fly() {
// some impl
}
}
class Ostrich extends GroundBird {
#Override
public void walk() {
// walk implementation
}
}

Related

Is there any way to access a sub-type method without casting using generic

To access sub-class method down-casting is needed, is there is a way to achieve this using generic without type-casting in same manner.
public class Main {
public static void main(String[] args){
Animal parrot = new Bird();
((Bird)parrot).fly();
}
}
interface Animal{
void eat();
}
class Bird implements Animal{
#Override
public void eat() {}
public void fly(){}
}
public class Main {
public static void main(String[] args){
Animal parrot = new Bird();
parrot.move();
}
}
interface Animal{
void eat();
void move();
}
class Bird implements Animal{
#Override
public void eat() {}
public void move(){fly();}
public void fly(){}
}
It could work with something like this I guess
Interfaces are meant to add methods to implemented classes without defining actual code for this method, meaning that any implemented class will definitely have the same methods but 2 implemented methods with the same name won't necessarily perform the same action.
To explain it with the current thread it would be:
interface Animal {
void move();
}
class Bird implements Animal{
public void move(){
fly();
}
}
class Dog implements Animal{
public void move(){
walk();
}
}
This way, each class will have its own definition of the move method while in main each method will be called by object.move().
This way of doing things allows to go from a code like this
for (object tmp:objList){
if(tmp.class=="Bird")
tmp.fly();
}
else if (tmp.class=="Dog"){
tmp.walk();
}
...
}
to
for (object tmp:objList){
tmp.move();
}
The parrot class should extend the Bird class, then you can call the fly method from a parrot object directly and without need to cast.
public class Parrot extends Bird{
//have some filed or method
}

java interface method removing from subclass

Anyone provide suggestion for below mentioned:
in java8 consider an interface having two methods (eg interface1 ,interface 2)
implementing those to many subclass later i want to remove one method interface1 from one of my subclass without affecting other is any possible solution is there?
If your subclass declares that it implements this interface, then you have no choice but to provide implementations for all methods, or declare the class abstract. If you want a concrete class which however does not functionally implement all methods in the interface, then here is one option:
public interface YourInterface {
void method1();
void method2();
}
public class YourSubClass implements YourInterface {
#Override
public void method1() {
// actually do something
}
#Override
public void method2() {
throw new UnsupportedOperationException("method2() is not supported here.");
}
}
Here while we do implement all methods, we throw a runtime exception should a caller try to access method2().
You can do this by providing a default method implementation for the interface1 method in the interface itself.
interface Interface {
default void interface1() {
System.out.println("interface1");
}
void interface2();
}
class Clazz implements Interface {
#Override
public void interface2() {
System.out.println("interface2");
}
}
Depends how you define 'remove one method'.
If you have interface
interface Interface{
void interface1();
void interface2();
}
And for example two subclasses that extend it:
class Class1 implementes Interface {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
class Class2 implementes Interface {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
Then there are two scenarios:
You don't want to implement for example interface1() method in Class2:
You don't want to have interface1() method in Class2
In case of 1. as Robby Cornelissen mentioned, you can simply provide default implementation in Interface:
default void interface1() { /*do default thing*/ }
In case of 2. you need to remove the interface1() method from the Interface.
You can do that by simple moving definition of method interface1() to Class1 (and any other sublass that needs to have it). but that is not really generic approach.
Best is to extract for example Interface1 with method interface1() and use that interface in classes that need to have that method. You will end up with this situation:
interface Interface{
void interface2();
}
interface Interface1{
void interface2();
}
And for example two subclasses that extend it:
class Class1 implementes Interface, Interface1 {
#Override
public void interface1(){ ... }
#Override
public void interface2(){ ... }
}
class Class2 implementes Interface {
#Override
public void interface2(){ ... }
}

Java - class extending abstract class and implementing an interface having same methods

like in topic. Here's an example:
public abstract class Bird{
public abstract void eat();
public abstract void fly();
}
public interface Flyable{
public void fly();
}
public class Test extends Bird implements Flyable{
public void eat(){
System.out.println("I'm eating");
}
// And what's now?
public void fly(){
}
}
And now, there is main question. What happens. Is an error being throwed, or fly is same for interface and abstract class?
Nothing happens. Just implement your logic inside fly() and be happy.
If the methods have the same signature, everything will be fine. It is also okay to have the implementation in the abstract class or to implement a method which is specified in multiple interfaces of the class.
In Java, a method is identified by its name and its parameters. Consequently, the return type of the implemented method must be compatible with all return types of all specified methods with the same identifier. The same applies to the throw clauses. If the return type or throw clauses of the implemented method are incompatible, you will get a compilation error.
This example is not working:
public interface Flyable {
void eat();
void fly();
}
public abstract class Bird {
public int eat() {
return 500;
}
public void fly() throws StallException {
}
}
public class Eagle extends Bird implements Flyable {
}
Eagle.java, line 1: Exception StallException in throws clause of Bird.fly() is not compatible with Flyable.fly()
Eagle.java, line 1: The return types are incompatible for the inherited methods Flyable.eat(), Bird.eat()

Add a parent to an abstract class so it is no longer abstract

say I have an interface and a class:
public static interface Businessman { public void workHard(); }
public static class CTO implements Businessman {}
What parent should I create for CTO that it will no longer be abstract?
Assuming that you don't want the compiler to complain about CTO not being abstract, you can make a default ("do-nothing") implementation of Businessman from which CTO inherits:
// DefaultBusinessman.java
public class DefaultBusinessman implements Businessman {
#Override
public void workHard() {
}
}
// CTO.java
public class CTO extends DefaultBusinessman {
// CTO does no longer need to be abstract, as the implementation of doWork is provided
// through inheritance
}
You can create CTO as abstract and dont have to implement Businessman
public abstract class CTO implements Businessman {}
public class specificCTO extends CTO {
#Override
public void workHard() {
}
}

Purpose of having abstract child by extending concrete parent

Sometimes, I came across some class design as follow.
abstract class animal {
public abstract void speak();
}
class dog extends animal {
#Override
public void speak() {
// Do something.
}
}
abstract class abstract_dog extends dog {
#Override
public abstract void speak();
}
I was wondering, what is the purpose of having an abstract_dog class? Why we "transform" the non-abstract speak method into abstract speak again?
In case you want to create a base class that forces people to override speak, but inherits Dog.
I agree with SLaks, and think that this would be a real life situation:
abstract class animal {
public abstract void speak();
}
class dog extends animal {
#Override
public void speak() {
// Dog says 'bark'
}
}
abstract class abstract_dog extends dog {
#Override
public abstract void speak();
}
class poodle extends abstract_dog {
#Override
public void speak() {
// poodle says 'yip yip'
}
}
class great_dane extends abstract_dog {
#Override
public void speak() {
// great dane says 'ruff ruff'
}
}
I think you would use this in the case where you want the make a new child class implement the speak method and the dog class may have other methods that the new child class would not have to implement.
Knowing more about your exact situation would help in determining if there is a better design for this scenario.

Categories