say we have three classes: class a , class b , class c;
class b inherits class a , if we define that class c inherits class b(which inherits class a) will the code give an error .If not the can we say that class c inherits class a;
in all i ask that can there be a subclass of a subclass??
In short, yes, you could definitely have a "chain" of inheritance. When you have a class A that inherits another class B, then it doesn't matter whether class B inherits from another class.
Though, you should keep in mind that a class is not able to inherit from multiple classes (it would throw a compiler error). Multiple inheritance in Java is achievable through the use of interfaces.
Yes, Multilevel inheritance refers to a mechanism where one can inherit from a derived class, thereby making this derived class the base class for the new class.
for example
Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}
Related
I have three interfaces A, B and C and one class D. B and C extend A and D implements B and C. Interface A has a default method called "does". I was expecting to have a compile-time error or run-time error, however, the program was executed without error.
Both B and C inherit from A. Shouldn't the compiler be confused as to which inherited method be called on D since D inherits two default methods one from C and one from B.
All of the three interfaces and the class are defined in separate files. I have placed the code sequentially in the code area below for demonstration.
public interface A {
default public void does(){
System.out.println("A");
}
}
public interface B extends A {
}
public interface C extends A {
}
public class D implements B, C {
}
public class Sample {
public static void main(String[] args) {
new D().does();
}
}
If class A creates an instance of class B, then in class B can I run a method from class A? sorry if the question is poorly worded, I don't know how else I could say it.
In the below code, Class A creates an instance of Class B and you can call Class A's method from Class B's method.
class A {
public void getA() {
System.out.println("In A");
}
public static void main(String[] args) {
B b = new B();
b.getB();
}
}
class B {
public void getB() {
System.out.println("In B");
A a = new A();
a.getA();
}
}
Output:
In B
In A
In class B you can call methods of class A only if A's methods are visible to B. It doesn't matter who created an instance of B.
This tutorial might help: http://www.tutorialspoint.com/java/java_access_modifiers.htm
So you can call it if the method is one of the following:
public
protected (and B is a subclass of A or B is in the same package as A)
no modifier (and B is in the same package as A)
I confused if
Abstract Class A{method();method2();}
And Other Class B Which Have Inner Class C
Class B{Abstract Class C{method(){//body}}}
And now Question is how to extends Class C b/C Abstract Class must be extends else
this is Unused class.
First, let's make it simpler - this has nothing to do with Android directly, and you don't need your A class at all. Here's what you want:
class Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
}
That doesn't compile, because when you create an instance of Child you need to provide an instance of Outer to the Inner constructor:
Test.java:6: error: an enclosing instance that contains Outer.Inner is required
class Child extends Outer.Inner {
^
1 error
There are two options that can fix this:
If you don't need to refer to an implicit instance of Outer from Inner, you could make Inner a static nested class instead:
static abstract class Inner {
}
You could change Child to accept a reference to an instance of Outer, and use that to call the Inner constructor, which uses slightly surprising syntax, but works:
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
Note that these are alternatives - you should pick which one you want based on whether or not the inner class really needs to be an inner class.
You simply extend it
class B{abstract class C{abstract void method();}}
class D extends B{
class E extends C{
void method(){
System.out.println("Hello World");
}
}
}
Or slightly more complicated without extending outer class
class B{abstract class C{abstract void method();}}
public class F extends B.C{
F(B b){
b.super();
}
void method(){
System.out.println("Hello World");
}
public static void main(String[] args){
B b = new B();
F f = new F(b);
f.method();
}
}
Can somebody please tell me if the keyword 'extends' must be an used (in the syntax) of child classes that overide methods of their super class.
The word extends is used to indicate for the whole class that this class is a sub-class of another class. It is not related to whether the sub-class overrides certain methods or not, that is entirely up to the sub-class class. The sub-class may decide to override none, some, or all of the methods of the super-class. The sub-class may override only methods which are not marked as final in the super-class.
Here is a somewhat trivial example:
class A {
// This is the super-class.
public void myMethod() {...};
}
class B extends A {
// This extends above says: B is sub-class of A.
// So this class B is the sub-class of A.
// You can override methods of A here, like this
public void myMethod() {...};
// but you're not required to override them.
}
Polymorphism in java is a concept by which we can perform a single action by different ways.it uses 2 concepts such as method overloading and method over riding.
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilising the method's name.
the method over riding concepts uses the key word 'extends'.
We can extend a class by using the extends keyword in a class declaration after the class name and before the parent class.
public class ParentClass {
}
and we define child class like
public class ChildClass extends ParentClass {
}
// example of extending a class
class B {
int x = 0;
void f1 () { x = x+1;}
}
class C extends B {}
public class Test1 {
public static void main(String[] args) {
B b = new B();
b.f1();
System.out.println( b.x ); // prints 1
}
}
// example of extending a class, overwriting a method
class B {
int x;
void setIt (int n) { x=n;}
void increase () { x=x+1;}
}
class C extends B {
void increase () { x=x+2;}
}
public class Test2 {
public static void main(String[] args) {
B b = new B();
b.setIt(2);
b.increase();
System.out.println( b.x ); // prints 3
C c = new C();
c.setIt(2);
c.increase();
System.out.println( c.x ); // prints 4
}
}
I was reading a Java tutorial and it said:
Abstract classes cannot be instantiated, but they can be subclassed.
what does this mean? I thought one had to instantiate in order to create a subclass? This line has really confused me, any and all help is much appreciated.
Instantiate:
AbstractClass a = new AbstractClass(); //illegal
Subclass:
class ConcreteClass extends AbstractClass { ... }
ConcreteClass c = new ConcreteClass(); //legal
You must create a new class that extends the abstract class, implement all of the abstract methods, and then use that new class.
Note that you can also do this:
class ConcreteClass extends AbstractClass { ... }
AbstractClass a = new ConcreteClass(); //legal
A subclass can get all the properties/methods that its parent class has, whereas, instantiated class is when you make an instance of that parent class in memory.
When you say instantiated it means you would like to create the object of the class. Sub-class is the inheriting child.
For example,
abstract class A{
//Class A is abstract hence, can not be instantiated. The reason being abstract class provides the layout of how the concrete sub-class should behave.
pubic abstract void doSomething();
//This abstract method doSomething is required to be implemented via all the sub-classes. The sub-class B and C implement this method as required.
}
class B extends A{
//Class B is subclass of A
public void doSomething(){ System.out.println("I am class B"); }
}
class C extends A{
//Class C is subclass of A
public void doSomething(){ System.out.println("I am class C"); }
}
if you try to do this, it would generate an exception
A a = new A();
But this would work fine.
B b = new B();
or
A a = new B(); //Note you are not instantiating A, here class A variable is referencing the instance of class B