Results 1 to 4 of 4

Thread: Overriding

  1. #1
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Overriding

    Hi !

    I have questions on overriding and abstraction, encapsulation , If any one can tell me that


    Q.1 How to prevent overriding?
    Q.2 Example on abstraction !
    Q.3 Example on encapsulation !


    Thanks

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    When you read the books and tutorials, what did they say were the answers to those questions?

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by renuka_renukut View Post
    Hi !

    I have questions on overriding and abstraction, encapsulation , If any one can tell me that


    Q.1 How to prevent overriding?
    Q.2 Example on abstraction !
    Q.3 Example on encapsulation !


    Thanks

    Ok for Q.1: You can prevent overriding by declaring your method final. So when the class is extended by a subclass the subclass can't override the final method.

    How much do you offer for Q.2 and Q.3 :)

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by renuka_renukut View Post
    Q.2 Example on abstraction !

    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();
    	}
    }

    Quote Originally Posted by renuka_renukut View Post
    Q.3 Example on encapsulation !

    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

  1. Overriding equals method
    By sky in forum New To Java
    Replies: 7
    Last Post: 03-12-2010, 03:39 PM
  2. Overriding Methods
    By AndrewM16921 in forum New To Java
    Replies: 2
    Last Post: 09-23-2009, 06:26 AM
  3. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 04:18 PM
  4. [SOLVED] Method Overriding
    By MithunDhar in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 08:02 AM
  5. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •