Inheritance and extending classes

Mkelliny
Mkelliny used Ask the Experts™
on
I am lost on this totally I followed the examples given, but I still am getting errors. I need to create a class called book which gets/sets the title and number of pages the subclass Textbook holds a grade level. demoBook inherits the results. I think I screwed it up pretty bad.
public class Book
	
	{
		private int numTitle;
		private int numPages;
		
		public int getNumTitle()
		{
			return numTitle;
		}
		public int getNumPages()
		{
			return numPages;
		}
		public void setNumTitle(int num)
		{
			numTitle = num;
		}
		public void setNumPages(int pag)
		{
		numPages = pag;
		}
	
	}






public class Textbook extends Book
	
	{
		private int numGradeLevel;
		
		
		public int getNumGradeLevel()
		{
			return numGradeLevel;
		}
			public void setNumGradeLevel(int grd)
		{
			numGradeLevel = grd;
		}
		
	}



public class DemoBook extends Book
{
	public static void main()
	{	
		DemoBook bookWithGradeLevel = new DemoBook();
	}
		
		 BookWithGradeLevel.getNumTitle();
		 BookWithGradeLevel.getNumPages();
		 BookWithGradeLevel.getNumGradeLevel();
		 
		 BookWithGradeLevel.setNumTitle(1);
		 BookWithGradeLevel.setNumPages(23);
		 BookWithGradeLevel.setNumGradeLevel(4);
	   }
		
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
So the first part is understandable -
TextBook extends Book and has one additional field numGradeLevel

What about DemoBook - it should also extend Book, but what new fields it should have?

Perhpas say number of illustrations?

demoBook inherits the results - this is not very understandable to me
Awarded 2011
Awarded 2011

Commented:
Or maybe DemoBook should extend TextBook and have some more additional field - along with pages, title, and numGradeLevel - ?
Awarded 2011
Awarded 2011

Commented:
In the way you coded DemoBook extends Book, but not TextBook,
therefore you cannot use this method:

 BookWithGradeLevel.setNumGradeLevel(4);

because it does not exist in the class Book,

and DemoBook does not inherit from TextBook, so it cannot use this method
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Demobook uses the objects from Textbook and Book.

Author

Commented:
How would I be able to get Demobook to inherit from Textbook and Book?
Awarded 2011
Awarded 2011

Commented:
You would declare

public class Demobook extends Textbook {
...
Awarded 2011
Awarded 2011

Commented:
Once it inherits from Textbook it will have al fields of Textbook and also of Book
becuse Textbook inherits from Book

Author

Commented:
So if it just inherits from Textbook it will also inherit from Book?
Awarded 2011
Awarded 2011

Commented:
yes, iheritance "is inherited".

So you "son" will have all files of a "father" and grandson has all fileds of both father and son
Awarded 2011
Awarded 2011

Commented:
The only difference from the analogy with father and son is that you should
understand that subclass ("son") is like father but "more complicated" than "father" (maybe as  opposed to real life fater and son situation)
The subclass is more complicated because it has all the fields of the father plus some new fields which father did not have
Awarded 2011
Awarded 2011

Commented:
Therefore each instance of "son" will ALWAYS at the same time be an instance of "father".

The opposite statement will not be true, as not every instance of father has those additonal
fields which would make it also an instance of son.

So father chracterizes a broader range of objects, and son characterizes a subset of objects of the father class
with some additional properties.

So Textbooks make a subset of all Books

Author

Commented:
Awesome! I appreciate that analogy. I understand how the inheritance works now. I have reduced the number of errors I receive from 19 to 10. I seem to really have messed up my Demobook section.
Awarded 2011
Awarded 2011
Commented:
these are compiling fine for me;

put each public class into a separate file with the same name - Textbook.java, Book.java, Demobook.java

public class Book

	{
		private int numTitle;
		private int numPages;

		public int getNumTitle()
		{
			return numTitle;
		}
		public int getNumPages()
		{
			return numPages;
		}
		public void setNumTitle(int num)
		{
			numTitle = num;
		}
		public void setNumPages(int pag)
		{
		numPages = pag;
		}

	}

Open in new window


public class Textbook extends Book

	{
		private int numGradeLevel;


		public int getNumGradeLevel()
		{
			return numGradeLevel;
		}
			public void setNumGradeLevel(int grd)
		{
			numGradeLevel = grd;
		}

	}

Open in new window


public class Demobook extends Textbook
{
	public static void main()
	{
		Demobook bookWithGradeLevel = new Demobook();


		 bookWithGradeLevel.getNumTitle();
		 bookWithGradeLevel.getNumPages();
		 bookWithGradeLevel.getNumGradeLevel();

		 bookWithGradeLevel.setNumTitle(1);
		 bookWithGradeLevel.setNumPages(23);
		 bookWithGradeLevel.setNumGradeLevel(4);
	   }

}

Open in new window

Author

Commented:
Oh Durp lol I am a GENIUS! I made Book.java like this
public class Book
	
	{
		private int numTitle;
		private int numPages;
		
		public int getNumTitle()
		{
			return numTitle;
		}
		public int getNumPages()
		{
			return numPages;
		}
		public void setNumTitle(int num)
		{
			numTitle = num;
		}
		public void setNumPages(int pag)
		{
		numPages = pag;
		}
	
	}






public class Textbook extends Book
	
	{
		private int numGradeLevel;
		
		
		public int getNumGradeLevel()
		{
			return numGradeLevel;
		}
			public void setNumGradeLevel(int grd)
		{
			numGradeLevel = grd;
		}
		
	}



public class DemoBook extends Textbook
{
	public static void main(String[] args)
	{	
		DemoBook bookWithGradeLevel = new DemoBook();
	
		
		 BookWithGradeLevel.getNumTitle();
		 BookWithGradeLevel.getNumPages();
		 BookWithGradeLevel.getNumGradeLevel();
		 
		 BookWithGradeLevel.setNumTitle(1);
		 BookWithGradeLevel.setNumPages(23);
		 BookWithGradeLevel.setNumGradeLevel(4);
	   }
		
}

Open in new window

Author

Commented:
Thanks again For Yan! It is compiling

Author

Commented:
One more question, Does the fact that it is compiling show that there is inheritance between the three?
Awarded 2011
Awarded 2011

Commented:
You are welcome.

One minor suggestion: when programming where possible try to stick to kind of templates - say if
you have class Textbook (lower case "b") also rather make class Demobook (rather than DemoBook)

It is difficult to do everything uniformly, howvere the more you try the more you'll be rewarded -
some time later you'll spend less time trtying to rememeber how you named them
Awarded 2011
Awarded 2011

Commented:

 Of course as soon as you say "extends" compiler recognizes this as inheritance.
That's why it allows you to use say method getNumPages() in the DemoBook class, though
this method was defined in the Book class;
just remove "extends Textbook" from the header of DemoBook and it will immediately complain

Author

Commented:
You are my Java Hero! Thanks so much and as always you are extremely appreciated! :)
Awarded 2011
Awarded 2011

Commented:
You are always welcome.

Author

Commented:
For Yan you absolutely ROCK! Easy to follow directions, great explanations, and are extremely patient and pleasant!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial