Download presentation
Presentation is loading. Please wait.
1
Java Programming with Multiple Classes
Two classes Three classes More classes
2
public class ClassTwo{
private int count; public ClassTwo() { // constructor count = 5; } public void method1() { count++; System.out.println(“Method 1:“ + count); public void method2() { System.out.println(“Method 2:“+ count); public void method3() { System.out.println(“Method 3:“ + count); public class ClassOne { public static void main(String[] args) ClassTwo ctwo = new ClassTwo(); ctwo.method1(); ctwo.method2(); ctwo.method3(); }
3
cThree can only be used in method1()
public class ClassTwo{ private int count; public ClassTwo() { // constructor count = 5; } public void method1() { count++; System.out.println(“Method 1:“ + count); ClassThree cThree = new ClassThree(); cThree.Class3Method1(); public void method2() { System.out.println(“Method 2:“+ count); public void method3() { System.out.println(“Method 3:“ + count); public class ClassOne { public static void main(String[] args) ClassTwo cTwo = new ClassTwo(); cTwo.method1(); cTwo.method2(); cTwo.method3(); } cThree can only be used in method1() public class ClassThree{ private int num; public ClassThree() { // constructor num = 3; } public void Class3Method1() { num++; System.out.println("Class 3 method 1:“ + num);
4
public class ClassTwo{
private int count; ClassThree cThree; public ClassTwo() { // constructor cThree = new ClassThree(); count = 5; } public void method1() { count++; System.out.println(“Method 1:“ + count); cThree.Class3Method1(); public void method2() { System.out.println(“Method 2:“+ count); public void method3() { System.out.println(“Method 3:“ + count); public class ClassOne { public static void main(String[] args) ClassTwo cTwo = new ClassTwo(); cTwo.method1(); cTwo.method2(); cTwo.method3(); } public class ClassThree{ private int num; public ClassThree() { // constructor num = 3; } public void Class3Method1() { num++; System.out.println("Class 3 method 1:“ + num);
5
public class ClassTwo{
private int count; ClassThree cThree; public ClassTwo() { cThree = new ClassThree(); count = 5; } public void method1() { count++; System.out.println(“Method 1:“ + count); cThree.Class3Method1(); public void method2() { System.out.println(“Method 2:“+ count); public void method3() { System.out.println(“Method 3:“ + count); public class ClassOne { public static void main(String[] args) ClassTwo cTwo = new ClassTwo(); cTwo.method1(); cTwo.method2(); cTwo.method3(); } public class ClassThree{ private int num; public ClassThree() { // constructor num = 3; } public void Class3Method1() { num++; System.out.println("Class 3 method 1:“ + num);
6
More Classes?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.