package supertest;
public class Parent {
public void show (){
System.out.println(" I am father");
}
package supertest;
public class Child extends Parent{
public void show(){
System.out.println("I am child ");
}
}
public class GrandChild extends Child{
public void test(){
super.show();
System.out.println("I am grand child");
}
public static void main(String[] args) {
GrandChild gr=new GrandChild();
gr.test();
}
The super method prints in output "I am child I am grand child"
but I would like to print all three method values "I am father, I am child, I am grand child"
How can I fix this
In grandChild.show(), calling super.show() will invoke child.show().
So, in child.show(), you need call super.show() explictly to invoke parent.show():
public class Child extends Parent{
public void show(){
super.show();
System.out.println("I am child ");
}
}
Related
I am getting error while accessing Child class method by using Parent class reference variable.
Please help me.
How can I access that method?
class Parent
{
public void show()
{
System.out.println("Show method in Parent class");
}
}
class Child extends Parent
{
public void print()
{
System.out.println("Print method in Child class");
}
}
public class Downcast
{
public static void main(String args[])
{
Parent p1=new Child();
p1.print();//showing error here
}
}
Your Parent class knows nothing about methods in your Child class. That's why you get error.
One of the possible solutions is to make your Parent class as abstract and add abstract print() method in it, but in this case all subclasses should override this method:
abstract class Parent {
public void show() {
System.out.println("Show method in Parent class");
}
public abstract void print();
}
class Child extends Parent {
#Override
public void print() {
System.out.println("Print method in Child class");
}
}
public class Downcast {
public static void main(String[] args) {
Parent p1 = new Child();
p1.print();
}
}
The error is caused since the Parent class knows nothing about the Child class. One way to fix the error is by doing an explicit cast ((Child) p1).print();
You can do a cast :
class Parent
{
public void show()
{
System.out.println("Show method in Parent class");
}
}
class Child extends Parent
{
public void print()
{
System.out.println("Print method in Child class");
}
}
public class Downcast
{
public static void main(String args[])
{
Parent p1=new Child();
((Child) p1).print();// Out : Print method in Child class
}
}
I have a java class which has more than 50,000 lines of codes. I want to refactor it and make multiple small classes.
I have tried making child classes but there are challenges (correct me if I am wrong)
a. we cannot call child method with parent object!
b. Furthermore, we can not call a method which is in child from parent.
public class Parent {
public void a(){
System.out.println("Parent :: a");
b(); //point b
}
}
public class Child extends Parent{
public void b(){
System.out.println("Child :: m");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Parent();
p.b(); //point a
Child c = new Child();
c.a();
}
}
Edit: The parent class is a #ManagedBean class ( I am using JSF and Spring ).
Point B
You can, it's called the template method pattern. A parent may define
the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method.
The helper methods may be either abstract methods, for which case subclasses are required to provide concrete implementations, or hook methods, which have empty bodies in the superclass.
The highlighted part is what I would prefer.
public abstract class Parent {
public void a(){
System.out.println("Parent :: a");
b();
}
public abstract void b(); // shouldn't necessarily be public
}
public class Child extends Parent {
#Override
public void b(){
System.out.println("Child :: m");
}
}
Point A
You can't, it's not a part of parent's interface. Either add the method to the parent or rethink why it's called on a parent reference.
A and B can be achieved by the use of polymorphism
public class Parent {
public void a(){
System.out.println("Parent :: a");
b(); //point b
}
public void b() {
System.out.println("not in use");
}
}
public class Child extends Parent{
#Override
public void b(){
System.out.println("Child :: m");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.b();
p.a();
}
}
results:
Child:: m
Parent:: a
Child:: m
is there a way to automatically initialize a subclass when the parent-class is initialized(constructed)?
For example like this:
public class Parent {
public Parent() { //Constructor
...
}
public class Child {
public void foo() {
...
}
}
}
I want to be able to do something like this:
Parent p = new Parent();
p.Child.foo();
Any Ideas? I think it's all about static-ness but I'm not sure, so any help is appreciated. Thanks in advance.
You cannot call it that way.
If the child class must be a non-static class and reside inside a parent class then you will have to initiate it either in the parent class or outside of it before using any of its methods.
You have this option.
public class Parent {
private Child child;
public Child getChild() {
return child;
}
public Parent() { //Constructor
this.child = new Child();
}
public class Child {
public void foo() {
...
}
}
}
After that you can call the foo() method this way.
Parent p = new Parent();
p.getChild().foo();
Maybe somehting like that:
public class Parent {
public Parent() { //Constructor
foo();
}
protected void foo() {
// Do nothing in parent
}
}
public class Child {
#Override
public void foo() {
...
}
}
Edit: this is not a correct anwser as Child does not extend Parent.
Try this code:
This first part is the main. You have to invoke que Parent and make an instance the Child (That is the first lane of the code) After that, you can use the child methods.
Parent child = new Parent().new Child();
child.foo();
On the other hand, the class:
public class Parent {
public Parent(){
}
protected void foo(){
}
public class Child extends Parent{
public Child(){
}
#Override
public void foo(){
System.out.println("I am the child!!");
}
}
}
As you can see that is very similar that you have, but you have to write in the child "extends Parent" to specify the parent of that class.
I hope that could help you!!!
I have a class with method I want to let the execution to child class. Example:
public class Parent {
protected void foo(){return null;}
private void bar(foo());
}
public class Child {
#Override protected void foo(){//do something}
}
and I want to call the foo method of child instead of parent in the bar method. It always calls the parent method.
The foo method can not be abstract...
In the Parent code, if you do:
this.foo();
...then provided the object is a Child instance, it's the Child's foo that will get called. This is an important part of polymorphism. (Assuming, of course, you add extends Parent to the Child declaration.)
Example: (live copy on IDEOne)
class Example
{
public static void main (String[] args)
{
new Child().bar();
}
}
class Parent {
protected void foo() {
System.out.println("Parent#foo");
}
public void bar() {
this.foo();
}
}
class Child extends Parent {
#Override
protected void foo() {
System.out.println("Child#foo");
}
}
Output:
Child#foo
I know that private methods are hidden in Derived class and they cant be overridden but my question is different.. Please go through the following problem. My question is mentioned in the problem stated below:
TestPolymorphism.java
class CheckParent {
private void display() {
System.out.println("This is parent class");
}
}
class CheckChild extends CheckParent {
void display() {
System.out.println("This is child class");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
CheckParent cp = new CheckChild();
cp.display(); // This will throw error as display() method id private
// and invisible to child class
}
However, in following code snippet no exception is thrown.
CheckParent.java
public class CheckParent {
private void display() {
System.out.println("This is parent class");
}
public static void main(String[] args) {
CheckParent cp = new CheckChild();
cp.display(); /*This will print "This is parent class" without any error
* my question is why no error is thrown like above case
* as here too display() method is private and invisible to
* derived class */
}
}
class CheckChild extends CheckParent {
void display() {
System.out.println("This is child class");
}
}
}
For constructor, private, final or static methods static(early) binding is used. For all others dynamic(late) binding is used. Check this out:
http://geekexplains.blogspot.co.uk/2008/06/dynamic-binding-vs-static-binding-in.html
If you would like to override a parent class method please consider placing the #Override annotation in front of the overriding method, so that compiler can warn you of possible mistakes.
I do not think you are overriding the display() method of a parent class with display() method of a child class, because the parent class's method is private. You are rather defining an new method.
class CheckParent {
//remove private access modifier to have the method overriden in child class
private void display() {
System.out.println("This is parent class");
}
}
class CheckChild extends CheckParent {
#Override //IDE or compiler will warn you that you are not overriding anything
void display() {
System.out.println("This is child class");
}
}
Please see Bloch's Effective Java. 2nd Edition, Item 36.
In the second code snippet you create CheckChild instance but store is as CheckParent accessing the private void display() of CheckParent instance method inside the CheckParent class's static method. Class members (static method in your case) can access it's private methods and fields, so this code is valid.
If you want to access the instance void display() method of the CheckChild class instance in the second code snippet you should cast the CheckParent instance to CheckChild first.
public class CheckParent {
private void display() {
System.out.println("This is parent class");
}
public static void main(String[] args) {
CheckParent cp = new CheckChild();
cp.display(); //"This is parent class"
if (cp instanceof CheckChild){
((CheckChild) cp).display(); //"This is child class"
}
}
}
class CheckChild extends CheckParent {
void display() {
System.out.println("This is child class");
}
}
Anyway such coding approach looks like a misuse of inheritance to me.