In multiple inheritance between abstract class and interface - java

In case of multiple inheritance with abstract class and interface. Abstract class dance() is called.why is the priority given to abstract class method but not default method in interface.
public class Child extends Absclass implements Interfc{
public static void main(String[] args){
Child c = new Child();
c.dance();
}
}
Abstract class Absclass{
public void dance(){
System.out.println("abstract dancing");
}
}
Interface Interfc{
default public void dance(){
System.out.println("interface dancing");
}
}

Related

Optional overloaded methods when overriding

Case :
I have an abstract class A. It contains two methods
abstract void search(Position p);
abstract void search(Animal a);
Class B and C extends A. I would like class B to implement search(Position p) only and class C to implement search(Animal a) only. However when I do so it gives an error, telling me to implement both of the method overloads.
How could I solve the problem? Any help is greatly appreciated
Here are the rules for classes extending Abstract class
First concrete/non-abstract class must implement all methods
If abstract class extends Abstract class, it can but need not implement
abstract methods.
Option 1: Interface Segregation
separate search(XXX) into two abstract classes
Option 2: Generics. Make search a Generic Type
public abstract class ClassA {
public abstract <T> void search(T t);
public static void main(String ...args){
ClassA classA = new ClassB();
classA.search(new Animal());
}
}
class Animal{
}
class ClassB extends ClassA {
#Override
public <Animal> void search(Animal t) {
System.out.println("called");
}
}
Option 3: Interface
public class ClassA {
public static void main(String... args) {
Searchable classA = new ClassB();
classA.search(new Animal());
}
}
interface Searchable {
public <T> void search(T t);
}
class Animal {
}
class ClassB implements Searchable {
#Override
public <Animal> void search(Animal t) {
System.out.println("called");
}
}
Option 4: Throw UnsupportedOperationException Exception(Not recomended)
class ClassB extends ClassA {
#Override
void search(Position p) {
throw new UnsupportedOperationException("Not Supported");
}
#Override
void search(Animal a) {
}
}

Lambda does not override an interface method from a separate file

