Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How to create sublass ?
New Posts  All Forums:Forum Nav:

How to create sublass ?

post #1 of 14
Thread Starter 
How to create a subclass in NetBean IDE 7.2 ?
I have done this >>>


public class C {
private int m;
private int n;
public C(int mIn , int nIn){
m=mIn;
n=nIn;
}
public int m1(){
return m+n;

}


I need to create a subclass to OVERRIDES methods m1 so that its return the difference between m and n instead of sum !
post #2 of 14
just create another class and make it inherit from class C and then override it.


offtopic.gif
and please for the love of god PEOPLE stop naming classes and variables like this (C, x, y, r, f, n). for the love of god name the class to what it actually does.

name classes like Car, Enemy, Bullet, Math, Physics. and functions like ConvertRadius, CalculateAngle, PrintString, KillObjectiveC.

sorry, that's my rant for today.
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #3 of 14
Lol @ KillObjectiveC
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
Hackintosh
(16 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770K MSI Z77 MPower Asus GTX 670 DCII G.SKILL Ares 2x4GB 
Hard DriveCoolingOSOS
Samsung 830 SSD NZXT Havik 140 Mac OSX 10.8 Windows 8 Professional 
MonitorKeyboardPowerCase
Yamakasi Catleap Q270 Logitech G510 Seasonic X750 Corsair 500R 
MouseAudio
Logitech G9x Logitech Z623 
  hide details  
Reply
post #4 of 14
Thread Starter 
Quote:
Originally Posted by Fantasy View Post

just create another class and make it inherit from class C and then override it.
offtopic.gif
and please for the love of god PEOPLE stop naming classes and variables like this (C, x, y, r, f, n). for the love of god name the class to what it actually does.
name classes like Car, Enemy, Bullet, Math, Physics. and functions like ConvertRadius, CalculateAngle, PrintString, KillObjectiveC.
sorry, that's my rant for today.
Sorry , the question asked to name like this . LOL .
err , i wrote public class B extends C , but its show me syntax error .
post #5 of 14
Quote:
Originally Posted by Yeong View Post

Sorry , the question asked to name like this

Tell your teacher that it is wrong to teach bad habits. It is much easier to understand object-oriented concepts when you think in terms of real world objects (eg. Car and Truck extend Vehicle) rather than meaningless letters. There is absolutely no reason to make the code so abstract that it can't be understood.
Quote:
Originally Posted by Yeong View Post

err , i wrote public class B extends C , but its show me syntax error .

Post all of the code and the full error message. It's hard enough to understand what B and C are meant to be without having to imagine what C might contain. Also, a syntax error can be caused by hundreds of things, so just saying that you've got one doesn't let us help you, especially when we can't see the syntax causing the error. thumb.gif

Oh, and post the code inside [ code ][ /code ] tags so that the formatting is maintained.
Edited by randomizer - 10/22/12 at 3:08am
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #6 of 14
Thread Starter 

post #7 of 14
Quote:
Originally Posted by qhduong92 View Post

Lol @ KillObjectiveC

biggrin.gif

Quote:
Originally Posted by Yeong View Post



You can't inherit from a class that has arguments in the constructor . You need to have an empty constructor to overload the other constructor that has arguments.

so do this
Code:
public class C 
{
    private int m;
    private int n;
    
//Empty constructor so we can inherit from this class
    public C()
    {
        

    }

    public C(int mIn , int nIn)
    {
        m=mIn;
        n=nIn;
    }
    
    public int m1()
    {
        return m+n;
    }
}

Code:
public class B extends C{

}


good luck thumb.gif
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #8 of 14
Thread Starter 
Quote:
Originally Posted by Fantasy View Post

biggrin.gif
You can't inherit from a class that has arguments in the constructor . You need to have an empty constructor to overload the other constructor that has arguments.
so do this
Code:
public class C 
{
    private int m;
    private int n;
    
//Empty constructor so we can inherit from this class
    public C()
    {
        
    }
    public C(int mIn , int nIn)
    {
        m=mIn;
        n=nIn;
    }
    
    public int m1()
    {
        return m+n;
    }
}
Code:
public class B extends C{
}
good luck thumb.gif
thank you so much ! i appreciate it !
Did you know how to overrides method ?
the words "get" cant use
post #9 of 14
Quote:
Originally Posted by Yeong View Post

thank you so much ! i appreciate it !
Did you know how to overrides method ?
the words "get" cant use
Code:
public class B extends C{
    
    @Override
    public int m1()
    {
       //New code in here
        return 0;
    }
}
Quote:
Originally Posted by Yeong View Post

the words "get" cant use

mmm what ?!
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 3930K 5040MHz @ 1.48 - 1.53v @ VTT/VCCSA ... Asus rampage extreme IV Gigabyte GTX 970 G1 @ 1580/4050MHz @ 1.325V Corsair Vengeance 64GB (8 x 8GB) DDR3 2400MHz. ... 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 Pro 256GB Seagate Barracuda ST3000DM001 3TB WD Black 1TB 64Mb Cache  WD Blue 500GB 16mb Cache 
Hard DriveOptical DriveCoolingCooling
WD Black 4TB 64mb Cache LG DVD EK-FB KIT RE4 - Acetal EK-FC970 GTX WF3 Backplate - Black 
CoolingCoolingCoolingCooling
Laing D5 Vario 12V DC Pump (MCP 655)  EK-BAY SPIN Reservoir - Plexi EK-CoolStream RAD XT (240) EK-CoolStream RAD XTX (120) 
CoolingCoolingCoolingCooling
EK-CoolStream XE 120 (Single) 8x Corsair Air SP120 PWM High Performance Edition 3x Corsair Air AF120 High Performance Edition EK-Supremacy EVO CPU Water Block (Nickel Acetal) 
CoolingOSMonitorMonitor
EK-FC970 GTX WF3 - Acetal+Nickel Windows 10 64-bit v1511 build 10586.14 BenQ RL2755HM LG W2261 
MonitorKeyboardPowerCase
Samsung S23C350 Razer BlackWidow Ultimate Cooler Master Silent Pro Gold 1000W Thermaltake Level 10 GT 
Mouse
Razer Lachesis 5600DPI 
  hide details  
Reply
post #10 of 14
Couldn't he also do it like this?
Code:
public class C 
{
    protected int m;
    protected int n;
    
    public C(int mIn , int nIn)
    {
        m=mIn;
        n=nIn;
    }
    
    public int m1()
    {
        return m+n;
    }
}
Code:
public class B extends C{

    public B(int mIn, int nIn) {
        super(mIn, nIn);
    }    
    
    @Override
    public int m1()
    {
        return m*n;
    }
}
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › How to create sublass ?