Wednesday, January 31, 2007

Lecture 11

Inheritance

  • Objects that inherit features of a parent class can be thought to contain an object of the parent type
  • all methods and fields of the parent are "inherited"

Best Practices With Inheritance
  • discard packaging
  • make all instance fields private
    • this means instance fields are not visible to child methods
    • if you need to access instance fields of the parent from the child use the public interface

Why Do This?
  • child inherits parent's methods and therefore inherits the public interface
    • this simplifies access
    • and prevents privacy problems via inheritance

public class Parent
{
protected mutable class x;
}

public class Child extends Parent
{public mutable class leakyMethod()
{return m;
}
}

Constructors
  • form part of the public interface

public class Child extends Parent
{
public child ( ...parameters for child / parent )
//call parent constructor
super(...parameters for parent)
//assigns child parameter
}


Overriding Methods

  • Child classes inherit all methods from the parent
  • child has the option to re-implement these methods
    • called overriding

Example
  • All objects extend type Object by default

public class MyClass [extends Object]
[
  • type object contains several default methods
    • equals(), toString(), clone()
]

  • declaration of an overridden method in the child must be exactly the same as the parent
    • same return type
    • same parameter type
    • same number of parameters

Parent

public int myMethod(double a, int b)

Child

public int myMethod(double a, int b)

  • if myMethod is overridden in a child class, use 'super.myMethod' to gain access to the parent method
  • to prevent overriding use 'final'


Abstract Classes

  • a student is either graduate or undergraduate
    • there is no 'plain' student
  • so it doesn't make sense to instantiate this class
  • all students have letter grades
  • child classes of student should be forced to implement getLetterGrade()

Solution

  • declare Student an abstract class
    • can't instantiate an object of type student
  • declare getLetterGrade() an abstract method within abstract class student
  • children must override getLetterGrade() or they will be abstract classes themselves

public abstract class Student
{
.
.
.
public abstract String getLetterGrade();

}

14 comments:

George said...

Thank you
I appreciate your blog

Anonymous said...

Can simeone explain Copy Constructor and Deep COpy??
How are they used?
ThanX

Jake said...

http://jacobdevos.blogspot.com/2007/01/lecture-8.html
lecture 8 explains it all ...
have you written the test yet?
if not.. that's a big part of it

Anonymous said...

ThanX Jake,
no, i'll write the test on Friday.

Anonymous said...

Do you know which examples from Andrew's blog are related to Deep Copy and Copy Constructor?

Anonymous said...

Heya i am for the first time here. I found this board and
I find It truly useful & it helped me out much. I hope to give something back and help others like you helped me.


My web page - exercises for vertical

Anonymous said...

Paragraph writing is also a excitement, if you know then you can
write or else it is complicated to write.


Here is my homepage - workouts to increase vertical leap

Anonymous said...

hello!,I really like your writing very so much! percentage we communicate more approximately your post on AOL?

I require an expert on this space to solve my problem.
Maybe that is you! Having a look ahead to look you.


Look into my web page - workouts to increase vertical jump

Anonymous said...

I believe what you composed was actually very logical.
However, consider this, suppose you were to write a awesome post title?

I am not suggesting your information is not good, but what if you added a post title to possibly grab a person's attention? I mean "Lecture 11" is a little vanilla. You might glance at Yahoo's
home page and watch how they write news titles to grab viewers to open
the links. You might add a related video or a related
pic or two to get people interested about everything've got to say. Just my opinion, it could bring your website a little livelier.

Also visit my webpage - the jump manual review

Anonymous said...

bookmarked!!, I like your site!

My web blog - www.linkiamo.com

Anonymous said...

It's enormous that you are getting ideas from this post as well as from our argument made at this time.

Here is my web page - exercises to increase vertical

Anonymous said...

Unquestionably believe that which you said. Your favourite reason appeared to be
on the net the easiest thing to have in mind of.

I say to you, I certainly get irked even as folks think about issues that they plainly
don't recognise about. You managed to hit the nail upon the highest and also outlined out the entire thing without having side effect , folks can take a signal. Will probably be again to get more. Thanks

Here is my web-site http://www.iamsport.org

Anonymous said...

Great post.

Feel free to visit my webpage - exercises for vertical jump

Anonymous said...

Pretty great post. I just stumbled upon your weblog and wanted to mention that I have truly loved surfing around your weblog
posts. After all I'll be subscribing for your feed and I hope you write again soon!

My blog post: exercises to jump higher exercises to improve vertical exercises to improve vertical jump exercises to improve vertical leap exercises to increase vertical exercises to increase vertical jump exercises to increase vertical leap exercises for vertical exercises for vertical jump exercises for vertical leap workouts to jump higher workouts to improve vertical workouts to improve vertical jump workouts to improve vertical leap workouts to increase vertical workouts to increase vertical jump workouts to increase vertical leap workouts for vertical workouts for vertical jump workouts for vertical leap vertical jump exercises vertical leap exercises vertical jump workouts vertical leap workouts