When the functional interface is in the same file where lambda overrides it, it compiles fine.
package test.test;
public class Base {
public static void main(String[] args) {
Interface1 a = n -> System.out.println(2*n);
}
}
interface Interface1 {
void multiplyByTwo(int x);
}
When the functional interface is in a separate file and Base class implements it, it fails to compile with Base is not abstract and does not override abstract method multiplyByFour(int) in Interface3 error.
package test.test;
public class Base implements Interface3 {
public static void main(String[] args) {
Interface3 b = n -> System.out.println(4*n);
}
}
package test.test;
public interface Interface3 {
void multiplyByFour(int x);
}
Is here something wrong? Why does lambda not override the method in the second case?
Your first example has:
public class Base {
which does not implement Interface1
However, your second example has:
public class Base implements Interface3 {
which DOES implement Interface3
Not sure what you are trying to do here, but this is intended behaviour:
Interfaces
When a class implements an interface, you must implement all of the methods into the class
For example:
public interface IFoo {
void bar();
}
and class:
public class FooImpl implements IFoo {
// must implement bar method in IFoo
public void bar() {
System.out.println("I did something");
}
}
Having a lambda in the main method does not constitute implementing interface methods.
Fix?
Just delete implements Interface3, you don't need to implement the interface in your class to be able to use it.

Why can I instantiate objects of abstract classes and interface? What is the super in constructor?

Here I instantiate objects from two abstract classes and an interface. I wonder why I can do that in three cases, especially the case of NotShape class where there is no abstract method. And the second question is that, What is the "super" when I instantiate the object of NotShape class? Is it belong to Object class or NotShape class itself? I thank you so much.
abstract class Shape{
String descrOfShape = "This is a shape";
abstract void draw();
}
abstract class NotShape {
String descrOfNotShape = "This is not a shape";
void printInfo() {
System.out.println("Can't be measured");
}
}
interface Test{
int ID = 10;
void showResult();
}
public class InstantiateObjects {
public static void main(String[] args) {
Shape s = new Shape() {
#Override
void draw() {
}
};
NotShape ns = new NotShape() {
#Override
void printInfo() {
super.printInfo(); /*What is the super? Is it belong to Object
class or NotShape class?*/
}
};
Test t = new Test() {
#Override
public void showResult() {
}
};
System.out.println(s.descrOfShape);
System.out.println(ns.descrOfNotShape);
System.out.println(t.ID);
}
}
You're not instantiating an abstract class or interface, you're instantiating a private anonymous extension/implementation of the abstract class/interface.
More reading: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Here You implementing interface or abstract class on your main method. You can not instantiate abstract class or interface never. But you can inherit then from another class or interface(depends on inheritance rules). Here you are instantiate a anonymus class which means you are implementing your interface or extending your abstruct classes. And your
#Override
void draw() {
}
and
#Override
void printInfo() {
super.printInfo(); /*What is the super? Ans: super means its call its parent class. Is it belong to Object class or NotShape class? Ans: Obviously its belongs to parent class NotShape*/
}
They are obviously overiding super classes(interfaces and abstruct classes) methods. You can try this then you can see its overiding your printinfo method
NotShape ns = new NotShape() {
#Override
void printInfo() {
super.printInfo(); /*What is the super? Is it belong to Object
class or NotShape class?*/
}
};
ns.printInfo();
That means you are calling your anonymus classes method. and then its calling your super classes because you are calling super.printInfo();
NB: take further study on highlighted terms. Thanks.

creating Abstract class object java

public abstract class AbstractObject {
AbstractObject(){
System.out.println("mahi2");
}
}
class PrintMahi extends AbstractObject{
public static void main(String ... args){
PrintMahi m = new PrintMahi();
}
}
In above code I understand that the PrintMahi object is getting created and because of inheritance superclass which is abstract in this case constructor is getting called. So is it like that the abstract class object is being created here?

calling non abstract method in abstract class java

I have 3 classes. It seems basic question. But I can'nt find answer by googling.
public abstract class Test {
void t1()
{
System.out.println("super");
}
}
public class concret extends Test{
void t1()
{
System.out.println("child");
}
void t2()
{
System.out.println("child2");
}
}
public class run {
public static void main(String[] args) {
Test t=new concret();
t.t1();
}
}
How do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class?
Thank you.
Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1(). For example:
void t1()
{
super.t1(); // First call the superclass implementation
System.out.println("child");
}
If you've only got an instance of an object which overrides a method, you cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.
you should be able to do it using
Test test = new Test(){};
test.t1();
Abstract class means the class has the abstract modifier before the class keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.
For example :
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
See following tests:
public abstract class BaseClass {
public void doStuff() {
System.out.println("Called BaseClass Do Stuff");
}
public abstract void doAbstractStuff();
}
public class ConcreteClassOne extends BaseClass{
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassOne Do Stuff");
}
}
public class ConcreteClassTwo extends BaseClass{
#Override
public void doStuff() {
System.out.println("Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassTwo Do Stuff");
}
}
public class ConcreteClassThree extends BaseClass{
#Override
public void doStuff() {
super.doStuff();
System.out.println("-Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassThree Do Stuff");
}
}
public class Test {
public static void main(String[] args) {
BaseClass a = new ConcreteClassOne();
a.doStuff(); //Called BaseClass Do Stuff
a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff
BaseClass b = new ConcreteClassTwo();
b.doStuff(); //Overriding BaseClass Do Stuff
b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff
BaseClass c = new ConcreteClassThree();
c.doStuff(); //Called BaseClass Do Stuff
//-Overriding BaseClass Do Stuff
c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
}
}
use keyword 'super' to do that
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use that in the overriden method though.
Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.
If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.
Or you can create a method in the concrete class for example:
public void invokeSuperT1(){
super.t1();
}
Create an anonymous Inner class,
Abstract class:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here is how to create anonymous inner class:
Test test = new Test() {
#Override
void t() {
//you can throw exception here, if you want
}
};
Call the class via the object created for abstract class,
test.t1();
An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.
void t1()
{
super.t1; // means the parent methods
System.out.println("child");
}
For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.
First Create abstarct class like as shown in link: Creating Abstract Class
Create Sub-Classs like as shown in link: Sub-class extending
Creating main method for executing this as show in link: Instanciate the subclass to access
Result as shown here: Result

Categories

Resources