Total members 10261 | Gratitudes |It is currently Wed May 23, 2012 8:44 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Inheritance in java
PostPosted: Fri Feb 06, 2009 10:59 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Inheritance in java :
Inheritance is one of the main concepts of object oriented programming. The base class is named superclass and the child class is named subclass. Using inheritance we can achieve the concepts of reusability. The child class can use the methods and variables of the superclass and add to them its own methods and variables. Inheritance represents problems in the real world.

Example on java inheritance :
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package codemiles;
public class Car {
   protected int CarSpeed;
   protected String CarModel;
   private int ID=0;

    public void setCarModel(String CarModel) {
        this.CarModel = CarModel;
    }

    public void setCarSpeed(int CarSpeed) {
        this.CarSpeed = CarSpeed;
    }

    public String getCarModel() {
        return CarModel;
    }

    public int getCarSpeed() {
        return CarSpeed;
    }
    // Main Function
    public static void main(String[] args) {
   
    }
   
}
class RaceCar extends Car
{
    public void RunTurbo()
    {
        CarSpeed+=100;
    }
   
}
Any instance of of RaceCar can use the members of Car class except the variable (int ID) because it is private.   




Notes:
1- Subclass can only inherit public and protected members but not private members.
2- You can only inherit from single class. (Single inheritance).That is means there is no multiple inheritance in Java. But as a solution to the multiple inheritance java supported class types called “interfaces”.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team