calling non abstract method in abstract class java - 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

Related

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.

Calling a method of abstract class from a concrete class without extending

Example abstract class is bellow.
public abstract class Vehicle {
void maintain(String str) {
System.out.println(str);
}
}
Example concrete class is bellow.
public class Driver {
public static void main(String[] args) {
}
}
Now I need to access the maintain method without extending the Vehicle class.Is there any way to do this without using static content?
No, there isn't, because maintain is an instance method. To call an instance method, you must have an instance. You can't create an instance of an abstract class.
You can subclass it anonymously (see this tutorial), but you still need to subclass it.
You can use an anonymous inner class. I've used your example code but also defined an anstract method in Vehicle
public class AbstractTest {
public static void main(String[] args){
Vehicle v = new Vehicle() {
#Override
void myOtherAbstractMethod() {
// Do what you need here
}
};
v.maintain("foo");
}
public static abstract class Vehicle {
void maintain(String str) {
System.out.println(str);
}
abstract void myOtherAbstractMethod();
}
}
You cannot do that as abstract classes are abstract. Also in your case there's no connection between Driver and Vehicle so even if you would be able to compile that code (you won't), then ClassCastException would show up.
You must extend abstract class first, like it or not.

How to call default interface method from another subclass?

Consider the following:
//Fooable.java
public interface Fooable {
public default void foo() {
System.out.println("Fooable::foo");
}
//Lots of other non-default methods...
}
//MyFooable.java
public class MyFooable implements Fooable {
#Override
public void foo() {
System.out.println("MyFooable::foo");
}
//implements other methods in Fooable...
}
//MyAdvancedFooable.java
public class MyAdvancedFooable extends MyFooable {
#Override
public void foo() {
Fooable.super.foo();
System.out.println("MyAdvancedFooable::foo");
}
public static void main(String[] args) {
new MyAdvancedFooable().foo();
}
}
As you can see, I want to call foo() in Fooable from MyAdvancedFooable (a subclass of MyFooable). However, when I try to compile, I get the following error:
MyAdvancedFooable.java:4: error: not an enclosing class: Fooable
Fooable.super.foo();
if I try MyAdvancedFooable extends MyFooable implements Fooable I get the following:
MyAdvancedFooable.java:4: error: bad type qualifier Fooable in default super call
Fooable.super.foo();
method foo() is overridden in MyFooable
How can I resolve this problem without having to create a new anonymous implementation of Fooable?
You can only call a method one level up so you would need
Fooable.super.foo();
in MyFooable, while just calling super.foo() in MyAdvancedFooable
You need to use just super.foo() or this.super.foo() as it is the parent of the object and not of the class as implied by Fooable.super.foo().

Abstract class method is never used

Say I have the following classes:
abstract class A {
abstract public void methodA();
}
class B extends A {
#Override
public void methodA(){
}
}
And then I use methodA() somewhere else with implementation of class B. In Android Studio, when I opened class A in editor, it gave me the warning that method methodA() is never used. Why is that so?

How to call overriden function from child in java?

I want to make an interface and from user point of view i want it to look clean and they can write their code with this syntax.
public class child extends parent {
#Override
public void run() {
}
}
Main is in the parent but how can one call a overriden function in parent.
Also i don't want the name "child" to be mandatory so i can't call it directly.
PS:this is the run function i want to override.
public class parent{
public static void main(String[] args) {
run();
}
}
Make Parent and run abstract. Abstract for a class means that this class cannot be directly instantiated, but can be instantiated if there is a subclass. Abstract for a method means that the method is defined in the abstract class, but is not implemented in the abstract class. Instead subclasses must provide an implementation of the abstract method or declare themselves to be abstract.
public abstract class Super {
public static void main(String[] args) {
Super s = new Sub();
s.main();
}
public abstract void run();
public void main() {
System.out.println("Calling sub class's implementation of run");
// The super class does not know the implementation of run
// but it does know that there must be an implementation to use.
run();
System.out.println("Done!");
}
}
class Sub extends Super {
#Override
public void run() {
System.out.println("sub class implementation of run");
}
}
to call overridden function of perent class use..
super.run();
EX:
public class child extends parent {
#Override
public void run() {
super.run();
}
}
what i get from you is ..u want to call child function from parent class..
For that is seams like a normal class function coz there is only relationship b/w parent-to-child no relation in child-to-parent
So u just make object of child and then call function of child class...
So your main of parent class may be look like this..
public static void main(String[] args) {
new child().run();
}
To call an overwritten function in the parent user something like one of these:
super.run();
((parent) this).run();
Do you mean you want to call the run() function defined in the parent?
public class child extends parent
{
#Override
public void run()
{
super.run(); // call parent's run() function
this.doStuff(); // call child's additional functionality
}
}
You can call super(Parent) class methods using 'super' keyword. Like this.
public class child extends parent {
#Override
public void run() {
super.run();
}
}
I suppose you are confusing between main() function and some random Main class that you are referring to. In general when it comes to inheritance you cannot call a derived class's overridden function from a base class. In that case what you do is create a base class pointer and make it point to derived class object. In that case when you use the base class pointer to invoke the function the overriden derived classs' function gets called.
I am not sure if I answered you entirely..
This might help you.. -> http://www.oodesign.com/dependency-inversion-principle.html
Not sure if i understand correctly. But parent can't know if, where and how it's protected/public methods will be overriden so it cant call the overridden implementations.
If you have to do it, you probably got your class hierarchy design wrong.
You probably need something like this.
public Parent
{
public final void run()
{
this.runChild
}
protected abstract void runChild();
}
public class Child extends Parent
{
public static void main(String[] args)
{
run();
}
protected void runChild()
{
....
}
}
I am not sure whether I am answering your question right. From what I understood is you are trying to do some thing like calling child's run from Parent. If that is the case below is the code snippet to do it.
public abstract class Parent{
public abstract void run();
public void mainMethod(){
//This automatically calls run from child
run();
}
}
And your child implementation is like as shown below.
public class child extends parent {
#Override
public void run() {
//Do the stuff you want to
}
}
public class MainClass{
public static void main(String args[]){
Parent obj = new Child();
// This inturn takes care of the rest.
obj.mainMethod();
//Some other child
obj = new Child2();
obj.mainMethod();
}
}
Hope this helps you.

Categories

Resources