Results 1 to 4 of 4
Thread: Overriding
- 05-20-2010, 10:11 AM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
- 05-20-2010, 10:25 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
When you read the books and tutorials, what did they say were the answers to those questions?
- 05-20-2010, 09:36 PM #3
- 05-21-2010, 08:45 AM #4
Here is an example of an abstract class with only two methods, one abstract and the other concrete.
Java Code:// if one method in a class is abstract, then // the whole class must be declared abstract. // but the abstract class can have concrete methods. public abstract class AbstractClass { public void method1() { System.out.println("concrete method1 from AbstractClass"); } public abstract void method2(); }
here is the example that uses the AbstractClass
Java Code:// abstract classes can't be instantiated, but only // extended, that's why the keyword extends // is used. in order to work, all abstract methods // must be emplomented before they run correctly. public class AbstractExample extends AbstractClass { // here method2 is implemented // the concrete method1 from AbstractClass is inherited public void method2() { System.out.println("implemented method2 from AbstractExample"); } public static void main(String[] args) { AbstractExample example = new AbstractExample(); example.method1(); example.method2(); } }
first, what is encapsulation? according to What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation
simplifying, in java encapsulation is done by declaring your member variables as private, so that the can't be accessed directly from outside the class but only through public setters and getters. a small example is
Java Code:public class Bycicle { private int speed = 0; private int gear = 1; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getGear() { return gear; } public void setGear(int gear) { this.gear = gear; } }
for details about abstraction see also Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
Similar Threads
-
Overriding equals method
By sky in forum New To JavaReplies: 7Last Post: 03-12-2010, 03:39 PM -
Overriding Methods
By AndrewM16921 in forum New To JavaReplies: 2Last Post: 09-23-2009, 06:26 AM -
Method Overriding - Seriously confused :-(
By fullmetaljacket in forum New To JavaReplies: 4Last Post: 05-26-2009, 04:18 PM -
[SOLVED] Method Overriding
By MithunDhar in forum New To JavaReplies: 3Last Post: 04-06-2009, 08:02 AM -
is overriding static method possible
By raghu in forum Advanced JavaReplies: 1Last Post: 01-22-2008, 12:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks